Hi, Can i ask some help,this is the first time that i make my simple game…it’s like the spaceship flying i am just using png file for this,I already know how to move my spaceship using keybindings, my problem is that i don’t have idea how to draw simple bullet when i pressed the forward key in the keyboard,and also i don’t know the logic how the bullet will go forward when fired up…I apologize for this silly question but i am just beginner in developing simple game…I hope someone will help me to achieve this…
I really appreciated those will help, Thank you in advance.
Hello
I’m quite a beginner myself so the way I show you might not be perfect.
You should make a Bullet class that holds the bullet information, like speed, position,image etc. and then in the player/spaceship you have a list of those bullets (List bulList) that you will add a new bullet to every time you fire using the bulList.add(new Bullet(param)). Now if I were you I’d make the class something like this:
class Bullet{
final double SPEED = 2;
double x,y;
Bullet(int x, int y){
this.x = x;
this.y = y;
}
public void move(){
//bullet goes up
y -= SPEED;
}
}
Now that is assuming that you are making a game like space invaders where the bullet goes up. Also you will need to draw the elements in that list every frame. Hope that helps ;D
I think every character in the game as an object and they all derive from the [icode]GObject[/icode] class found here in my game engine. To make a bullet, I’ll make a [icode]Bullet[/icode] class like this.
public class Bullet extends GObject
{
public Bullet (float x, float y)
{
super (IMAGE_OF_BULLET, x, y);
setVelocityY(-4); // moves up
}
}
And in the space ship object, I check for input and if [icode]SPACE[/icode] is pressed, I’ll fire a bullet.
public void update (long elapsedTime)
{
if (GKeyBoard.isPressed(KeyEvent.VK_SPACE))
{
// Create a new bullet at the current position
Bullet b = new Bullet(getX(), getY());
// Add it to the map to display it and it get's the events
Map.addObject (b);
}
}
The function [icode]Map.addObject()[/icode] adds the bullet to the [icode]ArrayList[/icode] containing the objects and the map automatically does redrawing it every frame and checks for collisions between them.
If you like, please try my engine here. Also there’s a Space Invaders game tutorial here.
Okay but i have not yet draw my bullet, or how do i make my bullet color orange or red?just like my ship i draw in the paintComponent…can you please help me.I am confuse.
Read the links on game loops for a start. It’s been linked twice now.
Bad example:
class Game {
bulletsArray = new ArrayList <Bullet>();
public void gameLoop() {
while (true) {
update();
render();
// sleep
try {
Thread.sleep(1);
} catch() {}
}
}
public void update() {
for (Bullet b : bulletsArray) {
b.update();
}
}
public void render(Graphics g) {
// draw things
for (Bullet b : bulletsArray) {
b.draw(g);
}
}
}
class Bullet {
int x, y, xspeed, yspeed;
update() {
x += xspeed;
y += yspeed;
}
draw(Graphics g) {
g.drawImage(bulletImage, x, y, null);
}
}