Well here is my code:
import java.applet.*;
import java.awt.*;
public class pokemon extends Applet implements Runnable
{
int x_pos = 0;
int y_pos = 0;
private Image dbImage;
private Graphics dbg;
int x_speed = 12;
int y_speed = 12;
int appletsize_x = 425;
int appletsize_y = 575;
int tile_horiz = 17;
int tile_vertic = 23;
AudioClip background;
Image backImage, mainChar, standUp, standDown, standRight, standLeft;
public void init() {
setBackground (Color.black);
background = getAudioClip (getCodeBase(), "");//would work but no sound file present(TESTED!)
backImage = getImage (getCodeBase (), "images/route1.gif");
standDown = getImage (getCodeBase (), "images/mainCharacter/standDown.gif");
standUp = getImage (getCodeBase (), "images/mainCharacter/standUp.gif");
standLeft = getImage (getCodeBase (), "images/mainCharacter/standLeft.gif");
standRight = getImage (getCodeBase (), "images/mainCharacter/standRight.gif");
}
public void start() {
Thread th = new Thread (this);
th.start ();
mainChar = standDown;
}
public void stop() { }
public void destroy() { }
public boolean keyDown (Event e, int key) {
// user presses left cursor key
if (key == Event.RIGHT){
mainChar = standRight;
while(x_pos > x_pos - tile_horiz){
x_pos -= x_speed;
repaint();
}
}
// user presses right cursor key
else if (key == Event.LEFT){
mainChar = standLeft;
while(x_pos < x_pos + tile_horiz){
x_pos += x_speed;
repaint();
}
//user presses up cursor key
}else if (key == Event.UP){
mainChar = standUp;
while(y_pos > y_pos - tile_vertic){
y_pos -= y_speed;
repaint();
}
//user presses down cursor key
}else if (key == Event.DOWN){
mainChar = standDown;
while(y_pos < y_pos + tile_vertic){
y_pos += y_speed;
repaint();
}
}else{
// testing other keys
System.out.println ("Character: " + (char)key + " Integer Value: " + key);
}
return true;
}
public void run () {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g) {
g.drawImage (backImage, x_pos, y_pos, this);
g.drawImage(mainChar, 217, 287, this);
}
}

New Topic/Question
Reply




MultiQuote



|