Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 27, 2009, 04:43 PM   #1
Awesomeness
macrumors member
 
Join Date: Feb 2009
Game loop help?

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!

Last edited by Awesomeness; Mar 28, 2009 at 01:09 PM.
Awesomeness is offline   0 Reply With Quote
Old Mar 27, 2009, 05:08 PM   #2
MrFusion
macrumors 6502a
 
Join Date: Jun 2005
Location: West-Europe
Quote:
Originally Posted by Awesomeness View Post
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
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?
MrFusion is offline   0 Reply With Quote
Old Mar 27, 2009, 10:41 PM   #3
mufflon
macrumors 6502
 
Join Date: Sep 2006
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 {}
mufflon is offline   0 Reply With Quote
Old Mar 28, 2009, 10:43 AM   #4
Awesomeness
Thread Starter
macrumors member
 
Join Date: Feb 2009
Quote:
Originally Posted by MrFusion View Post
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?
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.
Awesomeness is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Recovery Loop | Cant get iPhone 5 6.1.3 out of loop Fatush iPhone Tips, Help and Troubleshooting 1 Sep 19, 2013 10:14 PM
iPhone: Can't get out of a respring loop! help!! mustangalex00 Jailbreaks and iOS Hacks 7 Aug 20, 2013 01:48 PM
iPhone: iPHone 4 stuck in DFU loop then boot loop then DFU loop! urbanmacUser Jailbreaks and iOS Hacks 1 Jun 7, 2013 05:30 PM
Loop Stopper? Oldmanmac Mac Basics and Help 4 Apr 11, 2013 10:53 AM
iPhone 4 Stuck on restore loop, Help! I'm stuck on anger and frustration loop! vegas60 iPhone Tips, Help and Troubleshooting 3 Nov 8, 2012 09:49 AM

Forum Jump

All times are GMT -5. The time now is 10:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC