Pong

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jgordon
Posts: 4
Joined: Mon Jan 13, 2014 4:18 pm

Pong

Post by jgordon »

We are creating a two player pong game. We have gotten both paddles to move up and down, and now we are beginning to program the ball. What type of code should be used to make a shape move at different angles and bounce off paddles?
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10103
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Pong

Post by FourthWorld »

In the IDE, click the Resources button - there, see the last Tutorial in the list, "Bouncy".

That example does with a stack against your monitor edges what you want to do with a ball within your window, so I suspect at least some of that code may be a good starting point.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Pong

Post by Simon »

Here is a complete write up;
http://livecodegamedev.net/Techniques/livecode-pong/

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am

Re: Pong

Post by kelyanok »

hello
im from the future and im trying to make a pong too, but im stuck. im a begginner and i already made a few really simple games, and i tried to make one a little bit more difficult. ive made the render of the pong, and now im trying to use collisions but it doesnt work. here's the code for he button "start"

Code: Select all

on mouseUp
   hide me
   collision
   startAll
end mouseUp

on startAll
   move graphic "ball" to 0,5+random(310) in 50 tick
end startAll

on collision
   if intersect(graphic "ball",graphic "paddle")
   then
      answer "working"
   end if
end collision
the "answer working" is only to test if the collision is working. but its nooooooottttt :(
can someone help me? thanks!
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7423
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Pong

Post by jacque »

For one thing, the mouseUp handler is checking for the collision before the ball has moved. It would fail unless you've already set the ball position over the paddle before clicking the button.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am

Re: Pong

Post by kelyanok »

thats true, i changed it. but it still doesnt work... ill try to change all my code and ill post it when i finish it. anyway thank you for your answer
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Pong

Post by dunbarx »

Hi.

I do not want to go too fast for you, but try this. Make three buttons on a new card. Name one of them "ball" and one of them "paddle". Name the other one "Start" Set the loc of btn "ball" to "100,100". Set the loc of btn "paddle" somewhere to the lower right of btn "ball" In the script of btn "start":

Code: Select all

on mouseUp
   startTheBallMoving
end mouseUp

on startTheBallMoving
   set the loc of btn "ball" to item 1 of the loc of btn "ball" +2 & "," & item 2 of the loc of btn "ball" +2
   if the right of btn "ball" > item 3 of the rect of this cd or the bottom of btn "ball" > item 4 of the rect of this cd then
      set the loc of btn "ball" to "100,100"
         answer "A Miss"
      exit to top
   end if
   if intersect(btn "ball",btn"paddle") then
      set the loc of btn "ball" to "100,100"
      answer "A hit!"
      exit to top
   end if
   
   send "startTheBallMoving" to me in 1
end startTheBallMoving
Click btn "start". Btn "ball will move until it either hits btn "paddle" or the edge of the card. This should give you some ideas on the sequence of events that you must keep in mind. It also uses an entirely different method than the "move" command, and you will need to do that in order to test the state of your game on the fly. The "move" command does not easily allow for that, since it is generally used for animation, and you need more control over your environment.

Craig
Post Reply