• Advertisement
Sign in to follow this  

[Java] Draw something more than once?

This topic is 1926 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I'm a beginner to the LWJGL framework, and cannot for the life of me figure this out. I am recreating the space invader game. I have the ship on the screen and moving, but I can't draw the bullet so that you can have more than 1 on the screen. Right now it just draws and goes forward when you push space, but stops drawing when you let go of space. And then when you hit space again, it draws from where it left off. Here is the code

[code]
import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Move {
private static Texture texture;

private static Texture bull;

public static int mx = 200;

public static int my = 400;

public static int bvelx = SetShip.x;

public static int bvely = SetShip.y;

public static void main(String[] args){

setUpDisplay(640,480);
SetShip.init();
ShootBullet.init();

while (true){
glClear(GL_COLOR_BUFFER_BIT);
SetShip.draw();
logic();

Display.update();
Display.sync(60);

if(Display.isCloseRequested()){
Display.destroy();
System.exit(0);
}
}
}
private static void logic() {
// TODO Auto-generated method stub

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
mx = -5;
SetShip.update(mx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
mx = 5;
SetShip.update(mx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
bvely = -15;
ShootBullet.update(bvely);
ShootBullet.draw();
}

}

private static void setUpDisplay(int width, int height) {
// TODO Auto-generated method stub
try{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("Space Invaders");
Display.create();
} catch (LWJGLException e){
e.printStackTrace();
}

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

}

private static class ShootBullet {
private static int bulletx = SetShip.x, bullety = SetShip.y;

static void init(){
try{
bull = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/bullet.png")));
} catch (IOException e){
e.printStackTrace();
}
}

static void update(int by){
bullety += by;
}

static void draw(){
bull.bind();

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(bulletx, bullety);
glTexCoord2f(1,0);
glVertex2f(bulletx +50, bullety);
glTexCoord2f(1,1);
glVertex2f(bulletx +50, bullety+50);
glTexCoord2f(0,1);
glVertex2f(bulletx, bullety + 50);
glEnd();
}

}

private static class SetShip{

public static int x = 200, y = 400;

static void init() {
// TODO Auto-generated method stub
try{
texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/spaceship.png")));
} catch (IOException e){
e.printStackTrace();
}

}

static void update(int mx){
x += mx;
}

static void draw() {
// TODO Auto-generated method stub
texture.bind();



glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(x, y);
glTexCoord2f(1,0);
glVertex2f(x +200, y);
glTexCoord2f(1,1);
glVertex2f(x +200, y+100);
glTexCoord2f(0,1);
glVertex2f(x, y + 100);
glEnd();

}
}
}
[/code]

And the images used

(ship)
http://i.imgur.com/5ZsbS.png

(bullet)
http://i.imgur.com/w5qIM.png

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
It's because you only have it set to update when space is held. From my knowledge you would want to have a state for the bullet being checked to see if it's still alive. If it's still alive update its position, if not remove it. I think that's right.

Share this post


Link to post
Share on other sites
There's a couple issues here. If your bullet is its own class, with its own update method, you need to call that method every tick on every bullet, not just when the space bar key is pressed. One way to achieve this would be to maintain an array of all bullets currently created. Then iterate through that array and call each bullet's update method every tick. This also means you should set the velocity as a property of the bullet object.

Share this post


Link to post
Share on other sites
a few pointers to get you on the right track:

1) You are abusing static. (The only thing in your code that should be static is main, the rest shouldn't be static imo) (You don't have a bullet object, just a single global bulletx,bullety variable pair)
a static class can not be instantiated and its public members are effectivly globals. (Make a non static bullet class, create an array of bullets(in main) and add a new bullet to that array when you fire, update and draw all bullets in that array in the normal game loop.

2) You shouldn't load the image file when you shoot the bullet (loading images is slow so load them before the game starts)

Share this post


Link to post
Share on other sites
I've taken a look at your code and seems that it needs bit reorganizing. Games have a main loop that consist of few functions like input (could be user or/and network), logic, and draw.Each of them should handle specifically what it says. Meaning that in the input function you would only do things that have to do with input, logic would only handle the logic that needs to be run each frame and draw should only be drawing the game object on to your canvas.

What you have done is putting the draw function of the bullet inside your (logic/input) function which not the way to go. Move the draw function of the bullet to where you draw the ship. I suppose you did this with the idea that the bullet would only be displayed when you would press a key.

My advice to you is try to improve your OO by moving the ship and the bullet to a separate class file and like SimonForsman said don't use static you have noneed for this at this point.

Share this post


Link to post
Share on other sites
[quote name='DARSC0D3' timestamp='1349343094' post='4986701']
and like SimonForsman said don't use static you have noneed for this at this point.
[/quote]

Thanks for your help! And I only used static because eclipse was yelling at me, and it wouldn't work unless I put a static behind everything

Share this post


Link to post
Share on other sites
[quote name='LeisureForce' timestamp='1349358627' post='4986764']
[quote name='DARSC0D3' timestamp='1349343094' post='4986701']
and like SimonForsman said don't use static you have noneed for this at this point.
[/quote]

Thanks for your help! And I only used static because eclipse was yelling at me, and it wouldn't work unless I put a static behind everything
[/quote]

Thats usually because you didn't instantiate the class before trying to use it.

If you have a non static class named Bullet you use it by doing:

Bullet myBullet = new Bullet(ship.x,ship.y,ship.direction); //create a new bullet instance at the ships position heading in the direction the ship is facing.

