【发布时间】:2023-05-09 08:44:01
【问题描述】:
基本上,我正在尝试制作一个程序,当按下左箭头键时,将“盘子”向左移动一个像素,按下右箭头键时向右移动一个像素。目前,当我编译和运行时,我的输出窗口上没有任何内容,我不太确定我在这里做错了什么,或者代码到底在哪里没有做我想要的。感谢您的帮助,谢谢!
import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;
public class game extends Applet
{
Thread loopThread;
boolean left = false;
boolean right = false;
int platPos = 50;
public void run()
{
Graphics g = null;
int i, j;
long startTime;
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
startTime = System.currentTimeMillis();
while (Thread.currentThread() == loopThread)
{
updatePlatter(g);
}
}
public void updatePlatter(Graphics g)
{
if(left)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos, 200, 100, 20);
platPos--;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200, 100,20);
}
if(right)
{
g.setColor(new Color(255,255,255));
g.fillRect(50+platPos,200,100,20);
platPos++;
g.setColor(new Color(100,100,100));
g.fillRect(50+platPos,200,100,20);
}
}
public boolean keyDown(Event e, int key)
{
if (key == Event.LEFT)
left = true;
if (key == Event.RIGHT)
right = true;
return true;
}
}
【问题讨论】:
-
loopThread从未设置 -
那样,您将来可能会遇到问题,因为您从未将
left和right重置为 false。 -
旁注:考虑查看小程序以外的其他选项。你似乎是新人,所以这是切换到通常不被认为已死的东西的绝佳机会。
标签: java