Пример столкновения спрайтов

Данный пример показывает столкновение спрайтов многоугольников.

Платформы: Windows , Linux
Автор: BasicCoder2

imagecollide.png

#INCLUDE "fbgfx.bi"
Using fb
Screenres 640,480,32

Type SPRITE
    As Integer x   'top left coordinates
    As Integer y
    As Integer w   'width
    As Integer h   'height
    As Double  r   'degrees of rotation
    As Double  px  'point around which to rotate
    As Double  py
    As Integer d   'display = 1
    As FB.Image Ptr image
End Type

Dim Shared As SPRITE diamond  'shared makes them GLOBAL variables
Dim Shared As SPRITE star1

'create two bitmaps
diamond.image = Imagecreate( 64, 64, 0 )
star1.image = Imagecreate( 64, 64, 0 )

Function ImagesCollide(s1 As SPRITE, s2 As SPRITE) As Integer
    Dim As Uinteger pixel_value
    Dim As Integer hit,x1,y1,x2,y2

    hit = 0
    For j As Integer = s1.y To s1.y+s1.h-1
        For i As Integer = s1.x To s1.x+s1.w-1

            If i > s2.x And i < s2.x+s2.w Then
                If j > s2.y And j < s2.y + s2.h Then
                    x1 = i-s1.x
                    y1 = j-s1.y
                    x2 = i-s2.x
                    y2 = j-s2.y
                    If Point(x1,y1, s1.image) <> Rgb(255,0,255) And Point(x2,y2,s2.image) <> Rgb(255,0,255) Then
                        hit = 1
                        Pset(i,j),Rgb(100,100,0) 'show overlap
                    End If
                End If
            End If

        Next i
    Next j

    Return hit
End Function


Sub drawSprites()
    Screenlock
    Color Rgb(255,255,255) 'white ink
    Cls
    Locate 1,1
    Print "USE CURSOR KEYS TO MOVE BALL"
    Print "HIT ESC KEY TO QUIT GAME"

    If star1.d = 1 Then
        Put (star1.x,star1.y),star1.image,Trans
    End If
    'test for collision
    Locate 41,1
    If ImagesCollide(diamond,star1) Then
        'paint diamond.image,(diamond.x+diamond.w\2,diamond.y+diamond.h\2),rgb(110,10,255)
        Print "COLLISION"
    Else
        Print "         "
        'paint diamond.image,(diamond.x+diamond.w\2,diamond.y+diamond.h\2),rgb(240,10,25)
    End If
    'diamond sprite is drawn after and thus over star1 sprite
    If diamond.d = 1 Then
        Put (diamond.x,diamond.y),diamond.image,Trans
    End If

    Screenunlock

    Sleep 1,1
End Sub

Sub updateData()
    ' Check arrow keys and update position accordingly
    If Multikey(&h4B) And diamond.x > 0 Then diamond.x = diamond.x - 1
    If Multikey(&h4D) And diamond.x < 639 Then diamond.x = diamond.x + 1
    If Multikey(&h48) And diamond.y > 0 Then diamond.y = diamond.y - 1
    If Multikey(&h50) And diamond.y < 479 Then diamond.y = diamond.y + 1
    'Check and adjust for outOfBounds
    If diamond.x <0 Then diamond.x = 0
    If diamond.x > 640 - diamond.w Then diamond.x = 640-diamond.w
    If diamond.y <0 Then diamond.y = 0
    If diamond.y > 480 - diamond.h Then diamond.y = 480-diamond.h
End Sub



'read data for diamond sprite
Dim As Uinteger pixelValue
Dim As String datum
For j As Integer = 0 To 63
    Read datum
    For i As Integer = 0 To 63
        pixelValue = Val(Mid(datum,i+1,1))
        If pixelValue = 1 Then
            Pset diamond.image,(i,j),Rgb(255,0,0)  'red
        Else
            Pset diamond.image,(i,j),Rgb(255,0,255)
        End If
    Next i
Next j

diamond.w = 64
diamond.h = 64
diamond.x = 200
diamond.y = 200
diamond.d = 1

For j As Integer = 0 To 63
    Read datum
    For i As Integer = 0 To 63
        pixelValue = Val(Mid(datum,i+1,1))
        If pixelValue = 1 Then
            Pset star1.image,(i,j),Rgb(0,255,0) 'green
        Else
            Pset star1.image,(i,j),Rgb(255,0,255)
        End If
    Next i
Next j

star1.w = 64
star1.h = 64
star1.x = 100
star1.y = 100
star1.d = 1

