Game loop help?

Discussion in 'Mac Programming' started by Awesomeness, Mar 27, 2009.

  1. macrumors member

    Joined:
    Feb 12, 2009
    #1
    Ok, I am making a game in Java, (but this is overall code design help, not specific to Java) and I have made a (poorly designed) primitive version of a future game. It has no loop, when it detects input it re renders. At first this worked fine... But I want to add an AI. I tried making it into a game loop but it freezes and dies. Can someone give help on this sort of problem? It gets consumed by the while loop and nothing else happens. HELP!

    Thanks, Awesomeness


    Pseudo code:

    main()
    Code:
    import window stuffs;
    import painty stuffs;
    
    public class NewGame {
    
        public static void main() {
            new window;
            new GamePanel(width, height) inside;
            make window visible;
        }
    }
    
    public class GamePanel extends Canvas {
    
        GamePanel(int width, int height) {
            set bg to green; // only once, I use dirty rects
            player = new Dot(x, y, width, height, dotColor, bgColor);
            if key pressed:
                move player according to key pressed;
                repaint();
        }
    
        //note that Dot is just a square class that I made.
        //It's very useful!  It uses dirty rects.  I left out
        //some of thew painting code because it doesn't matter.
    }
    
    Sorry if the pseudo code is bad, I'm not sure how you're supposed to write pseudo code, so if this is not the right way, please show how it's supposed to be done.

    Ok, with mufflon's post (thanks!) and a little help from the internet, I found the answer! Hooray!
     
  2. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #2
    If you don't want to post actual code, you could post some pseudo code so at least we can look at the logic of your design.

    What is happening during the loop?
    What is the loop supposed to do?
    The user starts something, re-render, the AI does something, re render.
    Wait for user input, re-render, the AI does something, re render.
    Or is the game not turn based?
     
  3. macrumors 6502

    Joined:
    Sep 15, 2006
    #3
    the best idea, atleast in my book, is splitting everything a much as possible into the model-view-controller pattern wikipedia link, what this would mean is to rely on threads - atleast for the rendering, so that it's asynchronous against the AI and player (whose calculcations might interfere with proper rendering of the game).

    To make certain the more sensitive data won't be read/written at the same time make it syncronized...

    To express it in code (a bit tired, but it should be ok)

    Code:
    import java.util.LinkedList;
    
    public class MyMain {
    
    	public static void main(String[] args) {
    		Model model = new Model();
    		View view = new View(model);
    		Controller userInterface = new Controller(model);
    	}
    
    }
    
    public synchronized class Model extends Thread {
    	LinkedList<Integer[]> events = new LinkedList<Integer[]>();
    	
    	public Model() {
    	
    	}
    	
    	public boolean hasChanged() {
    		return true;
    		//returns true if anything has changed
    	}
    	
    	public synchronized theData getData() {
    	}
    	
    	private synchronized void readData(theData data) {
    	
    	}
    	
    	private synchronized void addEvent(Integer[] coords) {
    		this.events.add(coords);
    	}
    	
    	public void run() {
    		while(true) {
    			if(this.events.size() > 0) {
    				//do input to the game
    			}
    			//do whatever needs to be done
    			try {
    				sleep(50);
    			} catch(Exception E) {}
    		}
    	}
    	
    }
    
    class View extends Thread {
    	private Model model;
    	
    	public View(Model model) {
    		this.model = model;
    	}
    	
    	public void run() {
    		while(true) {
    			if(this.model.hasChanged()) {
    				//render
    			} 
    			try {
    				sleep(50);
    			} catch(Exception E) {}
    		}
    	}
    }
    
    class Controller {
    
    	private Model model;
    
    	public Controller(Model model) {
    		this.model = model;
    	}
    	
    	//mouselisterners and whatever, can easily be included in the other classes
    }
    
    
    
    class theData {}
     
  4. thread starter macrumors member

    Joined:
    Feb 12, 2009
    #4
    I want to make the while loop re-render anything needing to be re-rendered (e.g. stuff that's moved) and update the AI(s), which so far simply walk towards the player.
     

Share This Page