Results 1 to 4 of 4
  1. #1
    Fuzzier is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Question [help] game loop

    Take a look at this code:


    Java Code:
    public class Main extends Applet implements Runnable
    {
    	
    	Squar[][] sqr = new Squar[10][10];
    	Ball redBall,blueBall,pinkBall,whiteBall;
    	Thread th;
    	
    	private JButton cmnStart = new JButton("start");
    	
    	
    	private boolean pop=true;
    	
    	public static void main(String[] args){	}
    	
    	public void start(){
    		this.th=new Thread(this);
    		this.th.start();
    	}
    	//public void stop(){}
    	
    
    	public void init(){
    		super.setBackground(Color.gray);
    		
    		this.setSize(500, 500);
    		this.sqr=initBoard(this.sqr);
    		this.redBall = new Ball(Color.red);
    		this.whiteBall = new Ball(Color.white);
    		this.pinkBall = new Ball(Color.pink);
    		this.blueBall = new Ball(Color.blue);
    		
    	}
    	public void  run(){
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    		
    		while(true){
    			
    			repaint();
    			
    			try	{
    				Thread.sleep (100);
    			}catch (InterruptedException ex){}
    			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    		
    		}
    		
    	}
    	
    	public void paint(Graphics g){
    		
    		initPanel(g,this.sqr);
    		pop=false;
    	}
    
    	//---------------------------------_GAME_METHODES_--------------------------------//
    	
    	public Squar[][] initBoard(Squar[][] ar){
    		//Generate the squre array
    		for(int i=0;i<ar.length;i++){
    			for(int j=0;j<ar.length;j++){
    				ar[i][j] = new Squar(null);
    								
    			}
    		}
    		return ar;
    	}
    	
    	
    	public void initPanel(Graphics g,Squar[][] ar){
    		//Draw the buttons
    		
    		//Draw the squre array
    		for(int i=0;i<this.sqr.length;i++){
    			for(int j=0;j<this.sqr.length;j++){
    				this.sqr[i][j].draw(g,80+i*30,80+j*30,Color.green);
    						
    			}
    		}
    		//Draw 3 random balls in the array
    		if (pop=true)
    		PopABall(3,ar);
    		
    	}
    	
    	
    	public void PopABall(int num,Squar[][] ar){
    		int rnd=0,q=0,x=0,y=0;
    		do{	
    			
    			rnd=1+(int)(Math.random()*8);
    			x=(int)(Math.random()*ar.length);
    			y=(int)(Math.random()*ar.length);
    			
    			if(ar[x][y].ball()==null){ 
    					switch (rnd){
    						case 2: {
    						ar[x][y] = new Squar(this.redBall);
    						} ;break;
    						case 3: {
    						ar[x][y] = new Squar(this.whiteBall);
    						} ;break;
    						case 4: {
    						ar[x][y] = new Squar(this.pinkBall);
    						} ;break;
    						case 5: {
    						ar[x][y] = new Squar(this.blueBall);
    						} ;break;
    						default:{
    							
    						}
    					}
    			
    			}
    				
    			
    			q++;
    		}while(q<num);
    		
    	}
    	
    }
    when I run it, the PopABall methode supposed to draw 3 balls
    in the "squar" array, but instead, it's drawing balls untill the array is filled.
    apparently its something in the game loop.

    any help please?
    Last edited by Fuzzier; 10-29-2009 at 08:57 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    when I run it, the PopABall methode supposed to draw 3 balls
    in the "squar" array, but instead, it's drawing balls untill the array is filled.
    apparently its something in the game loop

    Yes. For each randomly selected Squar in array ar you are reassigning that array element to a new instance of Squar which contains a copy of one of your four Balls. So after a while all the array elements will have a non-null Ball in them, ie, a grid filled with balls.
    What you might want/intend to do instead (the PopABall methode supposed to draw 3 balls) is to move the three balls around the grid in a random way. To do this you won't want to replace elements with a "null" Ball with an instance with a Ball. You will want to find the indices of contiguous ar elements for each ar element containing a ball and then (randomly) choose one of these contiguous cells to move the ball into.
    Java Code:
        public void PopABall(int num,Squar[][] ar){
            int rnd=0,q=0,x=0,y=0;
            do{
                rnd=1+(int)(Math.random()*8);
                x=(int)(Math.random()*ar.length);
                y=(int)(Math.random()*ar.length);
    
                if(ar[x][y].ball()==null){
                    switch (rnd){
                        case 2:
                            // if ar[x][y] had a "null" ball in it
                            // before this it will now have a redBall
                            // in it. this particular cell may be
                            // visited again in future calls to, ie,
                            // trips through, this method.
                            ar[x][y] = new Squar(this.redBall);
                            break;
                        case 3:
                            // you do not need curley braces after
                            // the [i]case[/i] statement. the jvm
                            // will execute statements until it
                            // comes to the [i]break[/i] statement.
                            ar[x][y] = new Squar(this.whiteBall);
                            break;
                        case 4:
                            ar[x][y] = new Squar(this.pinkBall);
                            break;
                        case 5:
                            ar[x][y] = new Squar(this.blueBall);
                            break;
                        default:
    
                    }
                }
                q++;
            }while(q<num);
        }
    }

  3. #3
    Fuzzier is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your replay, but I did'nt completely understand.
    You will want to find the indices of contiguous ar elements for each ar element containing a ball and then (randomly) choose one of these contiguous cells to move the ball into.
    Can you show me how to do that?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Neighbors extends JPanel {
        final int PAD = 20;
        final int SIZE = 8;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            double w = getWidth();
            double h = getHeight();
            double xInc = (w - 2*PAD)/SIZE;
            double yInc = (h - 2*PAD)/SIZE;
            for(int i = 0; i <= SIZE; i++) {
                double x = PAD + i*xInc;
                g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
                double y = PAD + i*yInc;
                g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
            }
            // Consider the cell at row 4, col 3.
            int row = 4;
            int col = 3;
            // Fill this cell with red.
            double x = PAD + col*xInc;
            double y = PAD + row*yInc;
            g2.setPaint(Color.red);
            g2.fill(new Rectangle2D.Double(x+1, y+1, xInc-1, yInc-1));
            // Fill contiguous cells with blue.
            g2.setPaint(Color.blue);
            for(int i = row-1; i <= row+1; i++) {
                y = PAD + i*yInc;
                for(int j = col-1; j <= col+1; j++) {
                    if(i == row && j == col) continue;
                    x = PAD + j*xInc;
                    g2.fill(new Rectangle2D.Double(x+1, y+1, xInc-1, yInc-1));
                }
            }
        }
    
        public static void main(String[] args) {
            Neighbors demo = new Neighbors();
            demo.setPreferredSize(new Dimension(400,400));
            JOptionPane.showMessageDialog(null, demo, "", -1);
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. can someone help me with this game??
    By amalina in forum New To Java
    Replies: 1
    Last Post: 08-24-2009, 06:59 AM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 01:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •