Java swing rendering phenomenon in 2D Game -
i working on first jump'n run game. initial part working decent, still "bug" appears when "colums" moving. happens when "move_speed" 1<. tryed timer too, same thing.
this how looks like:
please have @ code:
... public class board implements runnable, keylistener { jframe frame; arraylist<column> clist; player p; private boolean ingame = false; public final static int init_width = 600; public final static int init_height = 400; public static int width; public static int height; private final int move_speed = 10; //when 1 problem doesnt appear! private final int gravity = -3; private int cplayerstayingon; private double playerbottom; private double floortop; private boolean mr = false; private boolean ml = false; private boolean jump = false; long lastlooptime = system.nanotime(); final int target_fps = 100; final long optimal_time = 1000000000 / target_fps; private int fps; private int lastfpstime; public static double delta = 1; public board() { initboard(); initplayer(); initcolumns(); } private void initboard() { frame = new jframe("jump'nrun"); frame.setsize(init_width, init_height); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(null); frame.addkeylistener(this); clist = new arraylist<column>(); frame.setvisible(true); board.width = frame.getcontentpane().getwidth(); board.height = frame.getcontentpane().getheight(); } private void initcolumns() { (int = 0; < 8; i++) { clist.add(new column(i + 1, true)); frame.add(clist.get(i)); } } private void initplayer() { p = new player(); frame.add(p); } private void movecolums() { (column col : clist) { col.setlocation((int) (col.getlocation().getx() - move_speed), 0); } } private int playerstanding(double px) { (int = 0; < 8; i++) { if (clist.get(i).getx() <= px && (clist.get(i).getx() + clist.get(i).getwidth()) >= px) { return i; } } return -1; } private void moveplayer() { // gravity if (playerbottom < floortop) { p.setlocation((int) p.getlocation().getx(), (int) p.getlocation() .gety() - gravity); } if (mr) { p.moveright(); } if (ml) { p.moveleft(); } if (jump) { p.jump(); jump = false; } } private void collectdata() { this.cplayerstayingon = playerstanding(p.getbounds().getx() + p.getbounds().getwidth()); this.playerbottom = p.getbounds().getmaxy(); this.floortop = clist.get(cplayerstayingon).floor.gety(); } private void recyclecolums() { if (clist.get(0).getx() + clist.get(0).getwidth() <= 0) { clist.remove(0); clist.add(7, new column(7 + 1, false)); frame.add(clist.get(7)); } } @override public void keytyped(keyevent e) { // todo auto-generated method stub } @override public void keypressed(keyevent e) { if (e.getkeycode() == keyevent.vk_space || e.getkeycode() == keyevent.vk_w || e.getkeycode() == keyevent.vk_up) { if (playerbottom >= floortop) { jump = true; } } if (e.getkeycode() == keyevent.vk_d || e.getkeycode() == keyevent.vk_right) { mr = true; } if (e.getkeycode() == keyevent.vk_a || e.getkeycode() == keyevent.vk_left) { ml = true; } } @override public void keyreleased(keyevent e) { if (e.getkeycode() == keyevent.vk_d || e.getkeycode() == keyevent.vk_right) { mr = false; } if (e.getkeycode() == keyevent.vk_a || e.getkeycode() == keyevent.vk_left) { ml = false; } } private int getfps() { return fps; } private void setfps(int fps) { this.fps = fps; } @override public void run() { while (true) { if (!ingame) { ingame = true; } else { long = system.nanotime(); long updatelength = - lastlooptime; lastlooptime = now; delta = updatelength / ((double) optimal_time); lastfpstime += updatelength; setfps(getfps() + 1); if (lastfpstime >= 1000000000) { lastfpstime = 0; setfps(0); } recyclecolums(); collectdata(); movecolums(); moveplayer(); try { thread.sleep((lastlooptime - system.nanotime() + optimal_time) / 1000000); } catch (exception e) { } } } } }
and column class:
... public class column extends jpanel { jpanel floor; jpanel grass; random rand = new random(); private static final long serialversionuid = 1l; public final static int width = board.width / 6; public final int height = board.height; private int position; public final static int floor_h = width; public column(int pos, boolean init) { this.position = pos; setbounds((width * position) - width, 0, width, height); setlayout(null); setbackground(color.white); floor = new jpanel(); floor.setlayout(null); floor.setbackground(color.black); if (init) { floor.setbounds(0, height - floor_h, width, floor_h); } else { floor.setbounds(0, (height - (floor_h + (floor_h * rand.nextint(2) / 2))), width, floor_h * 2); } grass = new jpanel(); grass.setbounds(0, 0, width, 10); grass.setbackground(color.green); floor.add(grass); add(floor); } }
any hints appreciatet!
(sorry bad english)
i fixed changing following:
private void recyclecolums() { if (clist.get(0).getx() + clist.get(0).getwidth() <= 0) { clist.remove(0); clist.add(7, new column(7 + 1, false, clist.get(6).getx())); frame.add(clist.get(7)); } }
...
public column(int pos, boolean init, int lastx) { this.position = pos; setlayout(null); setbackground(color.white); floor = new jpanel(); floor.setlayout(null); floor.setbackground(color.black); if (init) { setbounds((width * position) - width, 0, width, height); floor.setbounds(0, height - floor_h, width, floor_h); } else { setbounds(lastx + width, 0, width, height); floor.setbounds(0, (height - (floor_h + (floor_h * rand.nextint(2) / 2))), width, floor_h * 2); }
Comments
Post a Comment