My game is randomly pausing


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: My game is randomly pausing

  1. #1
    Join Date
    May 2007
    Posts
    1

    My game is randomly pausing

    So for my final in Java me and my friend made snake, heres the link:
    http://myspace-source.com/snake/
    Heres the code:
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.util.Random;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.Font;
    
        
    public class snake extends Applet implements Runnable
    {
    Random generator = new Random();
        int x_pos = 30;       
        int y_pos = 100;       
        int x_speed = 0;       
        int y_speed = 0;
        int radius = 5;      
        int appletsize_x = 500;
        int appletsize_y = 500;
        int x_apple = 250;
        int y_apple = 250;
        int nsnake = 1;
        int sdir = 2;
        int GameOver = 0;
        int level= 1;
        int start = 1;
        int high = 1;
            private Image dbImage;
        private Graphics dbg;
        private Image startimg;
        private Image gameoverimg;
      
        int[] x_posa, y_posa,s_dir,x_posa2,y_posa2,s_dir2,emptyarr;                                  
    
                                               
      
        public snake(){
            s_dir = new int[10000];
            x_posa = new int[10000];
            y_posa = new int[10000];
            emptyarr = new int[1000];
            x_posa[0] = x_pos;
            y_posa[0] = y_pos;
            s_dir[0] = sdir;
        }
    
        public void init()
        {
            startimg = getImage(getCodeBase(),"caterpillar.gif");
            gameoverimg = getImage(getCodeBase(),"owned.gif");
        }
        
        public void start ()
        {
           
            Thread th = new Thread (this);
          
            th.start ();
        }
    
        public void stop()
        {
      
        }
    
        public void destroy()
        {
    
        }
    
       
       public boolean keyDown (Event e, int key)
        {
       
          if (key == Event.LEFT && sdir != 2)
            {
                // Ball bewegt sich dann nach links
                x_speed = -5;
                y_speed = 0;
                sdir = 1;
                start = 0;
                GameOver = 0;
            }
                       else if (x_posa[0] > appletsize_x - radius)
                {
                    // Ändern der Richtung des Balles
                    x_speed = 0;
                }
            // rechte Cursortaste
            else if (key == Event.RIGHT && sdir != 1)
            {
                // Ball bewegt sich dann nach rechts
                x_speed = 5;
                y_speed = 0;
                sdir = 2;
                start = 0;
                GameOver = 0;
            }
            else if (x_posa[0] > appletsize_x - radius)
                {
                    // Ändern der Richtung des Balles
                    x_speed = 0;
                }
           
            else if (key == Event.DOWN && sdir != 3 )//if key = s go down
            {
               
        y_speed = 5;
        x_speed = 0;
        sdir = 4;
        start = 0;
        GameOver = 0;
            }
                    else if (y_posa[0] > appletsize_y)
        {
            y_speed = 0;
        }
            else if (key == Event.UP  && sdir != 4)//if key = w go up
            {
                y_speed = -5;
                x_speed = 0;
                sdir=3;
                start = 0;
                GameOver = 0;
            }
            else if (y_posa[0] < 0)
                {
                    y_speed = 0;
                }  
           
           
            else
            {
                // Ausgabe von gedrüktem Key und Zahlenwert an die Standardausgabe
                System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
            }
    
            return true;
        }
       
       
        public boolean mouseDown (Event e, int x, int y)
        {    
       return true;
        }
    
        public void run()
        {
              
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    
          
            while (true)
            {
                if (x_posa[0] <= x_apple+10 && x_posa[0] >= x_apple-10) //user's mover hit powerup
                {
                  if (y_posa[0] <= y_apple+10 && y_posa[0] >= y_apple-10)
                {
                   nsnake++;
                   if(s_dir[nsnake-2] == 1)
                   {
                   x_posa[nsnake-1] = x_posa[nsnake-2] - 10;
                   y_posa[nsnake-1] = y_posa[nsnake-2];
                   s_dir[nsnake-1] = s_dir[nsnake-2];
                   }
                  if(s_dir[nsnake-2] == 2)
                   {
                   x_posa[nsnake-1] = x_posa[nsnake-2] + 10;
                   y_posa[nsnake-1] = y_posa[nsnake-2];
                   s_dir[nsnake-1] = s_dir[nsnake-2];
                   }
                   if(s_dir[nsnake-2] == 3)
                   {
                   x_posa[nsnake-1] = x_posa[nsnake-2];
                   y_posa[nsnake-1] = y_posa[nsnake-2]-10;
                   s_dir[nsnake-1] = s_dir[nsnake-2];
                   }               
                   if(s_dir[nsnake-2] == 4)
                   {
                   x_posa[nsnake-1] = x_posa[nsnake-2];
                   y_posa[nsnake-1] = y_posa[nsnake-2]+10;
                   s_dir[nsnake-1] = s_dir[nsnake-2];
                   }               
                   makeApple();
                   
                }
                }
                if (x_posa[0] > appletsize_x - radius)
                {
                    // Ändern der Richtung des Balles
                    GameOver = 1;
                }
                // Ball brührt linken Rand und prallt ab
                else if (x_posa[0] < radius)
                {
                    // Ändern der Richtung des Balles
                    GameOver = 1;
                }
    
                // Verändern der x- Koordinate
              
               
                if (y_posa[0] > appletsize_y - radius)
                {
                    // Ändern der Richtung des Balles
                    GameOver = 1;
                }
                // Ball brührt linken Rand und prallt ab
                else if (y_posa[0] < radius)
                {
                    // Ändern der Richtung des Balles
                    GameOver = 1;
                }
                     if(nsnake == 1){
           x_posa[0] = x_posa[0] + x_speed;
           y_posa[0] = y_posa[0] + y_speed;
           s_dir[0] = sdir;
        }
        
        else if(nsnake > 1)
        {
            x_posa2 = x_posa;
            y_posa2 = y_posa;
            s_dir2 = s_dir; 
        for (int i = nsnake;i > 0;i--)
        {
            x_posa[i] = x_posa2[i-1];
            y_posa[i] = y_posa2[i-1];
            s_dir[i] = s_dir2[i-1];
    
        }
               x_posa[0] = x_posa2[0] + x_speed;
           y_posa[0] = y_posa2[0] + y_speed;
           s_dir[0] = sdir;
        }
        if (nsnake > 10)
        {
            
           for (int i= 10; i < nsnake; i++)
        {
        if(x_posa[0] == x_posa[i] && y_posa[0] == y_posa[i])
        {
            GameOver=1;   
        }
        
        }
    }
    
        
        
       
    
                repaint();
    
                try
                {
                  
                    Thread.sleep (20); //pause for movement
                }
                catch (InterruptedException ex)
                {
                    // do nothing
                }
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
           
                 
        }
       
        public void update (Graphics g)
        {
            // Initialisierung des DoubleBuffers
            if (dbImage == null)
            {
                dbImage = createImage (this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics ();
            }
    
            // Bildschirm im Hintergrund löschen
            dbg.setColor (getBackground ());
            dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
            // Auf gelöschten Hintergrund Vordergrund zeichnen
            dbg.setColor (getForeground());
            paint (dbg);
    
            // Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
            g.drawImage (dbImage, 0, 0, this);
           
        }
    
        public void paint (Graphics g)
        {          
          g.setColor  (Color.green);
    
          for(int i = 0;i<nsnake;i++)
          {
          g.fillOval (x_posa[i] - radius, y_posa[i] - radius, 2 * radius, 2 * radius);
        }  
          g.setColor (Color.red);
            g.fillOval (x_apple - radius, y_apple - radius, 2 * radius, 2 * radius);
            g.setColor(Color.black);
            g.drawString("Points: "+ nsnake, 5, 475);
            g.drawString("High Score: "+ high, 5, 495);
            if (start == 1)
            {
              g.drawImage(startimg,50,50,this);
            }
            
            if (GameOver == 1)
            {
                if(nsnake > high )
                {   
               high = nsnake;
            }
               x_speed =0;
               y_speed =0;
               nsnake=1;
            x_pos = 30;
            y_pos = 100;
            sdir = 2;
             x_posa[0] = x_pos;
            y_posa[0] = y_pos;
            s_dir[0] = sdir;
            g.drawImage(gameoverimg,100,50,this);
            }
           
    
    }
        public void makeApple(){
            x_apple = generator.nextInt(96)*5+10;                     /*set random position*/
            y_apple = generator.nextInt(96)*5+10;
        }
    
    }
    The game runs fine on his computer. When I run it on mine, it runs for about 6 seconds, pauses for 1 second, then keeps going, and repeating that process.

    On his computer it goes without pausing the whole game.

    I think it has something to do with our thread setup, but I don't know what it is. Any ideas?

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    I think it might just be your computer...I ran it for a few minutes and there were no pauses.

  3. #3
    Join Date
    May 2007
    Posts
    2
    It works OK on my computer. Since this is for your final you should change "Its" in "Its not snake" to "It's". "It's" is a contraction of "it is".

Similar Threads

  1. Replies: 1
    Last Post: 02-26-2007, 04:26 PM
  2. Ajax based game too slow..!!! Plz Help anyone
    By kartik_maguwala in forum AJAX
    Replies: 0
    Last Post: 02-16-2007, 04:59 AM
  3. Replies: 3
    Last Post: 06-23-2006, 05:44 AM
  4. Replies: 3
    Last Post: 03-08-2005, 11:24 PM
  5. Framework of a complex game...
    By AGuy in forum Java
    Replies: 0
    Last Post: 07-31-2000, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center