Help guys( Java pro only =P )

Java Cool Dude

New member
I'm working on a little arcade game, which can be found in a very early presentation at my website, and I really want to implement some keyboard keys to do the movements.
my current source code is


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class spaceShip extends Applet
{
private Image spaceShip;

public void init()
{
spaceShip = getImage(getDocumentBase(),"spaceShip.gif");
}//specifies the folder where the space ship is


static final int Height = 300;// the height of the game screen
static final int Width = 500;// the width of the game screen
private int sleep =50; // controls the frame rate
private int Missiles = 3; // number of attempts

private int oldScore =0;// initiates the score
int newScore;// the new value of score
int ScoreXPos = Width -40 , ScoreYPos =25;
//The X position of score , the Y position of the score
int remainMissiles = 3;// thge remaining missiles =P

private int speed = 5;
int newXCraft = 10, newYCraft =70;
int oldXCraft, oldYCraft;

public void update(Graphics gr)
{
paint(gr);
}

public void paint(Graphics gr)
{
setBackground(Color.black);//sets the background color
{
gr.drawImage(spaceShip,oldXCraft,oldYCraft,this);
gr.drawImage(spaceShip,newXCraft,newYCraft,this);
}//getting the spaceship

{
gr.setColor(Color.cyan);
gr.drawLine(ScoreXPos - 10,0 ,ScoreXPos - 10,Height);
gr.drawLine(ScoreXPos - 10,50 ,Width,50);
gr.drawLine(ScoreXPos - 10,100 ,Width,100);
gr.drawLine(0,0 ,Width,0);
gr.drawLine(0,Height ,Width,Height);
gr.drawLine(0,0 ,0,Height);
gr.drawLine(Width,0 ,Width,Height);
}// draws the boundries

{
gr.setColor(Color.blue);
gr.drawString("Score",ScoreXPos ,ScoreYPos);
}//positioning the string "score" and setting up the color

{
gr.drawString(" " + oldScore,ScoreXPos + 10 ,ScoreYPos +15);
gr.drawString(" " + newScore,ScoreXPos + 10 ,ScoreYPos +15);
}//positioning the score

{
gr.drawString("Missile",ScoreXPos ,ScoreYPos + 45);
}//positioning the string "Balls" and setting up the color

{
gr.drawString(" " + Missiles,ScoreXPos + 10 ,ScoreYPos +65);
gr.drawString(" " + remainMissiles,ScoreXPos + 10 ,ScoreYPos +65);
}//positioning the remaining lives

oldXCraft = newXCraft;
oldYCraft = newYCraft;

try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{}//controles the frame rate

repaint();
}

}


and I wish to implement this code



int move = evt.getKeyCode(); // which key was pressed

if (move == KeyEvent.VK_LEFT)
{
// Moves the spacecraft left
newXCraft -= speed;
}

if (move == KeyEvent.VK_RIGHT)
{
// Moves the spaceCraft right.
newXCraft += speed;
}
if (move == KeyEvent.VK_UP)
{
// Moves the spaceCraft up
newYCraft -=speed;
}

if (move == KeyEvent.VK_DOWN)
{
// Moves the spaceCraft down
newYCraft +=speed;
}

The thing is i'm a freshman and my java book ends up on how to create animation, so not a word about how to implement keyboard buttons to see some action.
My last project was rather easy since it didn't require any user entry, you can view it here.
So if you guys help me out, i would apreciate .
Thx for all of you

PS: It's my own personal project =P
 
First, your class needs to implement KeyListener:
public class spaceShip extends Applet implements KeyListener

Then, you would have a function within your class that looks like this:
Code:
public void keyPressed(KeyEvent e) {

      if (e.getKeyCode() == KeyEvent.VK_UP) {
         //do something
      }

      //etc

}

That should be it. I hope I'm not forgetting anything...
 
D'oh! I was forgetting something. You also need to implement the "public void keyReleased(KeyEvent e)" and "public void keyTyped(KeyEvent e)" functions. They can just be left blank if you don't need them.
 
Carmack, wait till I graduate you'll be my b***

Carmack, wait till I graduate you'll be my b***

