I wanted to make a very simple game as a way to expand my knowledge of java and have found only a little success on my own.
Here's my current code:
import java.awt.event.*; // Use events
import java.awt.*; // Use graphics
import java.applet.*; // Use applets
import javax.swing.*; // Use swing stuff (GUI)
import java.util.*; // Use util
public class Tutorial extends JApplet
{
public class DemoPanel extends JPanel
{
//private int linePos = 0; // Line's X value
//private int lineDir = 1; // Line's direction
private int bx = 200; //Box x value
private int by = 200; //Box y value
private int bw = 50; //Box width
private int bh = 50; //Box height
public boolean key_right,key_left,key_up,key_down,key_z,key_x; //All this code is keyboard code.
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == e.VK_DOWN)
key_down = true;
if(e.getKeyCode() == e.VK_UP)
key_up = true;
if(e.getKeyCode() == e.VK_LEFT)
key_left = true;
if(e.getKeyCode() == e.VK_RIGHT)
key_right = true;
if(e.getKeyCode() == e.VK_Z)
key_z = true;
if(e.getKeyCode() == e.VK_X)
key_x = true;
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == e.VK_DOWN)
key_down = false;
if(e.getKeyCode() == e.VK_UP)
key_up = false;
if(e.getKeyCode() == e.VK_LEFT)
key_left = false;
if(e.getKeyCode() == e.VK_RIGHT)
key_right = false;
if(e.getKeyCode() == e.VK_Z)
key_z = false;
if(e.getKeyCode() == e.VK_X)
key_x = false;
//end of keyboard code
}
void DemoPanel()
{
// Does nothing here
}
public void paintComponent(Graphics page) //sort of the primary game thing? It paints the square and refreshes.
{
//From here to....
setFocusable(true); // Required for keyboard input
GameKey game_key = new GameKey(); //Variable needed to get key states
addKeyListener(game_key); // Add it to your game
/*if(game_key.key_z)
{
bx = bx + 10;
}*/
super.paintComponent(page);
page.setColor(Color.red); // Use the stock color, red
page.fillRect(bx, by, bw, bh);
if(game_key.key_z)
{
page.fillRect(50, 50, 50, 50);
}
long index = 1999999999;
while(index > -100000000)
{
index = index - 1;
}
repaint();
}
}
public void init()
{
setSize(500,500);
getContentPane().add(new DemoPanel()); // Add the video panel
}
}
I currently have several components that should, I think, allow me to make a very simple game.
My end goal is to create a red square which you move around a maze using the keyboard.
Making the maze, the applet, the square and the game state I've figured out.
What I'm having trouble with is binding the keys. What I want is this: After running the code, I'm using JGrasp as my compiler because it's the one I'm familiar with, the user clicks on the applet to focus on it then uses the mouse keys to move around the maze to the end.
I also haven't figured out how to make the program end or load a new maze after the user reaches the end...I'm stuck on that.
All that being said, my biggest question is how do I use this code:
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == e.VK_DOWN)
key_down = true;
if(e.getKeyCode() == e.VK_UP)
key_up = true;
if(e.getKeyCode() == e.VK_LEFT)
key_left = true;
if(e.getKeyCode() == e.VK_RIGHT)
key_right = true;
if(e.getKeyCode() == e.VK_Z)
key_z = true;
if(e.getKeyCode() == e.VK_X)
key_x = true;
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == e.VK_DOWN)
key_down = false;
if(e.getKeyCode() == e.VK_UP)
key_up = false;
if(e.getKeyCode() == e.VK_LEFT)
key_left = false;
if(e.getKeyCode() == e.VK_RIGHT)
key_right = false;
if(e.getKeyCode() == e.VK_Z)
key_z = false;
if(e.getKeyCode() == e.VK_X)
key_x = false;
}
to move the square around? I know that the keys should, depending on which key is pressed, change the value of the integers I have listed at the top..But how do I do that? I need to link the code with the graphics

New Topic/Question
Reply


MultiQuote


|