Rebol Talk Forum  |  REBOL Discussions  |  REBOL View  |  Topic: Programming commands
Pages: [1] Print
Author Topic: Programming commands  (Read 1412 times)
gnrforever
Newbie
*
Offline Offline

Posts: 9


View Profile
Programming commands
« on: August 07, 2006, 06:58:18 PM »

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
Full Member
***
Offline Offline

Posts: 113


View Profile
Re: Programming commands
« Reply #1 on: August 08, 2006, 04:53:39 AM »

in most cases you just make a face, and then change the x and y offsets and then redisplay.
Logged

gnrforever
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Programming commands
« Reply #2 on: August 09, 2006, 04:55:47 PM »

I will go and try that.

Thank you.
Logged
Anton
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #3 on: August 20, 2006, 12:55:47 PM »

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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #4 on: August 20, 2006, 01:03:20 PM »

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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #5 on: August 20, 2006, 01:07:13 PM »

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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #6 on: August 20, 2006, 01:20:34 PM »

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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #7 on: August 20, 2006, 01:34:16 PM »

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
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Programming commands
« Reply #8 on: August 20, 2006, 04:20:08 PM »

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
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Programming commands
« Reply #9 on: August 21, 2006, 10:50:24 AM »

I love to hear such feedback Smiley,  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
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Programming commands
« Reply #10 on: October 10, 2006, 08:37:50 PM »

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. Smiley
Logged
Pages: [1] Print 
Rebol Talk Forum  |  REBOL Discussions  |  REBOL View  |  Topic: Programming commands
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 12, 2008, 12:25:21 AM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2251 Posts in 589 Topics by 2420 Members
Latest Member: pharmacytovvv

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc