|
Pages: [1]
|
 |
|
Author
|
Topic: Programming commands (Read 1444 times)
|
|
gnrforever
|
I am trying to create a 2 player Pong game. Does anyone know how to tell the paddels to respond to certain keys? In addition, I am not sure of the proper code for the balls movement. Any example or website would be extremely helpful.
Thanks. -_-
|
|
|
|
|
Logged
|
|
|
|
|
Graham
|
in most cases you just make a face, and then change the x and y offsets and then redisplay.
|
|
|
|
|
Logged
|
|
|
|
|
gnrforever
|
I will go and try that.
Thank you.
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
Here's my first line, defining a new style PADDLE, then placing two of those paddles at the top and bottom of the window.
view layout [size 400x400 backcolor black style paddle box 100x20 white at 150x20 paddle-1: paddle at 150x360 paddle-2: paddle]
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
Let's improve that to add an event handler, handling key events.
play-size: 400x400 view/new window: center-face layout [ size (play-size) backcolor black style paddle box 100x20 white at 150x20 paddle-1: paddle at 150x360 paddle-2: paddle at 190x190 ball: box 20x20 white effect [oval] ]
; resize everything correctly here, eg: ball/offset: (play-size - ball/size) / 2
window/feel: make window/feel [ detect: func [face event][ if event/type = 'key [ switch event/key [ #"z" [paddle-1/offset/x: paddle-1/offset/x - 10 show paddle-1] ; move paddle-1 left #"x" [paddle-1/offset/x: paddle-1/offset/x + 10 show paddle-1] left [paddle-2/offset/x: paddle-2/offset/x - 10 show paddle-2] right [paddle-2/offset/x: paddle-2/offset/x + 10 show paddle-2] ] ] event ; allow events to continue (especially the 'close event, allowing the window to be closed) ] ]
do-events
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
Ok, so let's add a time event handler and start time events flowing into the window:
play-size: 400x400 view/new window: center-face layout [ size (play-size) backcolor black style paddle box 100x20 white at 150x20 paddle-1: paddle at 150x360 paddle-2: paddle at 190x190 ball: box 20x20 white effect [oval] ]
; resize everything correctly here, eg: ball/offset: (play-size - ball/size) / 2
window/feel: make window/feel [ detect: func [face event][ if event/type = 'time [ print 'tick ] if event/type = 'key [ switch event/key [ #"z" [paddle-1/offset/x: paddle-1/offset/x - 10 show paddle-1] ; move paddle-1 left #"x" [paddle-1/offset/x: paddle-1/offset/x + 10 show paddle-1] left [paddle-2/offset/x: paddle-2/offset/x - 10 show paddle-2] right [paddle-2/offset/x: paddle-2/offset/x + 10 show paddle-2] ] ] event ; allow events to continue (especially the 'close event, allowing the window to be closed) ] ]
; Start time events flowing for this window window/rate: 10 show window ; changing rate requires SHOW afterwards
do-events
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
Let's add ball velocity, move the ball according to the velocity on every time event, and also check for wall collisions while we are there...
play-size: 400x400 view/new window: center-face layout [ size (play-size) backcolor black style paddle box 100x20 white at 150x20 paddle-1: paddle at 150x360 paddle-2: paddle at 190x190 ball: box 20x20 white effect [oval] with [velocity: none] ; extend the face object adding a new variable, VELOCITY ]
; resize everything correctly here, eg: ball/offset: (play-size - ball/size) / 2
; set initial ball speed and direction (velocity) ball/velocity: -6x3
window/feel: make window/feel [ detect: func [face event][ if event/type = 'time [ ball/offset: ball/offset + ball/velocity if ball/velocity/x < 0 [ ; travelling leftwards ? ; check left wall collision if ball/offset/x < 0 [ ball/velocity/x: - ball/velocity/x ; bounce by negating velocity ball/offset/x: ball/velocity/x - ball/offset/x ; reflect the amount the ball sank into the wall ] ] if ball/velocity/y < 0 [ ; travelling upwards ? ; check top wall collision ] if all [ ball/velocity/y > 0 ; travelling downwards ? ball/offset/y > (window/size/y - ball/size/y) ; and hit bottom wall ? ][ ; etc.. ] show ball ] if event/type = 'key [ switch event/key [ #"z" [paddle-1/offset/x: paddle-1/offset/x - 10 show paddle-1] ; move paddle-1 left #"x" [paddle-1/offset/x: paddle-1/offset/x + 10 show paddle-1] left [paddle-2/offset/x: paddle-2/offset/x - 10 show paddle-2] right [paddle-2/offset/x: paddle-2/offset/x + 10 show paddle-2] ] ] event ; allow events to continue (especially the 'close event, allowing the window to be closed) ] ]
; Start time events flowing for this window window/rate: 10 show window ; changing rate requires SHOW afterwards
do-events
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
Now I leave it up to you to further it as you like. I don't think you're going to like the key control considering the way key events are delivered by rebol, which is more useful for applications programming than games. You can't currently capture 'up events. So the only two keys which you can track continuously are Ctrl and Shift, which you could use for moving a paddle left and right.
Track control and shift by examining time events, eg:
if event/type = 'time [ if event/control [ ; Ctrl key pressed ? ; move left ] if event/shift [ ; Shift key pressed ? ; move right ] ] So you can track the state of these keys as fast as the time events are coming in. But not other keys... :-(
However, you could use three keys, say "A", "S", "D" = left, stop, right which would start the paddle moving left (ie. set paddle/velocity/x: -10) when the "A" key was pressed, stop when "S" was pressed, and start the paddle moving right when "D" was pressed.
|
|
|
|
|
Logged
|
|
|
|
|
gnrforever
|
Thank you soooooooooooooooooooo much. This was excellent. You really know what you are doing. ^_^ I think your status should be changed from newbie.
Thanks again
|
|
|
|
|
Logged
|
|
|
|
|
Anton
|
I love to hear such feedback  , but I am keen to find out what the next step with this game is, where you are going to take it, and what the aim of it all is. Is it just a learning exercise ? A stepping stone to a more complex game ?
|
|
|
|
|
Logged
|
|
|
|
|
gnrforever
|
The first goal I am trying to reach, is to make a simple 2 player game. After that I would really love to add graphics and possibly animations. If I can sucessfully code this game a certain business venture might become available to me. What is key is not just typing code, but understanding what it does. I really do appreciate the time you have put in to my topic. Sorry this response took so long. School started up again. 
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |
News: 01-09-08 Alpha version of REBOL 3 has been released!
2294 Posts in 593 Topics by 3753 Members
Latest Member: swift
|