Hi everyone,
So this might be a little more than a code snippet, but I hope you still enjoy it.
A tutorial by DarkPRINCIPLES got me started. [href]Here's a link[/href]
It's basically the same as the original pong, but you can move left and right now to put pressure on your opponent. I'm having just a little trouble with the multiplayer. You might not even notice, but when player A jams down on the arrow keys, Player B can't move. Is this a typical problem? Does anyone have a quick fix? thanks all...
` Daygamer Pong
` Improved from Pong by DarkPRINCIPLES by the Daygamer
Randomize Timer()
Type ObjectData
x as Float
y as Float
xspeed as Float
yspeed as Float
lives as Integer
trigger as Integer
control as Integer
aggressive as Integer
skill as Integer
screen as Integer
playonce as Integer
EndType
Global Player as ObjectData
global AI as ObjectData
Global Ball as ObjectData
Global Value as ObjectData
Ball.x = 320
Ball.y = 240
Ball.xspeed = 2
Ball.yspeed = rnd(1)+1
Player.x = 590
Player.y = 240
AI.x = 50
AI.y = 240
Player.lives = 3
AI.lives = 3
AI.trigger = 200
AI.aggressive = rnd(2)+1
Player.trigger = 280
Player.aggressive = - rnd(2)-1
Value.screen = 1
Player.control = 1
Hide Mouse
Sync On
Sync Rate 60
Backdrop On
Path$ = Get DIR$()
`Load Sound "Impact.wav",1
`Load Sound "Boundry.wav",2
`Load Sound "Win.wav",3
`Load Sound "Lose.wav",4
Do
if Value.playonce = 0 then Intro()
Draw_Screen()
if AI.control = 0 then AI()
Player_Control()
if Player.control=0 then Player_AI()
Draw_Pad(Player.x, Player.y)
Draw_Pad(AI.x, AI.y)
Ball()
Screentext()
Sync
loop
Function Draw_Screen()
`Top line
Line 10, 9, 630, 9
Line 10, 10, 630, 10
Line 10, 11, 630, 11
`Bottom line
Line 10, 469, 630, 469
Line 10, 470, 630, 470
Line 10, 471, 630, 471
`Middle Line
Line 319, 10, 319, 470
Line 320, 10, 320, 470
Line 321, 10, 321, 470
`Middle Circle
Circle 320, 240, 59
Circle 320, 240, 60
Circle 320, 240, 61
`Left Line
Line 29, 10, 29, 470
Line 30, 10, 30, 470
Line 31, 10, 31, 470
`Right Line
Line 609, 10, 609, 470
Line 610, 10, 610, 470
Line 611, 10, 611, 470
Color Backdrop rgb(0, 0, 0)
EndFunction
Function Draw_Pad(x, y)
Box x-10, y-25, x+10, y+25
EndFunction
Function Player_Control()
if ball.xspeed > 0 then If inkey$()="w" then dec AI.y, 5 : AI.control=1
if ball.xspeed > 0 then If inkey$()="s" then inc AI.y, 5 : AI.control=1
`hey, I know these three lines should go under the AI function, but I couldn't get them to work there.
if ball.xspeed > 0
if AI.aggressive > 0
Dec AI.x, AI.aggressive
Else
Inc AI.x, AI.aggressive
Endif
EndIf
if Player.control = 0
if ball.xspeed < 0
if Player.aggressive < 0
Dec Player.x, Player.aggressive
Else
Inc Player.x, Player.aggressive
Endif
EndIf
Endif
If Upkey()=1 then dec Player.y, 5 : Player.control=1
If Downkey()=1 then inc Player.y, 5 : Player.control=1
If Player.x > AI.x+50 then If Leftkey()=1 then dec Player.x, 5 : Player.control=1
If Rightkey()=1 then inc Player.x, 5 : Player.control=1
If Controlkey()=1 then Player.control=0
if ball.xspeed < 0 then If inkey$()="w" then dec AI.y, 5 : AI.control=1
if ball.xspeed < 0 then If inkey$()="s" then inc AI.y, 5 : AI.control=1
If Player.y > 445 then Player.y = 445
If Player.y < 35 then Player.y = 35
If Player.x > 590 then Player.x = 590
If Player.x < 70 then Player.x = 70
If AI.y > 445 then AI.y = 445
If AI.y < 35 then AI.y = 35
If AI.x > 570 then AI.x = 570
If AI.x < 50 then AI.x = 50
If inkey$()="o" then Value.screen = 1
If inkey$()="f" then Value.screen = 0
If inkey$()="c" then AI.control=0
`if multiplayer then reset player positions
If AI.control=1 then Player.x=590
If AI.control=1 then AI.x=50
EndFunction
Function Ball()
`Update Ball position
Inc Ball.x, Ball.xspeed
Inc Ball.y, Ball.yspeed
`Boundary
If Ball.x < 15
Dec AI.lives
Wait 1000
Reset()
EndIf
If Ball.x > 625
Dec Player.lives
Wait 1000
playonce = 1
Reset()
Endif
If Ball.y < 15
Ball.yspeed = abs(Ball.yspeed)
if sound exist (2) then Play Sound 2
Endif
If Ball.y > 465
Ball.yspeed = Ball.yspeed -(Ball.yspeed * 2)
if sound exist (2) then Play sound 2
Endif
`AI Pad Collision
If Ball.x < AI.x + 10 AND Ball.x > AI.x - 10 AND Ball.xspeed < 0
IF Ball.y < AI.y + 25 AND Ball.y > AI.y - 25
if AI.control = 0
Ball.xspeed = abs(Ball.xspeed) + 0.4
if player.skill > 0 then Ball.xspeed = Ball.xspeed + player.skill / 4
Ball.yspeed = Ball.yspeed + rnd(2)-1
else
Ball.xspeed = abs(Ball.xspeed) + 0.4
endif
if sound exist (1) then Play Sound 1
AI.aggressive = rnd(2)-1 + AI.skill
Player.aggressive = - rnd(2)-1 - Player.skill
Player.trigger = 640 - RND(250) - 150 - Player.skill*50
if Player.trigger < 100 then Player.trigger = 100
AI.trigger = 0
if AI.control = 1
If inkey$()="w" then Ball.yspeed = Ball.yspeed - 1
If inkey$()="s" then Ball.yspeed = Ball.yspeed + 1
Endif
EndIf
Endif
`Player Pad Collision
If Ball.x > Player.x - 10 AND Ball.x < Player.x + 10 AND Ball.xspeed > 0
If Ball.y < Player.y + 25 AND Ball.y > Player.y -25
if player.control = 0
Ball.xspeed = Ball.xspeed - (Ball.xspeed * 2) - 0.4
Ball.yspeed = Ball.yspeed + rnd(2)-1
else
Ball.xspeed = Ball.xspeed - (Ball.xspeed * 2) - 0.4
endif
if sound exist (1) then Play Sound 1
AI.aggressive = rnd(2)-1 + AI.skill
AI.trigger = RND(250) + 150 + AI.skill*50
if AI.trigger > 380 then AI.trigger = 380
Player.aggressive = - rnd(2)-1 - Player.skill
Player.trigger = 0
If Downkey() = 1 then Ball.yspeed = Ball.yspeed + 1
If Upkey() = 1 then Ball.yspeed = Ball.yspeed - 1
If Rightkey() = 1 then Ball.xspeed = Ball.xspeed + 0.5
If Leftkey() = 1 then Ball.xspeed = Ball.xspeed - 0.5
EndIf
EndIf
`draw ball
Circle Ball.x, Ball.y, 5
Circle Ball.x, Ball.y, 4
Circle Ball.x, Ball.y, 3
Circle Ball.x, Ball.y, 2
Circle Ball.x, Ball.y, 1
EndFunction
`Reset
Function Reset()
If Player.lives < 0
`make AI get harder or easier depending how good player plays!
temp = (Player.lives - AI.lives)
inc AI.skill, temp
inc Player.skill, -temp
AI.lives = 3
Player.lives = 3
if sound exist (4) then Play Sound 4
do
if AI.control = 0 then Center Text 320, 240, "Computer B Wins!! Press Enter to try again!!"
if AI.control = 1 then Center Text 320, 240, "Player B Wins!! Press Enter to try again!!"
If returnkey()=1 then exit
if player.control = 0 and AI.control = 0 then wait 5000 : exit
Sync
Loop
EndIf
If AI.lives < 0
`make AI get harder or easier depending how good player plays!
temp = (Player.lives - AI.lives)
inc AI.skill, temp
inc Player.skill, -temp
AI.lives = 3
Player.lives = 3
if sound exist (3) then Play Sound 3
do
if Player.control=0 then Center Text 320, 240, "Computer A Wins!! Press any key to try again!!"
if Player.control=1 then Center Text 320, 240, "Player A Wins!! Press any key to try again!!"
If returnkey()=1 then exit
if player.control = 0 and AI.control = 0 then wait 5000 : exit
Sync
Loop
Endif
Ball.x = 320
Ball.y = 240
temp = RND(1)
Select temp
Case 0
Ball.xspeed = -2
EndCase
Case 1
Ball.xspeed = 2
EndCase
Case Default
Sync Off
Print "Opps, there has been an error! Serve direction out of random range"
Wait Key
End
EndCase
EndSelect
temp = RND(1)
Select temp
Case 0
Ball.yspeed = rnd(1)+1
EndCase
Case 1
tempsec = rnd(1)+1
Ball.yspeed = tempsec - (tempsec * 2)
EndCase
Case Default
Sync Off
Print "Opps, there has been an error! Serve direction out of random range"
Wait Key
End
EndCase
EndSelect
AI.trigger = RND(250) + 150 + AI.skill
if AI.trigger < 100 then AI.trigger = 100
Player.trigger = RND(250) + 150 + Player.skill
if Player.trigger < 100 then Player.trigger = 100
Player.x = 590
Player.y = 240
AI.x = 50
AI.y = 240
EndFunction
`Text
Function ScreenText()
if AI.control=0 then Text 50, 10, "Computer B Lives: " + Str$(AI.lives)
if AI.control=1 then Text 50, 10, "Player B Lives: " + Str$(AI.lives)
if Player.control=0 then Text 420, 10, "Computer A Lives: " + Str$(Player.lives)
if Player.control=1 then Text 420, 10, "Player A Lives: " + Str$(Player.lives)
if Value.screen = 1
`Text 50, 440, "Ball.yspeed: " + Str$(Ball.xspeed)
`Text 50, 450, "Ball.yspeed: " + Str$(Ball.yspeed)
if player.control = 0 then Text 420, 450, "Computer A Skill: " + Str$(Player.skill)
`if player.control = 0 then Text 370, 410, "Player.trigger: " + Str$(Player.trigger)
`if player.control = 0 then Text 370, 440, "Player.aggressive: " + Str$(Player.aggressive)
if AI.control = 0 then Text 50, 450, "Computer B Skill: " + Str$(AI.skill)
`if AI.control = 0 then Text 370, 440, "AI.trigger: " + Str$(AI.trigger)
`if AI.control = 0 then Text 370, 440, "AI.aggressive: " + Str$(AI.aggressive)
Endif
EndFunction
`AI
Function AI()
If Ball.x < AI.trigger
If AI.y > Ball.y
temp = AI.y - Ball.y
If temp > 5
Dec AI.y, AI.aggressive/3+5
Else
Dec AI.y, temp
EndIf
EndIf
If AI.y < Ball.y
temp = abs(AI.y - Ball.y)
If temp > 5
Inc AI.y, AI.aggressive/3+5
Else
Inc AI.y, temp
EndIf
EndIf
If AI.x > Ball.x
Dec AI.x, AI.aggressive
EndIf
If AI.x < Ball.x
If Player.x > AI.x+50 then Inc AI.x, AI.aggressive
EndIf
If AI.y > 445 then AI.y = 445
If AI.y < 35 then AI.y = 35
EndIf
EndFunction
`Player.control
Function Player_AI()
If Ball.x > Player.trigger
If Player.y > Ball.y
temp = Player.y - Ball.y
If temp > 5
Dec Player.y, AI.aggressive/3+5
Else
Dec Player.y, temp
EndIf
EndIf
If Player.y < Ball.y
temp = abs(Player.y - Ball.y)
If temp > 5
Inc Player.y, AI.aggressive/3+5
Else
Inc Player.y, temp
EndIf
EndIf
If Player.x < Ball.x
Inc Player.x, Player.aggressive
EndIf
If Player.x > Ball.x
If AI.x < Player.x-50 then Dec Player.x, Player.aggressive
EndIf
If Player.y > 445 then Player.y = 445
If Player.y < 35 then Player.y = 35
EndIf
EndFunction
`Intro
Function Intro()
color backdrop 0
do
Center Text 320,80,"Welcome to Daygamer Pong. I did not create all of this game."
Center Text 320,100,"I got the foundation from a tutorial found at"
Center Text 320,120,"http://www.youtube.com/watch?v=0ymIjl6fRBo by DarkPRINCIPLES"
Center Text 320,160,"Anyway, after embellishing it some, I came up with what your about to see."
Center Text 320,180,"The concept is much like original pong, except you can move in left and right"
Center Text 320,200,"in addition to moving up and down. To beat your opponent faster, you can change"
Center Text 320,220,"the ball speed and direction by hitting it while moving. The computer will"
Center Text 320,240,"get harder or easier depending on how you play. You can look at the"
Center Text 320,260,"'Player skill' to see how hard he is playing. You can actually take control"
Center Text 320,280,"of Player B by using the 's 'and 'w' keys. In multiplayer you can't move right"
Center Text 320,300,"and left though. To turn the computer back on press the 'c' key. You can also"
Center Text 320,320,"turn on the computer for player A by pressing the 'control' key."
Center Text 320,340,"There you have it! Hope you have fun."
Center Text 320,360,"See if you can get the computer skill to 5 or so."
Center Text 320,380,"Please Press Enter"
if returnkey()=1 then exit
sync
loop
Value.playonce = 1
EndFunction
Have a nice day, God Bless You!