I'm having trouble creating a simple tron game in java. heres the
code I have so far which (sort of) does what I want, but it flickers
alot with the refresh:
import java.awt.*;
import java.applet.*;
public class Project41# extends Applet implements Runnable
{
int p1SpeedX = 0;
int p1SpeedY = 0;
int p1X = 10;
int p1Y = 10;
int appletWidth; // size of the applet - x
int appletHeight; // size of the applet - y
Image Buffer;
Graphics gBuffer;
public void init()
{
appletWidth = size().width;
appletHeight = size().height;
Buffer=createImage(appletWidth,appletHeight);
gBuffer=Buffer.getGraphics();
}
public void start ()
{
// creates a new thread
Thread th = new Thread (this);
// Starts the thread
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public boolean keyDown (Event e, int key)
{
if (key == Event.LEFT)
{
p1SpeedX = -1;
p1SpeedY = 0;
}
else if (key == Event.RIGHT)
{
p1SpeedX = 1;
p1SpeedY = 0;
}
else if (key == Event.UP)
{
p1SpeedY = -1;
p1SpeedX = 0;
}
else if(key == Event.DOWN)
{
p1SpeedY = 1;
p1SpeedX = 0;
}
else
{
System.out.println ("Charakter: " + (char)
key + " Integer Value: " + key);
}
return true;
}
public void run ()
{
Thread.currentThread().setPriority
(Thread.MIN_PRIORITY);
while (true)
{
if (p1X > appletWidth)
{
p1X = 0;
}
else if (p1X < 0)
{
p1X = appletWidth;
}
if (p1Y > appletHeight)
{
p1Y = 0;
}
else if (p1Y < 0)
{
p1Y = appletHeight;
}
p1X += p1SpeedX;
p1Y += p1SpeedY;
repaint();
try
{
Thread.sleep (5);
}
catch (InterruptedException ex)
{
// do nothing
}
Thread.currentThread().setPriority
(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.red);
gBuffer.fillOval(p1X, p1Y, 2 * 5, 2 * 5);
g.drawImage (Buffer,0,0, this);
}
}
Can anyone suggest a better way of coding this? ideally I would
just like one pixel that u can move about the screen with the
directional keypad and it leaves an un-broken trail behind wherever
it goes. im guessing that i would then need some kind of get color
at a specific x,y coordinate function to check just infront of the
user controlled line to see if it has intersected with another line.