suicide1405241470 0 Report post Posted September 11, 2004 Hi,I been making a retro style space game where the spaceship can move around the screen and other ships would slide accross the screen and try and shoot you down.Well, when i play it, its unfinnished, it seems really slow. It moves fast for a few seconds and then suddenly stops/slows down for a second. Sometimes is running really slow. But what dont makes sence is that my Thread.sleep is set at about 0.005 and that is really slow on the game and chnaging it to a lower value doesnt change much. But if i change it to 0, then the game goes crazy, well its still slow, but i end up on another part of the screen, meaning its going too fast.I think its somthing to do with the threading as i got lots of threads but i have given prioities which should make it a little more effiecent, but i dont want to put the threads into one thread becuase it will ruin the timeing of my spacecraft movement, the laser movement and the key listeners.I also thought it could be the display code which repaints every 24 frames, but removing that and adding repaints to every thread still makes it very slow.Thanks in advance if you can help me with the problemHeres the full code below, sorry its not documented, The graphics part is in the middle of the code and is pointed out. package spacy;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class game extends JFrame { BorderLayout borderLayout1 = new BorderLayout(); CardLayout cc = new CardLayout(); boolean ismenu = true; boolean isgame = false; int menuspace = -10; int menuchoice = 0; boolean[] keys = new boolean[256]; int[] playerpos = {10, 200}; int[][] playerlasers = new int[100][3]; long bullettime = System.currentTimeMillis(); //Construct the frame public game() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { this.getContentPane().setLayout(borderLayout1); this.getContentPane().setBackground(Color.black); this.setSize(new Dimension(600, 500)); this.setTitle(".:Space Shooter:."); addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { this_keyPressed(e); } public void keyReleased(KeyEvent e) { this_keyReleased(e); } }); new Thread(new display()).start(); new Thread(new animatemenu()).start(); new Thread(new menuchoiceselection()).start(); //this.getContentPane().add(jPanel1, BorderLayout.CENTER); this.getContentPane().setLayout(cc); jPanel1.setBackground(Color.black); this.getContentPane().add(jPanel1, "jPanel1"); this.getContentPane().add(jPanel2, "jPanel2"); cc.show(this.getContentPane(),"jPanel1"); } //Overridden so we can exit on System Close protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if(e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } JPanel jPanel1 = new JPanel() { public void paintComponent(Graphics g) { Graphics2D c = (Graphics2D)g; c.clearRect(0,0,jPanel1.getWidth(),jPanel1.getHeight()); c.setColor(Color.blue); c.fillRoundRect((jPanel1.getWidth()/2)-50,186 + (menuchoice*20),100,20,5,5); c.setColor(Color.black); c.drawLine(menuspace,100,menuspace+10,105); c.drawLine(menuspace,110,menuspace+10,105); c.setColor(Color.white); c.drawString("START",(jPanel1.getWidth()/2)-20,200); c.drawString("HIGH SCORES",(jPanel1.getWidth()/2)-44,220); c.drawString("OPTIONS",(jPanel1.getWidth()/2)-28,240); c.drawString("EXIT",(jPanel1.getWidth()/2)-16,260); } };//-------------------------panel------------------------------------------------ JPanel jPanel2 = new JPanel() { public void paintComponent(Graphics g) { Graphics2D c = (Graphics2D)g; c.clearRect(0,0,jPanel2.getWidth(),jPanel2.getHeight()); c.setColor(Color.black); c.drawLine(playerpos[0]-10,playerpos[1]-5,playerpos[0],playerpos[1]); c.drawLine(playerpos[0]-10,playerpos[1]+5,playerpos[0],playerpos[1]); for (int i = 0; i != 100; i++) { if (playerlasers[i][0] > 0) { c.drawLine(playerlasers[i][0]-5,playerlasers[i][1],playerlasers[i][0],playerlasers[i][1]); } } } }; class display implements Runnable { public void run() { while(true) { try { Thread.sleep(40); } catch (InterruptedException e){} repaint(); } } } class animatemenu implements Runnable { public void run() { while(ismenu == true) { try { Thread.sleep(100); } catch (InterruptedException e){} menuspace++; if (menuspace > jPanel1.getWidth()) { menuspace = -10; } } } }//=============================GAME============================================= class animategame implements Runnable { public void run() { while(isgame == true) { try { Thread.sleep(1); } catch (InterruptedException e){} for (int i = 0; i != 100; i++) { if (playerlasers[i][0] > 0) { playerlasers[i][0]++; } if (playerlasers[i][0] > 500) { playerlasers[i][0] = 0; } } } } } class gamebuttons implements Runnable { boolean slowdown = false; public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(isgame == true) { if (keys[32] == true && bullettime < System.currentTimeMillis()) { bullettime = System.currentTimeMillis()+500; for (int i = 0; i!=100;i++) { if (playerlasers[i][0] == 0){ playerlasers[i][0] = playerpos[0]; playerlasers[i][1] = playerpos[1]; playerlasers[i][2] = 0; break; } } } if (keys[38] == true && playerpos[1] > 20) { playerpos[1]--; slowdown = true; } else if (keys[40] == true && playerpos[1] < 500) { playerpos[1]++; slowdown = true; } if (keys[37] == true && playerpos[0] > 30) { playerpos[0]--; slowdown = true; } else if (keys[39] == true && playerpos[0] < 400) { playerpos[0]++; slowdown = true; } if (slowdown == true) { slowdown = false; try { Thread.sleep(5); } catch (InterruptedException e){} } } } } class menuchoiceselection implements Runnable { public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(ismenu == true) { try { Thread.sleep(1); } catch (InterruptedException e){} if (keys[38] == true && menuchoice > 0) { menuchoice--; try { Thread.sleep(200); } catch (InterruptedException e){} } else if (keys[40] == true && menuchoice < 3) { menuchoice++; try { Thread.sleep(200); } catch (InterruptedException e){} } else if (keys[10] == true && menuchoice == 0) { System.out.println("Play"); ismenu = false; isgame = true; new Thread(new gamebuttons()).start(); new Thread(new animategame()).start(); setpanel("jPanel2"); } else if (keys[10] == true && menuchoice == 1) { System.out.println("High scores"); } else if (keys[10] == true && menuchoice == 2) { System.out.println("Options"); } else if (keys[10] == true && menuchoice == 3) { System.out.println("BOOM"); System.exit(0); } } } } void this_keyPressed(KeyEvent e) { keys[e.getKeyCode()] = true; //System.out.println("Key: \t" + e.getKeyCode()); } void this_keyReleased(KeyEvent e) { keys[e.getKeyCode()] = false; } void setpanel(String panel) { cc.show(this.getContentPane(),panel); }} Share this post Link to post Share on other sites
Eric Straven 0 Report post Posted September 11, 2004 This is sorta offtopic, but what compiler are you using?? And is Jikes a good compiler? Sorry I'm new to java and haven't even downloaded a compiler yet and help would be appreciated ... Share this post Link to post Share on other sites
Jack Cheng 0 Report post Posted February 18, 2006 Can you attach the java codes in a file where they are better formatted. It's really hard to read the codes when they are that jumbled. Share this post Link to post Share on other sites
Hercco 0 Report post Posted February 24, 2006 Or better yet, use the CODE tag to enclose the program code. Share this post Link to post Share on other sites
twitch 0 Report post Posted February 24, 2006 Thanks Hercco for replying to this topic, helped me spot the excess of code. The code tags can only handle so much, then they will expand horizontally out of the main site. So as an alternative, use the [ codebox ] tags.Anyway, I think this is hardly the type of topic you should be replying to, it is two years old (well, in september it will be). Share this post Link to post Share on other sites