'*****************  MAIN LOOP ******************
drawSprites()
Do

    drawSprites()
    updateData()

Loop Until Multikey(&H01)  'esc key

'clean up
Imagedestroy(diamond.image)
Imagedestroy(star1.image)

End
Data "0000000000000000000000000000000000000000000000000000000000000000"
Data "0000000000000000000000000000000010000000000000000000000000000000"
Data "0000000000000000000000000000000010000000000000000000000000000000"
Data "0000000000000000000000000000000111000000000000000000000000000000"
Data "0000000000000000000000000000000111000000000000000000000000000000"
Data "0000000000000000000000000000000111000000000000000000000000000000"
Data "0000000000000000000000000000001111100000000000000000000000000000"
Data "0000000000000000000000000000001111100000000000000000000000000000"
Data "0000000000000000000000000000001111100000000000000000000000000000"
Data "0000000000000000000000000000011111110000000000000000000000000000"
Data "0000000000000000000000000000011111110000000000000000000000000000"
Data "0000000000000000000000000000011111110000000000000000000000000000"
Data "0000000000000000000000000000111111111000000000000000000000000000"
Data "0000000000000000000000000000111111111000000000000000000000000000"
Data "0000000000000000000000000000111111111000000000000000000000000000"
Data "0000000000000000000000000001111111111100000000000000000000000000"
Data "0000000000000000000000000001111111111100000000000000000000000000"
Data "0000000000000000000000000001111111111100000000000000000000000000"
Data "0000000000000000000000000011111111111110000000000000000000000000"
Data "0000000000000000000000000011111111111110000000000000000000000000"
Data "0000000000000000000000000011111111111110000000000000000000000000"
Data "0000000000000000000000000111111111111111000000000000000000000000"
Data "0000000000000000000000000111111111111111000000000000000000000000"
Data "0000000000000000000000000111111111111111000000000000000000000000"
Data "0111111111111111111111111111111111111111111111111111111111111111"
Data "0011111111111111111111111111111111111111111111111111111111111110"
Data "0001111111111111111111111111111111111111111111111111111111111100"
Data "0000111111111111111111111111111111111111111111111111111111111000"
Data "0000001111111111111111111111111111111111111111111111111111100000"
Data "0000000111111111111111111111111111111111111111111111111111000000"
Data "0000000011111111111111111111111111111111111111111111111110000000"
Data "0000000000111111111111111111111111111111111111111111111000000000"
Data "0000000000011111111111111111111111111111111111111111110000000000"
Data "0000000000001111111111111111111111111111111111111111100000000000"
Data "0000000000000011111111111111111111111111111111111110000000000000"
Data "0000000000000001111111111111111111111111111111111100000000000000"
Data "0000000000000000111111111111111111111111111111111000000000000000"
Data "0000000000000000001111111111111111111111111111100000000000000000"
Data "0000000000000000000111111111111111111111111111000000000000000000"
Data "0000000000000000000111111111111111111111111111000000000000000000"
Data "0000000000000000000111111111111111111111111111000000000000000000"
Data "0000000000000000000111111111111111111111111111000000000000000000"
Data "0000000000000000001111111111111111111111111111100000000000000000"
Data "0000000000000000001111111111111111111111111111100000000000000000"
Data "0000000000000000001111111111111111111111111111100000000000000000"
Data "0000000000000000001111111111111111111111111111100000000000000000"
Data "0000000000000000011111111111111111111111111111110000000000000000"
Data "0000000000000000011111111111111111111111111111110000000000000000"
Data "0000000000000000011111111111111111111111111111110000000000000000"
Data "0000000000000000111111111111111000111111111111111000000000000000"
Data "0000000000000000111111111111110000011111111111111000000000000000"
Data "0000000000000000111111111111100000001111111111111000000000000000"
Data "0000000000000001111111111110000000000011111111111100000000000000"
Data "0000000000000001111111111100000000000001111111111100000000000000"
Data "0000000000000001111111110000000000000000011111111100000000000000"
Data "0000000000000001111111100000000000000000001111111100000000000000"
Data "0000000000000011111110000000000000000000000011111110000000000000"
Data "0000000000000011111100000000000000000000000001111110000000000000"
Data "0000000000000011110000000000000000000000000000011110000000000000"
Data "0000000000000111100000000000000000000000000000001111000000000000"
Data "0000000000000110000000000000000000000000000000000011000000000000"
Data "0000000000000100000000000000000000000000000000000001000000000000"
Data "0000000000000000000000000000000000000000000000000000000000000000"
Data "0000000000000000000000000000000000000000000000000000000000000000"


