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
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