So, I am trying to add some elements to this script. Namely adding a obstacle in the form of a 32x32 pixel square.
I am using the following code:
Code: Select all
if Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos =< BL_Xpos
BM_XPos, BM_YPos = BL_XPos - 16, BM_YPos ; Handle collision with left side of obstacle
DebugPrint("Hit The Left Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos =< BL_Xpos + 32
BM_XPos, BM_YPos = BL_XPos + 32, BM_YPos ; Handle collision with right side of obstacle
DebugPrint("Hit The Right Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Ypos >= BL_Ypos
BM_XPos, BM_YPos = BM_XPos, BL_YPos + 32 ; Handle collision with bottom side of obstacle
DebugPrint("Hit The Bottom Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos >= BL_Xpos
BM_XPos, BM_YPos = BM_XPos, BL_YPos - 16 ; Handle collision with top side of obstacle
DebugPrint("Hit The Top Side")
EndIf
However, only two of these conditions work at a time and I am too dumb to figure out why. In fact I spent several hours trying to get any of the condition to work. Any input would be welcome.
The complete script in its current form:
Code: Select all
@DISPLAY{WIDTH = 800, HEIGHT = 600, MODE = "WINDOWED"} ;Create the window
CreateBrush(1, 16, 16, #GREEN) ; CREATE BLOCK-MAN
CreateBrush(2, 32, 32, #GRAY) ; CREATE OBSTICLE
BM_Xpos = #CENTER ; Set initial X-position
BM_Ypos = #CENTER ; Set initial Y-position
SPEED = 3 ; Set the movement speed
SPEED45=Sqrt(SPEED^2/2)
BL_Xpos = 400 ; Obstacle position
BL_Ypos = 300
/*
speed as vector, Pythagoras:
SPEEDX^2+SPEEDY^2 = SPEED^2
SPEEDX^2 = SPEED^2-SPEEDY^2
SPEEDX = Sqrt(SPEED^2-SPEEDY^2)
SPEEDY = Sqrt(SPEED^2-SPEEDX^2)
*/
Function p_UpdateScreen()
;Check Key-presses and move Block-Man:
;SPEEDX=Sqrt(SPEED^2/2)
SPEEDX=SPEED45 ; calculate this only one time
SPEEDY=SPEEDX ; (because 45°)
If Not IsKeyDown("LEFT") And Not IsKeyDown("RIGHT")
SPEEDX=0
SPEEDY=SPEED
EndIf
If Not IsKeyDown("UP") And Not IsKeyDown("DOWN")
SPEEDY=0
SPEEDX=SPEED
EndIf
If IsKeyDown("LEFT") = True Then BM_XPos = BM_XPos - SPEEDX
If IsKeyDown("RIGHT") = True Then BM_XPos = BM_XPos + SPEEDX
If IsKeyDown("UP") = True Then BM_YPos = BM_YPos - SPEEDY
If IsKeyDown("DOWN") = True Then BM_Ypos = BM_Ypos + SPEEDY
;Check if Block-Man leaves the screen and reposition if he does:
If BM_XPos < 0 Then BM_XPos = 0
If BM_XPos > 784 Then BM_XPos = 784
If BM_Ypos < 0 Then BM_Ypos = 0
If BM_Ypos > 584 Then BM_Ypos = 584
;Check if we're colliding with obstacle:
; Can only get two of these conditions to work:
if Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos =< BL_Xpos
BM_XPos, BM_YPos = BL_XPos - 16, BM_YPos ; Handle collision with left side of obstacle
DebugPrint("Hit The Left Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos =< BL_Xpos + 32
BM_XPos, BM_YPos = BL_XPos + 32, BM_YPos ; Handle collision with right side of obstacle
DebugPrint("Hit The Right Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Ypos >= BL_Ypos
BM_XPos, BM_YPos = BM_XPos, BL_YPos + 32 ; Handle collision with bottom side of obstacle
DebugPrint("Hit The Bottom Side")
elseif Collision(#BRUSH, 1, BM_XPos, BM_YPos, 2, BL_Xpos, BL_Ypos ) = True and BM_Xpos >= BL_Xpos
BM_XPos, BM_YPos = BM_XPos, BL_YPos - 16 ; Handle collision with top side of obstacle
DebugPrint("Hit The Top Side")
EndIf
;Display our elements at their respective positions:
DisplayBrush(1, BM_Xpos, BM_Ypos)
DisplayBrush(2, BL_XPos, BL_Ypos)
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
Repeat
Cls()
p_UpdateScreen()
Flip()
Forever
EndDoubleBuffer()