create game loop thread

Hi I have just been messing about polygons and awt. I have created a Jframe and can draw the polygons ok and have one of them move with keypresses. I'm wondering how to start a gameloop thread(and where to put it!) that will update the jframe independently. From googling im led to believe that I should have one thread for user input and one for the game itself. At the moment I have implemented KeyListener on the board class(code shown below),should I put that out into its own class and make it implement runnable?As the code stands I repaint the JFrame in the keypressed() method just so i can see that it moves correctly Being at it most of the day and I have myself very very confused :) As always any help much appreciated! Also while im at it from online tutourials should I use JPanel instead of JFrame and paintComponent() instead of paint()? import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.AffineTransform; import javax.swing.JFrame; public class Board extends JFrame implements KeyListener{ AffineTransform identity = new AffineTransform(); Graphics2D g2d; Ship ship = new Ship(); public static final int ALIENS = 3; Alien[] alien = new Alien[ALIENS]; Board(){ super("The Board"); setSize(1280,1024); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(Color.BLACK); for(int x=0;x1260){ ship.setxPos(1260); }else ship.setxPos(ship.getxPos()+1); } repaint(); }//end keypressed event public static void main(String[] args){ new Board(); } }

以上就是create game loop thread的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