Data "0000000000000000000000000000000100000000000000000000000000000000"
Data "0000000000000000000000000000001110000000000000000000000000000000"
Data "0000000000000000000000000000011111000000000000000000000000000000"
Data "0000000000000000000000000000111111100000000000000000000000000000"
Data "0000000000000000000000000001111111110000000000000000000000000000"
Data "0000000000000000000000000011111111111000000000000000000000000000"
Data "0000000000000000000000000111111111111100000000000000000000000000"
Data "0000000000000000000000001111111111111110000000000000000000000000"
Data "0000000000000000000000011111111111111111000000000000000000000000"
Data "0000000000000000000000111111111111111111100000000000000000000000"
Data "0000000000000000000001111111111111111111110000000000000000000000"
Data "0000000000000000000011111111111111111111111000000000000000000000"
Data "0000000000000000000111111111111111111111111100000000000000000000"
Data "0000000000000000001111111111111111111111111110000000000000000000"
Data "0000000000000000011111111111111111111111111111000000000000000000"
Data "0000000000000000111111111111111111111111111111100000000000000000"
Data "0000000000000001111111111111111111111111111111110000000000000000"
Data "0000000000000011111111111111111111111111111111111000000000000000"
Data "0000000000000111111111111111111111111111111111111100000000000000"
Data "0000000000001111111111111111111111111111111111111110000000000000"
Data "0000000000011111111111111111111111111111111111111111000000000000"
Data "0000000000111111111111111111111111111111111111111111100000000000"
Data "0000000001111111111111111111111111111111111111111111110000000000"
Data "0000000011111111111111111111111111111111111111111111111000000000"
Data "0000000111111111111111111111111111111111111111111111111100000000"
Data "0000001111111111111111111111111111111111111111111111111110000000"
Data "0000011111111111111111111111111111111111111111111111111111000000"
Data "0000111111111111111111111111111111111111111111111111111111100000"
Data "0001111111111111111111111111111111111111111111111111111111110000"
Data "0011111111111111111111111111111111111111111111111111111111111000"
Data "0111111111111111111111111111111111111111111111111111111111111100"
Data "0011111111111111111111111111111111111111111111111111111111111000"
Data "0001111111111111111111111111111111111111111111111111111111110000"
Data "0000111111111111111111111111111111111111111111111111111111100000"
Data "0000011111111111111111111111111111111111111111111111111111000000"
Data "0000001111111111111111111111111111111111111111111111111110000000"
Data "0000000111111111111111111111111111111111111111111111111100000000"
Data "0000000011111111111111111111111111111111111111111111111000000000"
Data "0000000001111111111111111111111111111111111111111111110000000000"
Data "0000000000111111111111111111111111111111111111111111100000000000"
Data "0000000000011111111111111111111111111111111111111111000000000000"
Data "0000000000001111111111111111111111111111111111111110000000000000"
Data "0000000000000111111111111111111111111111111111111100000000000000"
Data "0000000000000011111111111111111111111111111111111000000000000000"
Data "0000000000000001111111111111111111111111111111110000000000000000"
Data "0000000000000000111111111111111111111111111111100000000000000000"
Data "0000000000000000011111111111111111111111111111000000000000000000"
Data "0000000000000000001111111111111111111111111110000000000000000000"
Data "0000000000000000000111111111111111111111111100000000000000000000"
Data "0000000000000000000011111111111111111111111000000000000000000000"
Data "0000000000000000000001111111111111111111110000000000000000000000"
Data "0000000000000000000000111111111111111111100000000000000000000000"
Data "0000000000000000000000011111111111111111000000000000000000000000"
Data "0000000000000000000000001111111111111110000000000000000000000000"
Data "0000000000000000000000000111111111111100000000000000000000000000"
Data "0000000000000000000000000011111111111000000000000000000000000000"
Data "0000000000000000000000000001111111110000000000000000000000000000"
Data "0000000000000000000000000000111111100000000000000000000000000000"
Data "0000000000000000000000000000011111000000000000000000000000000000"
Data "0000000000000000000000000000001110000000000000000000000000000000"
Data "0000000000000000000000000000000100000000000000000000000000000000"
Data "0000000000000000000000000000000000000000000000000000000000000000"
Data "0000000000000000000000000000000000000000000000000000000000000000"
Data "0000000000000000000000000000000000000000000000000000000000000000"