Computer Science Canada

Using Arrow Keys to move an image/object in Java Ready to Program

Author:  ShdwVentus [ Wed Jan 11, 2017 10:53 pm ]
Post subject:  Using Arrow Keys to move an image/object in Java Ready to Program

Hi everyone.
As you may be able to tell, I am fairly new to coding and just started this semester. I have to make a Donkey Kong game for my culminating project and I am not 100% on how to do keyboard movements. I want to make Mario move around and jump and I found some code that will compile, it just wont work. Some of my friends suggested that using the mouse would make the development easier, but I think in terms of user game play, the keyboard would be more enjoyable. I am currently using Java Ready to Program, yet I find it very hard to find information that actually works with RTP. For some reason, the program will only show the barrels moving (in separate code) and not the pink rectangle in place of Mario (since I have the faintest idea of how to import images).

Below is some of the code for the movement process that I currently have:


import java.io.*; // Imports input / output commands.
import java.awt.*;
import java.util.*;
import java.awt.event.*; // Imports events for buttons.
import java.applet.Applet; // Imports applet functionality.
import java.awt.Image; // Imports the functionality for using jpeg images in the game.
import javax.swing.*; // Imports the swing functionality for the program.

import java.awt.event.KeyEvent.*;

class Donkey_Kong extends Applet

static int changeX; // The change in the character's x position.
static int changeY; // The change in the character's y position.
static int charMoveX = 780; // The character's current x position.
static int charMoveY = 670; // The character's current y position.


// Purpose: To draw the character movement of mario.
public static void charMove (Graphics g)
{
g.setColor (Color.pink);
g.fillRect (charMoveX, charMoveY, 50, 20);

charMoveX = charMoveX + changeX;
charMoveY = charMoveY + changeY;
} // End of charMove.

public void keyPressed (KeyEvent e)
{

int button = e.getKeyCode ();

if (button == KeyEvent.VK_SPACE)
{

}

if (button == KeyEvent.VK_V)
{
changeX = 6;
}

if (button == KeyEvent.VK_LEFT)
{
changeX = -10;
}

if (button == KeyEvent.VK_RIGHT)
{
changeX = 10;
}

if (button == KeyEvent.VK_UP)
{
changeY = -10;
}

if (button == KeyEvent.VK_DOWN)
{

}
} // End of keyPressed.


public void keyReleased (KeyEvent e)
{
int key = e.getKeyCode ();

if (key == KeyEvent.VK_LEFT)
{
changeX = 0;
}
if (key == KeyEvent.VK_SPACE)
{

}

if (key == KeyEvent.VK_RIGHT)
{
changeX = 0;
}

if (key == KeyEvent.VK_UP)
{
changeY = 0;
}

if (key == KeyEvent.VK_DOWN)
{
changeY = 0;
}
} // End of keyReleased.

} // End of Class.




Thank you very much for your time and help in solving this problem.

Author:  Gibbs [ Tue Jan 17, 2017 8:08 pm ]
Post subject:  RE:Using Arrow Keys to move an image/object in Java Ready to Program

Two things to keep in mind. You will want to make sure that each key is being read when you think it is. You can do this by doing some troubleshooting prints to console. E.g. System.out.println("RIGHT PRESSED").

As for moving the character, you have to make sure that you are actually trying to re-draw it. By the looks of it, charMove(graphics g) is not called whenever you press a key, so the program doesn't actually to redraw the square. I'm assuming that the barrels are handled in a separate class. Also make sure to think about the order of which the commands are made. In the charMove method, you change the charMoveX and Y variables AFTER you draw the rectangle. It would be more intuitive to change charMoveX and Y, and then draw the rectangle again, so a user's inputs happen and effect the square without having to make further inputs.


: