My first Cocoa Game that works! (Pong)

Member
Posts: 196
Joined: 2003.10
Post: #1
Ok, here goes:

Binary: http://s91890182.onlinehome.us/Pong.app.sit

Source Code: http://s91890182.onlinehome.us/pongsource.sit

Please download both and let me know what's good, what's not good. the gameplay works - it's supremely unpolished, but it's functional. i can't say i'm a big fan of eye-candy, not to mention my graphics skills are nil to none.

but it's exciting that it's actually fun to play. Any hints as to reducing binary size? 1.3MB seems excessive for pong, even if it compresses to about 250KB...

Thanks in advance. I'm loving this community. This is by far the most satisfying code i've ever written. I played my own game and had fun!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Well done! You've come further already than many people around here Wink

The computer player's paddle shakes when it moves faster than the ball. I suspect you can fix this by having the paddle not move if it's "close enough" to dead-center.

The ball is very unforgiving. It seems like it's being treated as a point rather than a circle.

I ran strip on your binary using the Terminal, and it's now 56KiB uncompressed and 11KiB compressed.
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #3
Nice work!

One small bug: I swear I saw the ball go right through the middle of the computer's bat on one occasion! I couldn't make it happen again, though.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #4
Hrm, I saw it go through too. It can't be a speed issue. I'm on a Dual 2.0 G5, maybe increse your framrate (I should look at the code)
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #5
OneSadCookie Wrote:The computer player's paddle shakes when it moves faster than the ball. I suspect you can fix this by having the paddle not move if it's "close enough" to dead-center.

yeah, i noticed that too. i played around with it last night, but it was getting late. my ai is pretty primitive - it basically just tries to maintain the ball at the middle of the paddle on the y axis.

eventually, if i feel like it, i think i could devise a nice recursive algorithm that would predict the location of the ball and move directly there. this would be, if well implemented, unbeatable.

OneSadCookie Wrote:The ball is very unforgiving. It seems like it's being treated as a point rather than a circle.

What do you mean by this? i mean, it IS a point Wink i had to take away the "if the paddle's moving, change the trajectory" code from the computer paddle, because the computer's paddle is almost ALWAYS moving. so the ball started to bounce too irradically. but it responds to human paddle speed...

OneSadCookie Wrote:I ran strip on your binary using the Terminal, and it's now 56KiB uncompressed and 11KiB compressed.

Ok, i just figured that out. i was trying to run strip on Pong.app, not the underlying binary. fixed.

NCarter Wrote:One small bug: I swear I saw the ball go right through the middle of the computer's bat on one occasion! I couldn't make it happen again, though.

Yeah, it happened to me a few times, too. it's difficult to deal with the ball hitting the top of the paddle. if it does hit the top, it should basically go through your paddle... this could be fixed, but the code required isn't really worth the effort, i think. if you disagree, let me know. but it DID go through the middle of my paddle once or twice. so i'll check that out and see what's happening with that. actually, i know what's happening with that, but i'm not sure i have a fix yet. to be continued Wink

Thanks to you folks for the feedback. It's very helpful so far! It's so much fun to play my own game and have fun with it!

Keep it coming, though! I'd love it if someone looked at my code and told me what I was doing wrong. I've already identified some areas as inneficient - they will be fixed (the ints to record if the paddle is moving up, or down, or static, should be just one int, not 2. also i have 2 switch statements for my keyboard input when it could be 1).

But thanks again, keep the criticisms coming!
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #6
blobbo Wrote:Ok, here goes:

Binary: http://s91890182.onlinehome.us/Pong.app.sit

Source Code: http://s91890182.onlinehome.us/pongsource.sit

Please download both and let me know what's good, what's not good. the gameplay works - it's supremely unpolished, but it's functional. i can't say i'm a big fan of eye-candy, not to mention my graphics skills are nil to none.

but it's exciting that it's actually fun to play. Any hints as to reducing binary size? 1.3MB seems excessive for pong, even if it compresses to about 250KB...

Thanks in advance. I'm loving this community. This is by far the most satisfying code i've ever written. I played my own game and had fun!

Congrats Blob! I'm downloading the game right now. I don't want to look at your code unless I get stuck. Hopefully I'll have my version done this weekend now that my Tic Tac Toe is done.

As far as reducing the size, in your targets select Deployment instead of Development. That reduced mine in size quite a bit.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #7
I had the ball go through the middle of my paddle & the computer's paddle as well. It's cool though. Did you use NSAffineTransform to animate your ball?
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #8
ok, i've figured out why the ball goes through the paddle. to check if you hit the ball with the top of your paddle i check some weird crap. i shouldn't. will be fixed Smile

nope, i just had a ball object, along with two instances of paddle objects. i just updated the model, and redrew my view.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #9
blobbo Wrote:ok, i've figured out why the ball goes through the paddle. to check if you hit the ball with the top of your paddle i check some weird crap. i shouldn't. will be fixed Smile

nope, i just had a ball object, along with two instances of paddle objects. i just updated the model, and redrew my view.

I got a chance to look at your program, now I have a couple of questions:

1) What is the file "Pong_Prefix.h"? Is this something to do with XCode? I'm still using Project Builder & 10.2

2) I notice in your keyDown & keyUp methods, you use [theEvent charactersIgnoringModifiers], whereas I used [theEvent characters]. Is there any advantage to the -charactersIgnoringModifiers method?

I thought it was very cool how you were able to use MVC. Where did you pick up on the idea of setting the view & controller to be outlets for each other? That really clears up for me how to use MVC! Thanks for posting your code Blobbo, I look forward to seeing more of your games in the future!
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #10
Pong_Prefix.h is a prefix header that is usually created by Xcode or ProjectBuilder whenever you make a new project.

The only difference between -[NSEvent characters] and -[NSEvent charactersIgnoringModifiers] is explained by the name. I suppose it would generally be better to use charactersIgnoringModifiers unless you are looking for a modifier.
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #11
to tell you the truth i have no idea what the difference between the characters and charactersIgnoringModifiers is. i mean, i didn't want modifiers messing up my keyboard input, so that's what i used.

the outlet idea just seemed logical. easier to have them initialised when the nib's loaded then to initialise them myself. dunno if that's good coding practise, though...

i think my app's problematic in that the view doesn't communicate directly with the model. Apple's MVC docs are sloppy - from what i understand you're allowed to do that. the controller just interprets stuff. i had duplicated methods in my controller to just "pass" info on to my models, and i didn't like that.
Quote this message in a reply
AlainODea
Unregistered
 
Post: #12
To get a multiple-keys down keyboard interaction coded in Cocoa you can see an article I posted called GameKeyboardHandling on the Cocoa Dev wiki:
http://www.cocoadev.com/index.pl?GameKeyboardHandling
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #13
We appreciate the link, AlainODea, but check out when the thread was started. Wink Good wiki to know about, though.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #14
blobbo: the link appears to be dead... I take it you're hosting off your own comp and that you've turned your comp off for the night Wink
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #15
...it's a year and a half old.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Request] Pong Source & One Question McSpider 6 7,467 Sep 18, 2011 07:21 AM
Last Post: fudog
  link redeclared as different kind of symbol in [...] works in simulator, not device sefiroths 8 9,500 Jul 7, 2011 12:43 AM
Last Post: sefiroths
  Pong AI trouble saxplayer13 15 7,860 Dec 16, 2005 10:40 AM
Last Post: jamil5454
  Pong "Physics" Nick 9 6,170 Sep 28, 2004 07:36 PM
Last Post: aarku
  Pong Questions: blobbo 8 4,221 Jan 21, 2004 08:16 PM
Last Post: blobbo