1
你能看到問題嗎?線程運行良好,但磚塊不想響應關鍵偵聽器。我嘗試測試,如果keylistener
的病情終於能一個事件,但它甚至沒有做system.out.println
keylistener和runnable不能正常工作
import java.awt.*;//imports
import java.util.*;
import java.applet.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Start extends Applet implements Runnable, KeyListener// where i put the keylistener in
{
DrawBackground dbg = new DrawBackground();
Brick brick = new Brick();
Thread gameLoop;
public void init() {
addKeyListener(this);// i add the key listener
}
public void start() {
Thread gameLoop = new Thread(this);
gameLoop.start();
}
public void run() {
while (true) {
brick.update(1);
repaint();
try {
Thread.sleep(17);
}
catch (InterruptedException e) {
}
}
}
public void stop() {
}
public void paint(Graphics g)// with out any paint it works if im changing
// somthing like a lable
{
dbg.paint(g, this);
brick.paint(g, this);
}
public void keyPressed(KeyEvent e)// test to see if it works
{
System.out.println("why");
if (e.getKeyCode() == 37) {
brick.left();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
這是磚類我想要的東西移動
import java.awt.*;
import java.util.*;
import java.applet.*;
public class Brick {
public int dy = 40;
public int yStart = -20;
public int time = 0;
public int dx = 0;
public int xStart = 0;
public int start = 1;
public void paint(Graphics g, Start sp) {
Dimension screenSize = sp.getSize();
int sheight = screenSize.height;
int swidth = screenSize.width;
if (start == 1) {
xStart = swidth - (int) (swidth/2.5);
start = 0;
}
int bWidth = swidth/15;
int bHeight = swidth/15;
int time = 0;
g.setColor(Color.red);
g.fillRect(xStart, yStart, bWidth, bHeight);
}
public void update(int x) {
if (time == 60) {
time = 0;
yStart += dy;
}
else {
time += x;
}
}
public void left() {
xStart -= dx;
}
}
該線程是否必要?它有什麼作用 ? –
是的,它使塊向下移動 – ZergRushJoe
它的俄羅斯方塊遊戲imring使 – ZergRushJoe