![]() |
| | #1 (permalink) |
| Banned | #Include "fbgfx.bi" #Macro PageInit() ScreenLock #EndMacro #Macro PageFlip() ScreenUnLock Screenlock #EndMacro #Macro PageClose() Screenunlock #EndMacro #Define Container Do #Define SetToTop Continue Do #Define ExitContainer Exit Do #Define EndContainer Loop While 0 > 1 Namespace Pong '' The side the ball is paddle is on. Enum Side LSide = 0 RSide = 1 End Enum '' a 2D vector. Type Vect2D X As Double Y As Double End Type '' A paddle ball Type Ball Pos As vect2D Vel As Vect2D Rad As Double Spd As Double End Type '' A paddle. Type Paddle Pos As Double Size As Vect2D Side As Integer End Type End Namespace '' Main Code. '' Set up our paddle and pall. Dim As Pong.Paddle Paddle(1) Dim As Pong.Ball Ball '' Score. Dim As Integer Score '' Set up paddle sides. Paddle(0).Side = Pong.LSide Paddle(1).Side = Pong.RSide '' Set up paddle sizes. Paddle(0).Size = Type<Pong.Vect2D>( 5, 80 ) Paddle(1).Size = Type<Pong.Vect2D>( 5, 80 ) '' Set up the ball. Ball.Pos = Type<Pong.Vect2D>( 319, 239 ) '' Set up a velocity. Ball.Vel = Type<Pong.Vect2D>( -.5, -.2 ) '' Set up the ball size. Ball.Rad = 5 '' Create our screen. Screenres 640, 480, 32 '' Lock the page for drawing. PageInit() '' Main loop. Do '' Move the user paddle. If MultiKey(FB.SC_UP) Then Paddle(0).Pos -= 3 If MultiKey(FB.SC_DOWN) Then Paddle(0).Pos += 3 If MultiKey(FB.SC_LEFT) Then Paddle(1).Pos -= 3 If MultiKey(FB.SC_RIGHT) Then Paddle(1).Pos += 3 '' Loop through all of the paddles. For DoVar As Integer = LBound(paddle) To UBound(paddle) '' Bounds checking. If Paddle(DoVar).Pos < 0 Then Paddle(DoVar).Pos = 0 If Paddle(DoVar).Pos > 479 - Paddle(DoVar).Size.Y Then Paddle(DoVar).Pos = 479 - Paddle(DoVar).Size.Y Scope '' Whether or not there was collision. Dim As Integer Col '' A container. Container '' Check the ball against the paddle. If Paddle(DoVar).Side = Pong.Lside Then '' Check for absolutely no possible collision. If Ball.Pos.Y + Ball.Rad < Paddle(DoVar).Pos Then ExitContainer If Ball.Pos.Y - Ball.Rad > Paddle(DoVar).Pos + Paddle(DoVar).Size.Y - 1 Then ExitContainer If Ball.Pos.X - Ball.Rad > Paddle(DoVar).Size.X - 1 Then ExitContainer '' Collision is guaranteed. Col = 1 '' Right side is drawn at screensize - paddlewidth, y ElseIf Paddle(DoVar).Side = Pong.Rside Then '' Check for absolutely no possible collision. If Ball.Pos.Y + Ball.Rad < Paddle(DoVar).Pos Then ExitContainer If Ball.Pos.Y - Ball.Rad > Paddle(DoVar).Pos + Paddle(DoVar).Size.Y - 1 Then ExitContainer If Ball.Pos.X + Ball.Rad < 639 - Paddle(DoVar).Size.X - 1 Then ExitContainer '' Collision is guaranteed. Col = 1 EndIf EndContainer '' There was collision between the paddle and the ball. If Col = 1 Then '' Flip the velocity of the ball. Ball.Vel.X = -Ball.Vel.X Ball.Vel.Y = Ball.Vel.Y EndIf End Scope '' Move ball position. Ball.Pos.X += Ball.Vel.X Ball.Pos.Y += Ball.Vel.Y '' Increase velocity slightly each time. Ball.Vel.X *= 1.0005 Ball.Vel.Y *= 1.0005 '' Ball bounds checking. If Ball.Pos.Y < 0 Then Ball.Vel.Y = -Ball.Vel.Y If Ball.Pos.Y > 479 Then Ball.Vel.Y = -Ball.Vel.Y '' Score subtraction. If Ball.Pos.X < 0 Then Score -= 1: Ball.Pos = Type<Pong.vect2D>( 319, 239 ): Ball.Vel = Type<Pong.vect2D>( -1 + Rnd* 2, -1 + Rnd* 2 ) If Ball.Pos.X > 639 Then Score += 1: Ball.Pos = Type<Pong.vect2D>( 319, 239 ): Ball.Vel = Type<Pong.vect2D>( -1 + Rnd* 2, -1 + Rnd* 2 ) '' Draw the paddles. '' Left side is drawn at 0, y If Paddle(DoVar).Side = Pong.Lside Then Line ( 0, Paddle(DoVar).Pos)- Step ( Paddle(DoVar).Size.X - 1, Paddle(DoVar).Size.Y - 1), RGB(255, 255, 255), bf '' Right side is drawn at screensize - paddlewidth, y ElseIf Paddle(DoVar).Side = Pong.Rside Then Line ( 639 - Paddle(DoVar).Size.X + 1, Paddle(DoVar).Pos)- Step ( Paddle(DoVar).Size.X - 1, Paddle(DoVar).Size.Y - 1), RGB(255, 255, 255), bf EndIf '' Draw the ball. Circle ( Ball.Pos.X, Ball.Pos.Y ), Ball.Rad, RGB(255, 255, 255),,,,f '' Our score. Locate 1, 1 Print score Next PageFlip() Sleep 5, 1 Cls Loop Until MultiKey(1) PageClose() |
| | |
![]() |
| Thread Tools | |
| |