Keep Up!

Blitz3D Forums/Blitz3D Beginners Area/Keep Up!

Jono(Posted 2003) [#1]
Hello, All

I've got yet another problem. :(, I've been trying to make my own Breakout clone, i've got a pad that can move left and right and a ball that bounces around. Now if i leave the pad alone the ball bounces around without any lag or slow down...but soon as i start moving the pad both the pad and the ball slows down. this is very frustrating as i've just got everything working...

Source:

AppTitle "BreakIn!"

Local ScreenHeight = 165
Local ScreenWidth = 165
Graphics ScreenWidth,ScreenHeight

Local Pad = LoadImage("Pad.bmp")
Local PadX = ScreenWidth / 2 - ImageWidth(Pad) / 2
Local PadY = 128
Local PadSpeed = 15

Local Ball = LoadImage("Ball.bmp")
Local BallX = ScreenWidth / 2 - ImageWidth(Ball) / 2
Local BallY = PadY - ImageHeight(Ball)
Local BallSpeed = 0
Local BallDirection = 1

MaskImage Pad,250,250,250
MaskImage Ball,250,250,250

SetBuffer BackBuffer()

ClsColor 250,250,250

While Not KeyDown(1)

Cls
DrawImage Pad,PadX,PadY
DrawImage Ball,BallX,BallY
Flip

If KeyDown(203) And PadX => 1 Then PadX = PadX - 1: Delay PadSpeed ; Move Pad Left
If KeyDown(205) And PadX <= ScreenWidth - ImageWidth(Pad)-1 Then PadX = PadX + 1: Delay PadSpeed ; Move Pad Right

;;;;; If Ball Hits Sides Of Screen ;;;;;
; Collision Detection: Wall:Left
If BallX <= 0 And BallDirection = 1 Then BallDirection = 2
If BallX <= 0 And BallDirection = 4 Then BallDirection = 3
; Collision Detection: Wall:Top
If BallY <= 0 And BallDirection = 1 Then BallDirection = 4
If BallY <= 0 And BallDirection = 2 Then BallDirection = 3
; Collision Detection: Wall:Right
If BallX => ScreenWidth - ImageWidth(Ball) And BallDirection = 3 Then BallDirection = 4
If BallX => ScreenWidth - ImageWidth(Ball) And BallDirection = 2 Then BallDirection = 1
; Collision Detection: Wall:Bottom
If BallY => ScreenHeight - ImageHeight(Ball) And BallDirection = 3 Then BallDirection = 2
If BallY => ScreenHeight - ImageHeight(Ball) And BallDirection = 4 Then BallDirection = 1

;;;;; If Ball Hits Pad ;;;;;
If BallDirection = 4 And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 1
If BallDirection = 3 And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 2
; Collision Detection: Pad:Bottom
If BallDirection = 2 And BallY <= PadY + ImageHeight(Pad)-1 And BallY => PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 3
If BallDirection = 1 And BallY <= PadY + ImageHeight(Pad)-1 And BallY => PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 4
; Collision Detection: Pad:Right
If BallDirection = 4 And BallX <= PadX + ImageWidth(Ball)-1 And BallX => PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 2
If BallDirection = 1 And BallX <= PadX + ImageWidth(Ball)-1 And BallX => PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 3
; Collision Detection: Pad:Left
If BallDirection = 2 And BallX => PadX - ImageWidth(Ball)+1 And BallX <= PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 1
If BallDirection = 4 And BallX => PadX - ImageWidth(Ball)+1 And BallX <= PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 3

; Move Ball Around The Screen
If BallSpeed > 0 And BallDirection = 1 Then BallX = BallX - 2: BallY = BallY - 2: Delay BallSpeed ; Move Ball Top:Left
If BallSpeed > 0 And BallDirection = 2 Then BallX = BallX + 2: BallY = BallY - 2: Delay BallSpeed ; Move Ball Top:Right
If BallSpeed > 0 And BallDirection = 3 Then BallX = BallX + 2: BallY = BallY + 2: Delay BallSpeed ; Move Ball Bottom:Right
If BallSpeed > 0 And BallDirection = 4 Then BallX = BallX - 2: BallY = BallY + 2: Delay BallSpeed ; Move Ball Bottom:Left

; Start Ball Moving If It's Not
If BallSpeed = 0 And KeyDown(57) Then BallSpeed = 10

Wend
End

Could it be due to the fact i've got to many IF statements?


Ross C(Posted 2003) [#2]
Hey, the delay command halts the entire program from running, that's where the slowdown is coming from. Everytime you move the bat, your delaying the game for x number of millisecs().
How about this. I've put in a millisecs() timer. If 20 millisecs() has passed then the key for moving the pad will be checked. Taken out PadSpeed variable and put in pad_time and pad_timer. Adjust pad_time for faster/slower pad movement.

AppTitle "BreakIn!" 

Local ScreenHeight = 165 
Local ScreenWidth = 165 
Graphics ScreenWidth,ScreenHeight 

Local Pad = LoadImage("Pad.bmp") 
Local PadX = ScreenWidth / 2 - ImageWidth(Pad) / 2 
Local PadY = 128 

Local Ball = LoadImage("Ball.bmp") 
Local BallX = ScreenWidth / 2 - ImageWidth(Ball) / 2 
Local BallY = PadY - ImageHeight(Ball) 
Local BallSpeed = 0 
Local BallDirection = 1 

MaskImage Pad,250,250,250 
MaskImage Ball,250,250,250 

SetBuffer BackBuffer() 

ClsColor 250,250,250 

Global pad_time=20; This is the time in millisecs() the keys that move the pad will be checked
Global pad_timer-millisecs(); This is the counter for the pad time

While Not KeyDown(1) 

Cls 
DrawImage Pad,PadX,PadY 
DrawImage Ball,BallX,BallY 
Flip 

If Millisecs()>pad_time+pad_timer then; if pad_time amount of millisecs() has passed then do pad movement
   pad_timer=millisecs(); resets timer
   If KeyDown(203) And PadX => 1 Then PadX = PadX - 1; Move Pad Left 
   If KeyDown(205) And PadX <= ScreenWidth - ImageWidth(Pad)-1 Then PadX = PadX + 1; Move Pad Right 
end if
;;;;; If Ball Hits Sides Of Screen ;;;;; 
; Collision Detection: Wall:Left 
If BallX <= 0 And BallDirection = 1 Then BallDirection = 2 
If BallX <= 0 And BallDirection = 4 Then BallDirection = 3 
; Collision Detection: Wall:Top 
If BallY <= 0 And BallDirection = 1 Then BallDirection = 4 
If BallY <= 0 And BallDirection = 2 Then BallDirection = 3 
; Collision Detection: Wall:Right 
If BallX => ScreenWidth - ImageWidth(Ball) And BallDirection = 3 Then BallDirection = 4 
If BallX => ScreenWidth - ImageWidth(Ball) And BallDirection = 2 Then BallDirection = 1 
; Collision Detection: Wall:Bottom 
If BallY => ScreenHeight - ImageHeight(Ball) And BallDirection = 3 Then BallDirection = 2 
If BallY => ScreenHeight - ImageHeight(Ball) And BallDirection = 4 Then BallDirection = 1 

;;;;; If Ball Hits Pad ;;;;; 
If BallDirection = 4 And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 1 
If BallDirection = 3 And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 2 
; Collision Detection: Pad:Bottom 
If BallDirection = 2 And BallY <= PadY + ImageHeight(Pad)-1 And BallY => PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 3 
If BallDirection = 1 And BallY <= PadY + ImageHeight(Pad)-1 And BallY => PadY And BallX <= PadX + ImageWidth(Pad)-1 And BallX => PadX - ImageWidth(Ball)+1 Then BallDirection = 4 
; Collision Detection: Pad:Right 
If BallDirection = 4 And BallX <= PadX + ImageWidth(Ball)-1 And BallX => PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 2 
If BallDirection = 1 And BallX <= PadX + ImageWidth(Ball)-1 And BallX => PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 3 
; Collision Detection: Pad:Left 
If BallDirection = 2 And BallX => PadX - ImageWidth(Ball)+1 And BallX <= PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 1 
If BallDirection = 4 And BallX => PadX - ImageWidth(Ball)+1 And BallX <= PadX And BallY => PadY - ImageHeight(Pad)+1 And BallY <= PadY + ImageHeight(Pad)-1 Then BallDirection = 3 

; Move Ball Around The Screen 
If BallSpeed > 0 And BallDirection = 1 Then BallX = BallX - 2: BallY = BallY - 2: Delay BallSpeed ; Move Ball Top:Left 
If BallSpeed > 0 And BallDirection = 2 Then BallX = BallX + 2: BallY = BallY - 2: Delay BallSpeed ; Move Ball Top:Right 
If BallSpeed > 0 And BallDirection = 3 Then BallX = BallX + 2: BallY = BallY + 2: Delay BallSpeed ; Move Ball Bottom:Right 
If BallSpeed > 0 And BallDirection = 4 Then BallX = BallX - 2: BallY = BallY + 2: Delay BallSpeed ; Move Ball Bottom:Left 

; Start Ball Moving If It's Not 
If BallSpeed = 0 And KeyDown(57) Then BallSpeed = 10 

Wend 
End



Jono(Posted 2003) [#3]
Oh right, i think i get it, but does it matter how many IF statements i use, or do IF statements not effect the speed of the game?


Ross C(Posted 2003) [#4]
No, if statement won't really affect the speed of the game. They are executed lightning quick.


WolRon(Posted 2003) [#5]
If you have about ten thousand if statements, it might make a difference...


Anthony Flack(Posted 2003) [#6]
Even ten thousand should be okay.