Pong Questions:

Member
Posts: 196
Joined: 2003.10
Post: #1
Hi all,

so my cocoa pong is going ok here. i've got a paddle that moves when i press up or down, the ball bounces off the walls ok. but i'm encountering some sticky animation questions that aren't easily answered. so here goes:

taking keyboard input, parsing it, and shooting off a message to my controller who in turn modifies data in my paddle model is great, easy to understand. but it bogs down my ball. i tried putting my ball animation within a thread, and it's better, but still a bit boggy. it just bogs through the animate method every 0.015 seconds and refreshes my view. it's ok, but when i move my paddle there's still (barely) noticeable lag.

so should i have two timers running, one that updates the ball, and one that updates the paddles? what's a good paradigm for this situation?

thanks in advance...
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
K.I.S.S. is a great paradigm
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #3
*rolls eyes* that doesn't answer anything Smile basically, how do i get my ball moving at a consistent speed while taking keyboard input?
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #4
I don't really know what's going on in your program, but from your description it sounds like your keyboard input is directly controlling the speed of the ball in some way. Is it being influenced by the keyboard repeat rate or something? If so, that's not a good idea in a realtime game.

I suggest you have a single timer which controls all game activity and redraws the screen (it's only Pong, no need to get sophisticated!). Every time the timer fires, you move the paddles and the ball, then redraw the screen. The keyboard events shouldn't move the paddles or update the screen directly - instead, when you receive a keyboard event, make a note of what it was and utilise that information the next time the timer fires.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #5
well that's basically what's happening. you know, i think you're right about my key repeating thing. there's a delay between the first press and the first reaction. right now the system works just as you say, the view takes the keyboard input with the following code:

Code:
- (void)keyDown:(NSEvent *)theEvent
{
    NSString *keyChar = [theEvent charactersIgnoringModifiers];
    switch ([keyChar characterAtIndex:0])
    {
        case NSUpArrowFunctionKey:
            [PongControllerOutlet movePaddle:1];
            [self setNeedsDisplay:YES];
            return;
    case NSDownArrowFunctionKey:
            [PongControllerOutlet movePaddle:0];
            [self setNeedsDisplay:YES];
            return;
    }
}

and changes the paddle's state. oh wait. i just saw that. i don't need to redraw. d'oh! redundant post. ah well, i'll post it anyway to show how i can make silly mistakes! let me try that...
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #6
ok, so the extra redraws are gone and so are my jagged graphics problems! w00t. thanks a ton, that was very helpful.

now, on to the next problem: there's a noticeable "key repeat" lag for my mousedown event after the initial call. how do i set the key repeat rate to nothing for this app? thanks Smile
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #7
Great, I'm pleased to hear that it's working better! Smile

blobbo Wrote:now, on to the next problem: there's a noticeable "key repeat" lag for my mousedown event after the initial call. how do i set the key repeat rate to nothing for this app?
That sounds wrong - I wouldn't expect mousedown events to be affected by the key repeat rate unless there's something odd going on with your program.

It might help if you briefly explained which events you handle and how you're using each kind of event.

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #8
keydown is affected by key repeat rate.
BUT! if you capture keydown and key up, you can simulate no key repeat
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #9
oh i get it. so set a flag when i get a keydown, then take away the flag with keyup. i'll try that now.
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,474 Sep 18, 2011 07:21 AM
Last Post: fudog
  Pong AI trouble saxplayer13 15 7,878 Dec 16, 2005 10:40 AM
Last Post: jamil5454
  My first Cocoa Game that works! (Pong) blobbo 16 9,977 Aug 4, 2005 03:15 PM
Last Post: Andrew
  Pong "Physics" Nick 9 6,174 Sep 28, 2004 07:36 PM
Last Post: aarku
  Tic Tac Toe almost done... Next up: Pong! blobbo 15 7,699 Jan 16, 2004 06:33 PM
Last Post: blobbo