boundires help

BlitzMax Forums/BlitzMax Beginners Area/boundires help

grimx(Posted 2006) [#1]
tryin to make the ball bounce of the paddles and walls and go in a random direction as long as it is in bounds..
thats where i need help . my bounds checking dos'nt seem to work..
help
(replace media with your own)

Type player
Field image:TImage
Field x#
Field y#
Field speed
End Type
Type ball
Field image:TImage
Field x#
Field y#
Field speed
Field dir
End Type

Global PList:TList= CreateList()
Global BList:TList= CreateList()


Graphics 400,400,0,60
SeedRnd MilliSecs()

SetMaskColor 0,0,0

Create_Player()
Create_Ball()

While Not KeyHit(Key_Escape)

Update_Player()
Update_Ball()


Flip
Cls
Wend

Function Create_Player()
p:player=New player
p.image=LoadImage("paddle1.bmp")
p.x=200
p.y=380
p.speed=3
PList.AddLast(p)
End Function
Function Update_Player()
For p:Player= EachIn PList
DrawImage(p.image,p.x,p.y)
If KeyDown(Key_Left) And p.x>0 Then p.x:-p.speed
If KeyDown(Key_Right) And p.x+ImageWidth(p.image)<400 Then p.x:+p.speed
Next
End Function
Function Create_Ball()
b:ball=New ball
b.image=LoadImage("ball1.bmp")
b.x=200
b.y=20
b.speed=2
b.dir=1
BList.AddLast(b)
End Function
Function Update_Ball()
For b:Ball=EachIn BList
DrawImage(b.image,b.x,b.y)
Select b.dir
Case 1
b.x=b.x
b.y:+b.speed
Case 2
b.x=b.x
b.y:-b.speed
Case 3 'downright
b.x:-b.speed
b.y:+b.speed
Case 4 'downleft
b.x:+b.speed
b.y:+b.speed
Case 5 'upleft
b.x:-b.speed
b.y:-b.speed
Case 6 'upright
b.x:+b.speed
b.y:-b.speed
End Select

For p:player=EachIn PList
If ImagesCollide(p.image,p.x,p.y,0,b.image,b.x,b.y,0)
b.dir=Rnd(2,6)
End If
If b.y<=3 b.dir=Rnd(1,6) <-- Bounds check
If b.x<=3 b.dir=Rnd(2,6) Help
If b.x>=598 b.dir=Rnd(2,6)
Next
Next
End Function


Chris C(Posted 2006) [#2]
i'd have a x movement and a y movement variable ( x&y speed )

if you hit a horizontal surface xm=-xm
if you hit a vertical surface ym=-ym

and do

x:+xm
y:+ym

each frame