Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 27, 2009, 03: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 12:09 PM.
Awesomeness is offline   0 Reply With Quote
Old Mar 27, 2009, 04: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, 09: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, 09: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Help! iPhone stuck in Connect to iTunes Loop Coolio-Alert Jailbreaks and iOS Hacks 2 Mar 26, 2012 02:23 PM
A Possible Fix For DFU Loops in iPhone 4 and iPod Touch mooodey iPhone Tips, Help and Troubleshooting 0 Aug 9, 2011 05:56 PM
iPhone 3G DFU Loop dweezle3 iPhone Tips, Help and Troubleshooting 5 May 15, 2011 02:51 PM
General C++ game loop question Branskins Mac Programming 6 Jun 15, 2010 02:00 PM
How to run code just once every time i want in game loop? PizzaTray iPhone/iPad Programming 2 Nov 10, 2009 02:35 PM


All times are GMT -5. The time now is 08:35 PM.

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

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