Creative COW SIGN IN :: SPONSORS :: ABOUT US :: CONTACT US
FORUMS: listlist (w/ descriptions)archivetagssearchhall of fame

Tip of the week: a game of Pong

Cow Forums : Adobe Director
VIEW POSTS   •   ADD A NEW POST   •   SEARCH   •   CHANGE FORUM
Respond to this post   •   Return to posts index   •   Read entire thread


Tip of the week: a game of Pong
by Brodd Nesset on Jun 3, 2008 at 10:28:32 am

This is a simple game with endless attraction and fun, a real videogame classic. Not the most advanced Lingo here; no trigonometry with determening angles etc. and no advanced collision detection. Instead it's a good starting point I hope for understanding the logic behind 'switches' which you need in games like this.

Requirements and setup:

1. A ball which is a bitmap member. Do not use a 'Shape' member, due to problem with reg-point. Size around 18 pixels diameter.

2. A 'batter' which is another bitmap member. This has a visible part which is the bat itself, and an invisible ‘zone’ that surrounds it, used for collision detection. If your movie or play area has a black background, create a black rectangle around 40 pixels wide and 80 tall. In the middle draw the ‘bat’ with a contrasting colour.

3. Set up the movie to be 320 x 240 to have the game work right away. If you want another size you will need to alter the script slightly.

Insert the ‘batter’ member in Sprite channel #2, with its right edge about 15 pixels away from the right side of the play area (screen).

Insert the ‘ball’ member in Sprite channel #3

(This means there’s room in channel #1 for some background graphics)

4. Import a ‘hit’ sound of your liking, and name the member “hit”

5. Create a Text member and call it “hits”. Use a coloured font and a large size. Drag to Stage. Use ‘Matte’ Sprite ink to make the background transparent.

4 & 5 are optional, but you need to comment these out in the script if you omit them.

6. The script below is the entire game in one Scorescript. It start right away when you enter the frame. If you want a starting screen you need to create that yourself. There, member(“hits”).text = “0” will initialize / reset the points. A Stop button would probably also be nice ;-) it can simply go back to the starting screen.


on enterFrame me
global gSpeedH, gSpeedV, gBatSpeed

--Initializes this variable, if empty
if voidP(gSpeedH) then
gSpeedH = 8
gSpeedV = gSpeedH / 2
end if

gBatSpeed = 8 --Omit if set elsewhere

BallH = sprite(3).locH
BallV = sprite(3).locV

--Hit left wall, switch direction right:
if BallH < 10 then
gSpeedH = abs(gSpeedH)
end if

--Hit right wall, switch direction left:
if BallH > 310 then
gSpeedH = (0 - gSpeedH)
end if

--Hit the batter:
if sprite(3).within(sprite(2)) then
if gSpeedH = abs(gSpeedH) then
--Play a sound:
puppetsound "hit"
--Update score:
member("hits").text = string(value(member("hits").text) + 1)
gSpeedH = (0 - gSpeedH) --Bounce left
else
gSpeedH = abs(gSpeedH) --Bounce right, behind bat
end if
end if

--Update horizontal movement:
sprite(3).locH = BallH + gSpeedH

--Hit the ceiling or floor: switch directions, update vertical movement:
if BallV < 10 then
gSpeedV = abs(gSpeedV)
else
if BallV > 230 then
gSpeedV = (0 - gSpeedV)
end if
end if

sprite(3).locV = BallV + gSpeedV


--Moving the batter with arrow keys:

if keypressed(125) then
sprite(2).locV = sprite(2).locV + gBatSpeed
end if

if keypressed(126) then
sprite(2).locV = sprite(2).locV - gBatSpeed
end if

--Limit Batter movement:
if sprite(2).locV < 20 then
sprite(2).locV = 20
end if
if sprite(2).locV > 220 then
sprite(2).locV = 220
end if

end


on exitFrame me
go the frame
end






Respond to this post   •   Return to posts index   •   Read entire thread


Current Message Thread:




Note: If you are a registered user please click here to login before posting.

Your post will not be accepted if your name and email address are not registered in our database. Click here if you do not have an account.

Name
E-Mail Address
Subject
E-Mail me when someone responds
Just This Message   Entire Thread   None  

Message:

Add Bold Tag To Message (JavaScript required)

To put any item inside this tag:

1. Highlight the desired text
2.Click this buttonAdd Italic Tag To Message (JavaScript required)

To put any item inside this tag:

1. Highlight the desired text
2.Click this buttonAdd Underline Tag To Message (JavaScript required)

To put any item inside this tag:

1. Highlight the desired text
2.Click this buttonAdd Image Link Tag To Message (JavaScript required)

To put any item inside this tag:

1. Highlight the desired text
2.Click this buttonAdd URL Link Tag To Message (JavaScript required)

To put any item inside this tag:

1. Highlight the desired text
2.Click this button

Note: The following are HTML characters and may cause parts of your post to disappear if not used correctly: < > &
To include any portion of the post in your response, highlight the desired text and hit the "Q" key. Read more...



Add your message signature


 


Note: By clicking "Post Direct" button above, you are agreeing to the Creative Cow's Code of Conduct.



FORUMSTUTORIALSMAGAZINEDVDsBOOKSPODCASTSEVENTSSERVICESNEWSLETTERNEWSBLOGS

© CreativeCOW.net All rights are reserved.

[Top]