This is the code for a simple game I am making just to get the hang of types and to learn collisions. Can someone help me with the colisions part. I am working on the part that tests when a bullet hits the rectangle coming down. The wy I have bin trying to write it is
If RectsOverlap (bullet\x,bullet\y,3,20,moveall\x,moveall\y,20,25) Then
The resulting eror is " variable must be a type"
Here is the code
Graphics 640,480
SetBuffer BackBuffer()
SeedRnd (MilliSecs ())
; make random boxes fall and if one reaches the bottom you loose
Print "this is a game by matt anthony"
Type player
Field x
Field y
Field r,g,b
End Type
Type rocks
Field x
Field y
Field r,g,b
Field fallrate ; alien's fall rate
End Type
Type bullet
Field x
Field y
Field r,g,b
Field bulletrate
End Type
Const rocknumber = 5
For a = 1 To rocknumber
rock.rocks = New rocks
rock\x = Rnd(20,620)
rock\y = 0
rock\fallrate = Rnd (-1,1.5) + Rnd(1.3,2.8)
rock\r=Rnd(100,255)
rock\g=Rnd(1,255)
rock\b=Rnd(1,255)
Next
player.player=New player
; Colour:
player\r = 25
player\g = 0
player\b = 200
player\x = 320
player\y = 440
Rect player\x, player\y, 50, 10, 1
Color 255, 255, 255
Text player\x+80, player\y, "Player 1"
WaitKey()
;start of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
While Not KeyHit(1)
;Cls
For moveall.rocks = Each rocks
Delay 10
moveall\y = moveall\y + moveall\fallrate
Color rock\r, rock\g, rock\b
Rect moveall\x, moveall\y, 20, 25,0 ; Draw each invader
If moveall\y > 480 Then atbottom = 1 ; If any are at bottom of screen, end!
If atbottom=1 Then Print "Game over! The earth is in ashes!":Delay 5000:Print "Hope your happy.":Delay 1000 :End
Next
;203=left,205=right
If KeyDown(203) player\x=player\x-15
If KeyDown(205) player\x=player\x+15
Color player\r, player\g, player\b
Rect player\x, player\y, 50, 10, 1
Rect player\x+5, player\y-8,40,8,1
Rect player\x+20, player\y-16,10,8,1
If KeyHit (57)
CreateBullet(player\x,player\y)
EndIf
UpdateBullets()
Flip
Cls
Wend
;End of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Function CreateBullet(x,y)
;Create a new bullet
bullets.bullet = New bullet
bullets\x = x+23
bullets\y = y-20
;bullets\bulletrate=-4
End Function
Function UpdateBullets()
For bullet.bullet=Each bullet
bullet\y=bullet\y-5
If bullet\y < 0
Delete bullet
Else
Color 255,200,200
Rect bullet\x,bullet\y,3,20,1
Color 0,0,200
Rect bullet\x,bullet\y+12,3,6,1
Color Rnd (100,255), Rnd (100,255),0
Rect bullet\x,bullet\y+16,3,6,1
EndIf
;RectsOverlap (rect1 X,rect1 Y,rect1 Width,rect1 Height,rect2 X,rect2 Y,rect2 Width,rect2 Height)
;Parameters
;rect1 X = rectangle 1 x location
;rect1 Y = rectangle 1 y location
;rect1 Width = rectangle 1 width
;rect1 Height = rectangle 1 height
;rect2 X = rectangle 2 x location
;rect2 Y = rectangle 2 y location
;rect2 Width = rectangle 2 width
;rect2 Height = rectangle 2 height
If RectsOverlap (bullets\x,y,3,20,moveall\x,moveall\y,20,25) Then
;If RectsOverlap (
;End
EndIf
Next
End Function
If RectsOverlap (bullet\x,bullet\y,3,20,moveall\x,moveall\y,20,25) Then
The resulting eror is " variable must be a type"
Here is the code
Graphics 640,480
SetBuffer BackBuffer()
SeedRnd (MilliSecs ())
; make random boxes fall and if one reaches the bottom you loose
Print "this is a game by matt anthony"
Type player
Field x
Field y
Field r,g,b
End Type
Type rocks
Field x
Field y
Field r,g,b
Field fallrate ; alien's fall rate
End Type
Type bullet
Field x
Field y
Field r,g,b
Field bulletrate
End Type
Const rocknumber = 5
For a = 1 To rocknumber
rock.rocks = New rocks
rock\x = Rnd(20,620)
rock\y = 0
rock\fallrate = Rnd (-1,1.5) + Rnd(1.3,2.8)
rock\r=Rnd(100,255)
rock\g=Rnd(1,255)
rock\b=Rnd(1,255)
Next
player.player=New player
; Colour:
player\r = 25
player\g = 0
player\b = 200
player\x = 320
player\y = 440
Rect player\x, player\y, 50, 10, 1
Color 255, 255, 255
Text player\x+80, player\y, "Player 1"
WaitKey()
;start of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
While Not KeyHit(1)
;Cls
For moveall.rocks = Each rocks
Delay 10
moveall\y = moveall\y + moveall\fallrate
Color rock\r, rock\g, rock\b
Rect moveall\x, moveall\y, 20, 25,0 ; Draw each invader
If moveall\y > 480 Then atbottom = 1 ; If any are at bottom of screen, end!
If atbottom=1 Then Print "Game over! The earth is in ashes!":Delay 5000:Print "Hope your happy.":Delay 1000 :End
Next
;203=left,205=right
If KeyDown(203) player\x=player\x-15
If KeyDown(205) player\x=player\x+15
Color player\r, player\g, player\b
Rect player\x, player\y, 50, 10, 1
Rect player\x+5, player\y-8,40,8,1
Rect player\x+20, player\y-16,10,8,1
If KeyHit (57)
CreateBullet(player\x,player\y)
EndIf
UpdateBullets()
Flip
Cls
Wend
;End of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Function CreateBullet(x,y)
;Create a new bullet
bullets.bullet = New bullet
bullets\x = x+23
bullets\y = y-20
;bullets\bulletrate=-4
End Function
Function UpdateBullets()
For bullet.bullet=Each bullet
bullet\y=bullet\y-5
If bullet\y < 0
Delete bullet
Else
Color 255,200,200
Rect bullet\x,bullet\y,3,20,1
Color 0,0,200
Rect bullet\x,bullet\y+12,3,6,1
Color Rnd (100,255), Rnd (100,255),0
Rect bullet\x,bullet\y+16,3,6,1
EndIf
;RectsOverlap (rect1 X,rect1 Y,rect1 Width,rect1 Height,rect2 X,rect2 Y,rect2 Width,rect2 Height)
;Parameters
;rect1 X = rectangle 1 x location
;rect1 Y = rectangle 1 y location
;rect1 Width = rectangle 1 width
;rect1 Height = rectangle 1 height
;rect2 X = rectangle 2 x location
;rect2 Y = rectangle 2 y location
;rect2 Width = rectangle 2 width
;rect2 Height = rectangle 2 height
If RectsOverlap (bullets\x,y,3,20,moveall\x,moveall\y,20,25) Then
;If RectsOverlap (
;End
EndIf
Next
End Function