you can create as many bullets as you want this way and each bullet will get its own instance of the member variables you've defined in the Bullet class, you can also store the bullets in a Vector or List instead of a variable(which helps if you want lots of them). ( Vector<Bullet> bullets = new Vector<Bullet>();
bullets.push_back(new Bullet(ship.x,ship.y,ship.direction); //create a bullet and add it to the bullets vector.

myBullet.update(); //update the bullet instance we just created //this method should move the bullet.
myBullet.draw(); //draw the bullet we just created (normally you don't want the bullet to render itself but in this case its a bit overkill to create a proper renderer)
or if you use a vector:
for (Bullet b : bullets) {
b.update();
b.draw();
} //updates and draws all bullets
Just remove the bullets from the vector when you want to destroy them, with vectors its fastest to remove from the back, with Lists it doesn't really matter that much. (You can swap the bullet you want to remove with the last bullet in the vector before removing to speed things up if necessary)

static classes are not objects, they're effectivly just a collection of global functions and variables, (in languages such as C++ you use namespaces for this instead) and in Java they are only really useful to group support functions in a logical way.

Share this post


Link to post
Share on other sites
Okay, now i've taken all of that into consideration, but I still have a few problems. Now, when I go to draw the bullet, it just turns the ship purple instead of drawing a rectangle to the screen. Here is my code. (I know that i am still drawing the rectangle only when i hit space, I'm just doing it for now).

[code]import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Move {
//make the screen width and height values public
public int SCREENWIDTH = 640, SCREENHEIGHT = 480;
private boolean isRunning = true;
private Texture spaceShip;
public int inx = 100;
public SetShip myShip = new SetShip();
public Bullet myBullet = new Bullet();
public int x = myShip.ShipXStart;
public int y = myShip.ShipYStart;

public Move(){
//Set up the display
try{
Display.setDisplayMode(new DisplayMode(SCREENWIDTH, SCREENHEIGHT));
Display.setTitle("Space Invaders");
Display.create();
} catch (LWJGLException e){
e.printStackTrace();
}





glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

myShip.init();

//main game loop
while (isRunning){
glClear(GL_COLOR_BUFFER_BIT);
myShip.draw();
input();


Display.update();
Display.sync(60);

if(Display.isCloseRequested()){
isRunning = false;
Display.destroy();
System.exit(0);
}
}
Display.destroy();


}

private void input() {
// TODO Auto-generated method stub
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)){
inx = -5;
myShip.update(inx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){
inx = 5;
myShip.update(inx);
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
myBullet.update(10, 20);
myBullet.draw();
}
}
private Texture loadTexture(String key) {
// TODO Auto-generated method stub
try{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("img/" + key + ".png")));
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}

public class SetShip{
public int ShipXStart = 100, ShipYStart = 400;
//initialize the ship
void init(){
//store the image in a texture
//load the image
try {

spaceShip = TextureLoader.getTexture("PNG", new FileInputStream(new File("img/spaceship.png")));
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}



}


void update(int inx){
ShipXStart += inx;
}

void draw(){


spaceShip.bind();

glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(ShipXStart, ShipYStart);
glTexCoord2f(1, 0);
glVertex2f(ShipXStart + 200, ShipYStart);
glTexCoord2f(1, 1);
glVertex2f(ShipXStart + 200, ShipYStart + 100);
glTexCoord2f(0, 1);
glVertex2f(ShipXStart, ShipYStart + 100);
glEnd();
}


}
private static class Bullet{
//set up the variables
public int x, y; //set the starting x and y for the bullets

void update(int dx, int dy){
x += dx;
y += dy;
}

void draw(){
glColor3f(1.0f,0f,1.0f);
glBegin(GL_QUADS);
glVertex2i(50, 50);
glVertex2i(100, 100);
glVertex2i(50, 100);
glVertex2i(100, 100);
glEnd();
}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Move();
}

}[/code]

Share this post


Link to post
Share on other sites
[quote name='rockstar8577' timestamp='1349404208' post='4986996']
Maybe a picture to see exactly what's wrong?
[/quote]

Sorry.

Before: [url="http://i.imgur.com/tsy2M.png"]http://i.imgur.com/tsy2M.png[/url]

After: http://i.imgur.com/DflSe.png Edited by LeisureForce

Share this post


Link to post
Share on other sites
Okay i could totally be wrong on this, but isn't the color globally used? So once you change it to purple for the bullet it changes it when you draw the ship? It is totally possible that i am wrong on this.

Share this post


Link to post
Share on other sites
[quote name='rockstar8577' timestamp='1349413078' post='4987008']
Okay i could totally be wrong on this, but isn't the color globally used? So once you change it to purple for the bullet it changes it when you draw the ship? It is totally possible that i am wrong on this.
[/quote]

That is correct, within the context.
adding a glColor3f(1.0f,1.0f,1.0f); to the top of the render ship function would solve this.


Also for the op,

don't pass dx,dy to the bullets update method(this will require you to track the direction of the bullets separatly which isn't a good idea), pass it to the constructor and add dx,dy as private member variables. (This way your bullet can keep track of its own direction)
to add a constructor that sets the direction of the bullet you add (in the Bullet class):

private float dx,dy;
public Bullet(float dx, float dy, etc) {
this.dx = dx;
this.dy = dy;
}

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1349419263' post='4987029']
private float dx,dy;
public Bullet(float dx, float dy, etc) {
this.dx = dx;
this.dy = dy;
}
[/quote]

Okay, so instead of calling update when I hit space, I would do Bullet(dx, dy)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement