Skip to main content

maze program (reading arrow keys)

1 reply [Last post]
puhatch
Offline
Joined: 2008-05-09

i'm a sophomore in high school. i'm writing a maze program as my end-of-the-year application. the main point was to have a turtle go through a maze, picking up ten eggs, and get to the end with those ten eggs. the program will identify the eggs when the turtle is drawn at a certain location. my original idea was to use the "a", "s", "d", and "w" keys to move like games i've seen online. but my teacher said that in an application, he would rather us have the program read in the arrow keys. this is my problem... just for clarification, the turtle image is written in another program and the majority of my program is NOWHERE near complete. i know i have some unused variables and no comments. if you need any clarification, just ask. i just need to know why the turtle wont move when i press the arrow keys... any help would be appreciated. here's my program...

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class PS_Project extends JApplet
{
int egg = 0, turtlex = 10, turtley = 475, end = 0, Key;
String pressed, keypressed, line;
PS_Turtle Turtle = new PS_Turtle(turtlex, turtley);

public class KeyHandler implements KeyListener
{
public void keyPressed (KeyEvent event)
{
Key = event.getKeyCode();
keypressed = event.getKeyText(Key);
while(end == 0)
{
if (keypressed.equals ("Left"))
{
turtlex-=50;
}
if (keypressed.equals ("Up"))
{
turtlex+=50;
}
if (keypressed.equals("Right"))
{
turtlex+=50;
}
if (keypressed.equals("Down"))
{
turtley-=50;
}
}
}
public void keyTyped(KeyEvent event){}
public void keyReleased (KeyEvent event){}
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
KeyHandler move = new KeyHandler();
addKeyListener(move);

g.setColor(new Color(210, 180, 140));
g.fillRect(0, 0, 500, 500);
g.setColor(new Color(135, 206, 235));
g.fillRect(350, 0, 150, 50);
g.fillRect(400, 50, 100, 100);
g.setColor(new Color(0, 170, 0));
Turtle.draw(g2);
g.setColor(Color.black);
g.fillRect(150, 0, 200, 50);
g.drawLine(50, 500, 50, 450);
g.drawLine(50, 450, 200, 450);
g.drawLine(200, 450, 200, 400);
g.drawLine(200, 400, 250, 400);
g.drawLine(250, 400, 250, 450);
g.drawLine(250, 450, 300, 450);
g.drawLine(300, 450, 300, 300);
g.drawLine(300, 300, 400, 300);
g.drawLine(400, 300, 400, 400);
g.drawLine(400, 400, 350, 400);
g.drawLine(350, 400, 350, 350);
g.drawLine(350, 450, 450, 450);
g.drawLine(450, 450, 450, 250);
g.drawLine(450, 250, 300, 250);
g.drawLine(300, 250, 300, 200);
g.drawLine(300, 200, 200, 200);
g.drawLine(200, 200, 200, 100);
g.drawLine(200, 100, 300, 100);
g.drawLine(300, 100, 300, 150);
g.drawLine(300, 150, 350, 150);
g.drawLine(350, 150, 350, 200);
g.drawLine(350, 200, 500, 200);
g.drawLine(500, 200, 500, 0);
g.drawLine(50, 400, 150, 400);
g.drawLine(50, 400, 50, 300);
g.drawLine(50, 300, 200, 300);
g.drawLine(100, 350, 100, 250);
g.drawLine(100, 250, 250, 250);
g.drawLine(250, 250, 250, 350);
g.drawLine(250, 350, 150, 350);
g.drawLine(150, 250, 150, 0);
g.drawLine(50, 50, 50, 250);
g.drawLine(50, 50, 100, 50);
g.drawLine(50, 150, 100, 150);
g.drawLine(50, 200, 150, 200);
g.drawLine(150, 50, 400, 50);
g.drawLine(400, 50, 400, 100);
g.drawLine(400, 100, 350, 100);
g.drawLine(350, 100, 350, 0);
g.drawLine(400, 100, 400, 150);
g.drawLine(400, 150, 450, 150);

g.setColor(new Color(248, 248, 255));
g.drawString("Move: w-up, a-left, d-right, s-down", 158, 30);
g.drawString("Collect all eggs & reach the ocean", 155, 15);
g.drawString("GOOD LUCK!", 215, 45);
g.fillOval(220, 120, 10, 10);
g.fillOval(70, 170, 10, 10);
g.fillOval(120, 220, 10, 10);
g.fillOval(120, 270, 10, 10);
g.fillOval(70, 320, 10, 10);
g.fillOval(370, 370, 10, 10);
g.fillOval(220, 420, 10, 10);
g.fillOval(270, 420, 10, 10);
g.fillOval(70, 470, 10, 10);
g.fillOval(470, 470, 10, 10);

while(end == 0)
{
if (keypressed.equals ("Left"))
{
g.setColor(new Color(210, 180, 140));
g.fillRect(turtlex-10, turtley-25, 50, 50);
g.setColor(new Color(0, 170, 0));
Turtle.draw(g2);
}
if (keypressed.equals ("Up"))
{
g.setColor(new Color(210, 180, 140));
g.fillRect(turtlex-10, turtley-25, 50, 50);
g.setColor(new Color(0, 170, 0));
Turtle.draw(g2);
}
if (keypressed.equals("Right"))
{
g.setColor(new Color(210, 180, 140));
g.fillRect(turtlex-10, turtley-25, 50, 50);
g.setColor(new Color(0, 170, 0));
Turtle.draw(g2);
}
if (keypressed.equals("Down"))
{
g.setColor(new Color(210, 180, 140));
g.fillRect(turtlex-10, turtley-25, 50, 50);
g.setColor(new Color(0, 170, 0));
Turtle.draw(g2);
}
}
if(turtlex == 460 && turtley == 125)
{
if(egg==10)
{
g.setColor(Color.black);
g.fillRect(150, 0, 200, 50);
g.setColor(new Color(248, 248, 255));
g.drawString("You win!", 235, 240);
end++;
}
else
{
g.setColor(Color.black);
g.fillRect(150, 0, 200, 50);
g.setColor(new Color(248, 248, 255));
g.drawString("You still need " + (10 - egg) + "egg.", 235, 240);
}
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

Puhatch,

Your code should not add the keylistener in the paint() method. You should only add the keylistener once, in the constructor of the JApplet, or in the init() method.

and instead of taking the keyevent keycode and getting a text label just test the keycode using the constants VK_KP_DOWN, VK_KP_UP, VK_KP_LEFT, VK_KP_RIGHT. Then save this int and test in your paint methods. In the KkeyPress() of KeyListener you have a while(end==0), and end is always == 0, so this is an infinite loop. You can remove that while, as the system will send you a message when the key is pressed, you don't have to poll for it.

In another post you mentioned this was for a mobile device, however this is applet code which will not work in JavaME. Your code is simple enough that most of it could be ported to a JavaME platform, however you would want to write this JApplet as a GameCanvas, being held in a MIDlet to port it.

One suggestion for your code to make it smaller and easier to understand, put your location data into an int[][], then use a for() loop to process your draw methods. Something like.

int[][] points = {{50, 500, 50, 450},{50, 450, 200, 450},{200, 450, 200, 400},{00, 400, 250, 400},....};
:
:
for(int i=0; i g.drawLine(points[i][0],points[i][1],points[i][2],points[i][3]);

Regards,
-Shawn