Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2009, 10:48 AM   #1 (permalink)
New Member
 
Join Date: May 2009
Posts: 6
Greg is on a distinguished road
Default MVC pattern for games

Hi All,

Can someone please explain how the MVC pattern is used for a game.

I understand the basic idea behind it and how I could use it to build say an address book but I really struggle to implement it for something like a game. Maybe MVC isn't the pattern I should be using...?

As a simple example how would I structure a space invaders game.

- Controller, captures accelerometer and touches and passes them to the model to update user spaceship position etc. Also contains the main loop to trigger updates in the model (to move enemys across/down screen, do collision detection).

- Model, holds data on all objects. spaceship/enemy position, rotation, health, score.
Shouldn't this also hold info on how to draw the objects. ie. enemy1 = blue, enemy2 = green, battlement shape after being hit.

- View, redraws the screen with info from the model... but wouldn't it be easier to ask each object to draw itself in a given context/view?

Which class deals with the sounds? the model?

Thanks for any help/direction.

Greg
Greg is offline   Reply With Quote
Old 05-08-2009, 12:13 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I'll give my philosophy - it may not be textbook correct.

Quote:
Originally Posted by Greg View Post
- Model, holds data on all objects. spaceship/enemy position, rotation, health, score.
Shouldn't this also hold info on how to draw the objects. ie. enemy1 = blue, enemy2 = green, battlement shape after being hit.
Sure, the model can know things like "I am green" or "I look like enemy.png". No problem.

Quote:
Originally Posted by Greg View Post
- View, redraws the screen with info from the model... but wouldn't it be easier to ask each object to draw itself in a given context/view?
Many books for object-oriented languages start with the example of "shapes who know how to draw themselves" , and it may be very OOP, but it's not very MCV. In my game the model knows each object's position (in map squares, not pixels!) but the view loops through them and draws them.

You get a nice separation of code this way - if I wanted to change my view from layers to OpenGL, there's nothing to change in the model. If I change the camera view or the screen size, those changes are just in the view, not the model. I could go from 2d to 3d with very few changes in the model (replace the pngs with 3d chars).

Plus the code in the model stays very clean - I can move a player by one square by saying player.x +=1 instead of player.x+=MAP_SQUARE_WIDTH.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-12-2009, 06:54 AM   #3 (permalink)
New Member
 
Join Date: May 2009
Posts: 6
Greg is on a distinguished road
Default

thanks Smasher,

I have a feeling MVC might not be the best approach for game I have in mind... a large scrolling world.

I'll have a play it with and see how I get on.

thanks again
Greg
Greg is offline   Reply With Quote
Old 05-12-2009, 11:32 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Greg View Post
thanks Smasher,

I have a feeling MVC might not be the best approach for game I have in mind... a large scrolling world.

I'll have a play it with and see how I get on.

thanks again
Greg
I dunno - if every tree, rock, and orc knows how to draw itself, then you have a lot of classes to modify code in if you make changes to the display (sprite size, orientation, etc.)
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-13-2009, 08:07 AM   #5 (permalink)
New Member
 
Join Date: May 2009
Posts: 6
Greg is on a distinguished road
Default

Yeah I'm starting to agree with you, but for some reason my gut still says otherwise.
I think it's just because it's a different approach and I'm new to Objective-C so I'm a little out of my comfort zone...

However, what I've implementing so far is working well
Greg is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 408
7 members and 401 guests
13dario13, ChrisYates, fredidf, iOS.Lover, Leslie80, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0