java - Simple Applet Game Bug -
i'm having trouble applet. it's purpose create game can move block @ bottom of screen fit through holes in blocks falling down above. i've implemented block can't block move when press key. here source code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class mathewborumgame extends japplet { public void init() { dimension dim = getsize(); mathewborumgamepanel avoidance = new mathewborumgamepanel(dim); add(avoidance); thread game = new thread(avoidance); game.start(); } }// end of extended japplet class class mathewborumgamepanel extends jpanel implements runnable{ protected int x = 225; protected int y = 450; private int speed = 0; private boolean isplaying = false; private boolean hashit = false; private string instructions = "press 'enter' start survive long as" + " possible."; private string keyinstructions = "use arrow keys move."; private string test; private dimension dim = null; private thread game = null; private timer timer = new timer(1000, new timerlistener()); public mathewborumgamepanel(dimension paramdimension) { this.dim = paramdimension; setbackground(new color(255, 255, 204)); setforeground(color.black); addkeylistener(new keyadapter() { public void keypressed(keyevent e) { switch (e.getkeycode()) { case keyevent.vk_left: if(x >= 10) x = x - 10; repaint(); break; case keyevent.vk_right: if(x <= 490) x = x + 10; repaint(); break; default: repaint(); break; } } }); } public void run() { (;;) { repaint(); } } public void paintcomponent(graphics paramgraphics) { super.paintcomponent(paramgraphics); paramgraphics.fillrect(x, y, 50, 15); paramgraphics.setfont(new font("serif", font.bold, 15)); paramgraphics.drawstring(this.instructions, 30, 30); paramgraphics.drawstring(this.keyinstructions, 30, 50); test = test.valueof(x); paramgraphics.drawstring(test, 30, 70); } private class timerlistener implements actionlistener { public void actionperformed(actionevent e) { repaint(); } } }//end of extended jpanel class
thanks
see comments in code further tips..
import java.awt.*; import java.awt.event.*; import javax.swing.*; // <applet code='mathewborumgame' width=400 height=400></applet> public class mathewborumgame extends japplet { public void init() { dimension dim = getsize(); mathewborumgamepanel avoidance = new mathewborumgamepanel(dim); add(avoidance); /* thread game = new thread(avoidance); game.start();*/ } }// end of extended japplet class class mathewborumgamepanel extends jpanel { //implements runnable{ protected int x = 225; protected int y = 450; private int speed = 0; private boolean isplaying = false; private boolean hashit = false; private string instructions = "press 'enter' start survive long as" + " possible."; private string keyinstructions = "use arrow keys move."; private string test; private dimension dim = null; private thread game = null; private timer timer = new timer(1000, new timerlistener()); public mathewborumgamepanel(dimension paramdimension) { this.dim = paramdimension; setbackground(new color(255, 255, 204)); setforeground(color.black); addkeylistener(new keyadapter() { public void keypressed(keyevent e) { switch (e.getkeycode()) { case keyevent.vk_left: system.out.println("go left"); if(x >= 10) x = x - 10; repaint(); break; case keyevent.vk_right: system.out.println("go right"); if(x <= 490) x = x + 10; repaint(); break; default: //repaint(); break; } } }); // must focusable! setfocusable(true); // important! must have focus.. requestfocusinwindow(); timer.start(); } /* not how animation! @override public void run() { (;;) { repaint(); } }*/ public void paintcomponent(graphics paramgraphics) { super.paintcomponent(paramgraphics); paramgraphics.fillrect(x, y, 50, 15); paramgraphics.setfont(new font("serif", font.bold, 15)); paramgraphics.drawstring(this.instructions, 30, 30); paramgraphics.drawstring(this.keyinstructions, 30, 50); test = test.valueof(x); paramgraphics.drawstring(test, 30, 70); } private class timerlistener implements actionlistener { public void actionperformed(actionevent e) { repaint(); } } }//end of extended jpanel class
Comments
Post a Comment