Blitzmax 2d Collide Basic
posted on 30 Apr 2009 02:27 by inoobna
Blitzmax มีฟังค์ชั่นหนึ่งนะครับที่ใช้เชคการชนของพี่ได้ดีเลยคือ
imagesCollide:Int(image1,x1,y1,frame1,image2,x2,y2,frame2)
โดยจะคือค่าเป็น ข้อมูลชนิด Boolean
Code :
Apptitle="Basic Collision"
Graphics 800, 600
Local Ball:IBall = IBall.Create(50, 50)
Local Box:IBox = IBox.Create(150, 150)
Repeat
Cls
If ImagesCollide(Ball.entity, Ball.x, Ball.y, 0, Box.entity, Box.x, Box.y, 0)
DrawText("Collision !!", 0, 0)
End If
Ball.Update()
Box.Draw()
Flip
Until AppTerminate() Or KeyDown(KEY_ESCAPE) ;End
Type IObj
Field x:Float, y:Float
Field speed:Float, entity:TImage
Field width:Float, height:Float
End Type
Type IBox Extends IObj
Function Create:IBox(posX:Float, posY:Float)
Local Box:IBox = New IBox
Box.x = posX;Box.y = posY
Box.width = 30;Box.height = 30
Box.entity = CreateImage(Box.width, Box.height)
SetColor(0, 0, 255)
DrawRect(Box.x, Box.y, Box.width, Box.height)
SetColor(255, 255, 255)
GrabImage(Box.entity, Box.x, Box.y)
Return Box
End Function
Method Draw()
DrawImage Self.entity, Self.x, Self.y
End Method
End Type
Type IBall Extends IObj
Function Create:IBall(posX:Float, posY:Float)
Local ball:IBall = New IBall
ball.width = 30;ball.height = 30
ball.x = posX;ball.y = posY
ball.speed = 1
ball.entity = CreateImage(ball.width, ball.height)
SetColor(255, 150, 50)
DrawOval x, y, 30, 30
SetColor(255, 255, 255)
GrabImage(ball.entity, x, y)
Return ball
End Function
Method Update()
If KeyDown(KEY_W)
Self.y:-Self.speed
End If
If KeyDown(KEY_S)
Self.y:+Self.speed
End If
If KeyDown(KEY_A)
Self.x:-Self.speed
End If
If KeyDown(KEY_D)
Self.x:+Self.speed
End If
DrawImage(Self.entity, Self.x, Self.y)
End Method
End Type