java - visual error with adding bullets from a list with lwjgl and openGL with a for loop -
so i'm learning opengl lwjgl , everything's fine until add way shoot. upon pressing space, bullet created , added list of bullets. when go render it, call each bullet's x , y using loop, render using coords, , update of bullets. except when this, player (a green box) gets flashy , doesn't move (visually) , bullets never disappear screen , instead move downward leaving "ghost trail". can, however, move player without moving green box, because when move him , right little can create new ghost trail of bullets , right second.
bullet.java:
package game; import java.util.arraylist; import java.util.list; public class bullet { public int speed; public int x; public int y; public char direction; int origin; public static final int player_origin = 0; public static final int monster_origin = 1; static list<bullet> bullets = new arraylist<bullet>(); public static void dotick(){ for(int = 0; < bullets.size(); i++){ //moves bullets based on speed , direction if(bullets.get(i).direction == 'w'){ bullets.get(i).y -= bullets.get(i).speed; } else if(bullets.get(i).direction == 'a'){ bullets.get(i).x -= bullets.get(i).speed; } else if(bullets.get(i).direction == 's'){ bullets.get(i).y += bullets.get(i).speed; } else if(bullets.get(i).direction == 'd'){ bullets.get(i).x += bullets.get(i).speed; } } } public bullet(int x, int y, char newdirection, int neworigin){ direction = newdirection; speed = 6; if(neworigin == player_origin){ if(direction == 'w' ){ x= x + 16; y= y; } else if (direction == 'd'){ x= x + 32; y= y + 16; } else if (direction == 'a'){ x= x; y= y + 16; } else if(direction == 's'){ x= x + 16; y= y + 32; } } } }
game.java:
package game; import javax.swing.*; import java.util.arraylist; import java.awt.*; import java.awt.event.actionevent; import javax.swing.timer; import java.awt.event.*; import java.util.list; import org.lwjgl.lwjglexception; import org.lwjgl.input.keyboard; import org.lwjgl.opengl.display; import org.lwjgl.opengl.displaymode; import static org.lwjgl.opengl.gl11.*; @suppresswarnings({ "serial" }) public class game{ static player player = new player(10,10); timer timer; boolean wp; boolean ap; boolean sp; boolean dp; boolean spp; int xp; int yp; public static void main(string[] args){ game game = new game(); game.start(); } private void start(){ //initialize display try { display.setdisplaymode(new displaymode(800,600)); display.create(); } catch (lwjglexception e) { e.printstacktrace(); } //initialize opengl glmatrixmode(gl_projection); glloadidentity(); glortho(0,display.getwidth(),0,display.getheight(), -1, 1); glmatrixmode(gl_modelview); glclearcolor(0,0,0,1); gldisable(gl_depth_test); //no 3d //game loop while(!display.iscloserequested()){ glclear(gl_color_buffer_bit); glcolor3f(0.25f,0.75f,0.5f); glbegin(gl_quads); { glvertex2f(xp, yp); glvertex2f(xp, 64 + yp); glvertex2f(64 + xp, 64 + yp); glvertex2f(64 + xp, yp); } glend(); for(int = 0; < bullet.bullets.size(); i++){ glbegin(gl_quads); glvertex2f(bullet.bullets.get(i).x, bullet.bullets.get(i).y); glvertex2f(bullet.bullets.get(i).x, 4 + bullet.bullets.get(i).y); glvertex2f(4 + bullet.bullets.get(i).x, bullet.bullets.get(i).y); glvertex2f(4 + bullet.bullets.get(i).x, 4 + bullet.bullets.get(i).y); } display.update(); if(keyboard.iskeydown(keyboard.key_w)){ yp += 10; } if(keyboard.iskeydown(keyboard.key_a)){ xp -= 10; } if(keyboard.iskeydown(keyboard.key_s)){ yp -= 10; } if(keyboard.iskeydown(keyboard.key_d)){ xp += 10; } if(keyboard.iskeydown(keyboard.key_space)){ bullet tempbullet = new bullet(xp+32, yp+32, 'w', bullet.player_origin); //creates temporary bullet based on player bullet.bullets.add(tempbullet); //adds bullet static list of bullets in bullet.java } bullet.dotick(); } display.destroy(); } }
a glend();
after drawing each bullet might help.
also, unrelated, can put glbegin
/glend
on outside of loop.
Comments
Post a Comment