Walking around world problem
Alright I'm trying to make a simple game, as it's my first game that not a simple puzzle game. Right now I'm attempting to make the player just walk around on screen. However for some reason it keeps allowing the player to move off screen. I can't figure out why. Any help would be aprreciated.
Driver:
import javax.swing.JFrame;
publicclass Direction
{
publicstaticvoid main(String[] args)
{
JFrame frame=new JFrame("Direction");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new DirectionPanel());
frame.pack();
frame.setVisible(true);
}
}
Program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass DirectionPanelextends JPanel
{
privatefinalint WIDTH=600, HEIGHT=500;
privatefinalint JUMP=20;
privatefinalint IMAGE_SIZE=20;
private ImageIcon up, down, right, left, currentImage;
privateint x, y, moveX, moveY;
public DirectionPanel()
{
addKeyListener (new DirectionListener());
x=40;
y=40;
moveX=moveY=40;
up=new ImageIcon("walkingUp.gif");
down=new ImageIcon("walkingDown.gif");
left=new ImageIcon("walkingLeft.gif");
right=new ImageIcon("walkingRight.gif");
currentImage= right;
setBackground (Color.orange);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
publicvoid paintComponent(Graphics page)
{
super.paintComponent(page);
currentImage.paintIcon (this, page, x, y);
}
privateclass DirectionListenerimplements KeyListener
{
publicvoid keyPressed(KeyEvent event)
{
switch(event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage=up;
moveY-=JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage=down;
moveY+=JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage=left;
moveX-=JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage=right;
moveX+=JUMP;
break;
}
if(moveY!=20||moveY!=HEIGHT-20||moveX!=20||moveX!=WIDTH-20)
{
y=moveY;
x=moveX;
}
repaint();
}
publicvoid keyTyped(KeyEvent event){}
publicvoid keyReleased(KeyEvent event){}
}
}