Thx man I appreciate it a lot.
Now I can remain in the same boat as my friend Josh.
We're actually working on two similar arcade games: He chose the space as the battlefield, I chose earth =P
Here's the beta of my game
Use the arrows to move the space ship, I didn't implement the fire stuff and the aliens :p yet. Hopefully this weekend :D

BTW here's a part of the source code, anyone interested in having the whole source lemmie know.



public void keyPressed(KeyEvent evt)
{

int move = evt.getKeyCode(); // which key was pressed
if (move == KeyEvent.VK_LEFT && newXCraft >=5 )
{
// Moves the spacecraft left
speedX = -5;
speedY = 0;
repaint();
}

if (move == KeyEvent.VK_RIGHT && newXCraft < ScoreXPos - 100)
{
// Moves the spaceCraft right.
speedX = 5;
speedY = 0;
repaint();
}
if (move == KeyEvent.VK_UP && newYCraft >=5)
{
// Moves the spaceCraft up
speedX = 0;
speedY = -5;
repaint();
}

ee

if (move == KeyEvent.VK_DOWN && newYCraft <= Height - 45)
{
// Moves the spaceCraft right.
speedX = 0;
speedY = 5;
repaint();
}

} // end keyPressed()

public void focusGained(FocusEvent evt)
{
}

public void focusLost(FocusEvent evt)
{
}

public void keyTyped(KeyEvent evt)
{
}

public void keyReleased(KeyEvent evt)
{

}

public void mousePressed(MouseEvent evt)
{
requestFocus();
}

public void mouseEntered(MouseEvent evt) { } // Required by the
public void mouseExited(MouseEvent evt) { } // MouseListener
public void mouseReleased(MouseEvent evt) { } // interface.
public void mouseClicked(MouseEvent evt) { }
 
Not bad. Pretty good start there. You're well on your way. I look forward to seeing the finished product.
 
Double buffering rules my friends, as it killed the jerky flickering my little game used to suffer from.
Now I implemented the pause Key as well as the space bar for missiles =p
It's gonna take me sometime to create the first alien spaceship, so you guys can shoot something else than nvidiots :p
Woot woot thanks Jason for encouraging me =P " You and Zardon "
 
Last edited:
wooooooooooooooot
Now my source codes are thourougly described, if any one wants them, lemmie know :D
A sample from the source code about the wall collisions:
for(int i =0; i< numberOfWalls ; i++)
{
{
if(color == "red")
bufferg.setColor(Color.red);
if(color == "blue")
bufferg.setColor(Color.blue);
if(color == "yellow")
bufferg.setColor(Color.yellow);
if(color == "gray")
bufferg.setColor(Color.gray);
if(color == "lightGray")
bufferg.setColor(Color.lightGray);
if(color == "green")
bufferg.setColor(Color.green);
if(color == "magenta")
bufferg.setColor(Color.magenta);
if(color == "orange")
bufferg.setColor(Color.orange);
if(color == "cyan")
bufferg.setColor(Color.cyan);
}// Sets the color for the Walls

if( real != false)
{
bufferg.fillRect(x,y, a ,b );// Fills the walls
}
}// Drawing the visible walls

for(int i =0; i< numberOfWalls ; i++)
{
if( real != false)
{
if(newYMan == y-25 && newXMan > x-25 && newXMan < x + a)
{
speedY =0;
goDown = false;
}// Top collisions

if( newYMan == b+ y && newXMan > x-25 && newXMan < x + a)
{
speedY =0;
goUp = false;
}// Down collisions

if(newXMan == x-25 && newYMan > y-25 && newYMan < y + b )
{
goRight = false;
speedX =0;
}// Left collisions

if( newXMan == x + a && newYMan > y-25 && newYMan < b+ y )
{
goLeft = false;
speedX =0;
}// Right collisions

}// Sets the collisions for visible walls
}//Checks the collisions for the walls
 
Wind is blowing along the empty streets of these forums. Old wooden doors scream in rusty pain as they get pushed by the invisble force. Broken windows make sad dance as I contemplate this part of the Rage3D ocean turning into a dead salty lake:(
 
Oh, I see. I was just too stupid to figure out the controls. I was expecting the mouse to do something.
 
Back
Top