hi again everyone. I hope I've not become too annoying. Thanks to some of your help, I've completed my first game in Blitz :: Pong!! I've tried to code in the most straight forward and simple structure (KISS). But I believe my code can be further optimized and simplify. I hope that you can please help "mark" my code, and perhaps give me some tips on improving it.
I know I should have used more functions, but it was pretty impossible to do so, because somehow, I can't get my type variables to work in a function. Anyway, here's the code:
Cheers, and thanks again!
I know I should have used more functions, but it was pretty impossible to do so, because somehow, I can't get my type variables to work in a function. Anyway, here's the code:
AppTitle "-= My Pong =-" Global screenx = 640 Global screeny = 480 Global upkey = 200 Global leftkey = 203 Global rightkey = 205 Global downkey = 208 Global batspd = 6 Global MyScore = 0 Global OpScore = 0 SeedRnd MilliSecs() Graphics screenx,screeny,16,2 ;Entity type Type entityinfo Field x Field y Field width Field height Field speedx# Field speedy# Field directionx Field directiony End Type ;Create Mybat instance Mybat.entityinfo = New entityinfo Mybat\x = 320 Mybat\y = screeny - 30 Mybat\width = 80 Mybat\height = 15 ;Create Opbat instance Opbat.entityinfo = New entityinfo Opbat\x = 320 Opbat\y = 30 Opbat\width = 80 Opbat\height = 15 ;Create Ball instance Ball.entityinfo = New entityinfo Ball\x = 320 Ball\y = 240 Ball\width = 15 Ball\height = 15 Ball\speedx# = Rnd(10) Ball\speedy# = 6 ;Rnd(10) Ball\directionx = 1 Ball\directiony = 1 ;Create bat and ball image gfxBat = CreateImage(Mybat\width,Mybat\height) gfxBall = CreateImage(ball\width,ball\height) ;Draw bat and ball image SetBuffer ImageBuffer(gfxBat) Rect 0,0,mybat\width,mybat\height,1 SetBuffer ImageBuffer(gfxBall) Oval 0,0,ball\width,ball\height,1 gfxOpBat = CopyImage(gfxBat) ;Center the images MidHandle gfxBat MidHandle gfxOpBat MidHandle gfxBall ;Switch back to back buffer SetBuffer BackBuffer() ;Game loops starts here ;================================ While Not KeyHit(1) ;================================ ;Clear screen Cls ;Show the score Text 2,screeny-15, "Your Score : " + MyScore Text 2,0, "Computer's Score : " + OpScore AppTitle "-= My Pong =- You : " + MyScore + " Comp : " + OpScore ;Draw my bat onto screen DrawImage gfxbat,mybat\x,mybat\y DrawImage gfxOpBat,Opbat\x,Opbat\y ;Calculate ball location. ;Keep ball in screen If ball\x <(ball\width)/2+5 Or ball\x > screenx-(ball\width)/2-5 Then ball\speedx# = ball\speedx# * - ball\directionx EndIf If ball\y <(ball\height)/2 Then MyScore = MyScore + 1 ;I Scored!! ball\y = 240 ;Reset ball to center-y ball\speedx# = Rnd(2) ;ball move straighter Delay 500 ElseIf ball\y > screeny-(ball\height)/2 Then OpScore = OpScore + 1 ;Opponent Scored!! ball\y = 240 ;Reset ball to center-y ball\speedx# = Rnd(2) ;ball move straighter Delay 500 EndIf ball\x = (ball\x + ball\speedx#) ball\y = (ball\y + ball\speedy#) ;Draw Ball to screen DrawImage gfxball,ball\x,ball\y ;Collision with my or opponent's bat If ImagesCollide (gfxBat,Mybat\x,Mybat\y,0,gfxBall,Ball\x,Ball\y,0) Then ball\speedx# = Rnd(10) ball\speedy# = ball\speedy# * - ball\directiony ball\speedx# = ball\speedx# * - ball\directionx EndIf If ImagesCollide (gfxOpBat,Opbat\x,Opbat\y,0,gfxBall,Ball\x,Ball\y,0) Then ball\speedx# = Rnd(10) ball\speedy# = ball\speedy# * - ball\directiony ball\speedx# = ball\speedx# * - ball\directionx EndIf ;Simple Opponent AI Movement If ball\x < OpBat\x Then OpBat\x = MoveBatLeft(OpBat\x, OpBat\width) EndIf If ball\x > OpBat\x Then OpBat\x = MoveBatRight(OpBat\x, OpBat\width) EndIf ;My Bat Movement If leftkey()=1 Then Mybat\x = movebatleft(Mybat\x, Mybat\width) EndIf If rightkey()=1 Then Mybat\x = movebatright(Mybat\x, Mybat\width) EndIf Flip Wend ;Free images FreeImage gfxBat FreeImage gfxOpBat FreeImage gfxBall Cls WaitKey() End ;Function to move the bat left Function MoveBatLeft(batx, batwidth) Local old_x = batx If batx > (batwidth/2) Then batx = batx - batspd Return batx Else Return old_x EndIf End Function ;Function to move the bat right Function MoveBatRight(batx, batwidth) Local old_x = batx If batx < screenx-(batwidth/2) Then batx = batx + batspd Return batx Else Return old_x EndIf End Function ;Key functions ;Leftkey Function LeftKey() If KeyDown(leftkey) = 1 Then Return 1 Else Return 0 EndIf End Function ;rightkey Function RightKey() If KeyDown(rightkey) = 1 Then Return 1 Else Return 0 EndIf End Function ;Upkey Function UpKey() If KeyDown(upkey) = 1 Then Return 1 Else Return 0 EndIf End Function ;Downkey Function DownKey() If KeyDown(Downkey) = 1 Then Return 1 Else Return 0 EndIf End Function
Cheers, and thanks again!

