Page 1 of 1

Mouse Collision with Sprite and with Image Examples

Posted: Tue Jun 24, 2008 1:05 pm
by Rook Zimbabwe
I have been picking some of the best brains here and using a little of mine own to formulate these examples. I had planned to write a PBCards.dll with a nice shiny new deck for my new card game. I still might...

But I have had help chunking these together for anyone who wants to learn how to click on a sprite or on an image and get info.

Without having to have a declared procedure to check the Image Gadget to do so...

You will need to download the following file which has very low res images for these examples:

http://www.bluemesapc.com/Downloads/cursor.zip

I put the graphics in a folder called DECK/ where I was, you may have to modify that small bit of code.

The collision checking procedure in all these examples is called CheckBump() One of them is a bit simpler since I used CASE statements

First Example [[ SPRITE 2 SPRITE collision returning value and doing something to the sprite ]]

Code: Select all

; mouse collision with sprite 2 sprite example
; Ralph W Dunn
Enumeration
    #AS
    #BJ
    #QH
    #CURSOR
    #BACK
EndEnumeration

Global x
Global y

UsePNGImageDecoder()

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Can't open DirectX 7 or later", 0)
  End
EndIf

InitMouse()

Procedure CheckBump()
       R1 = 0
       R2 = 0
       R3 = 0 ; make sure all flags are reset
       ; *** Remember: X and Y were GLOBAL so we just check them against other sprites
       R1 = SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #AS, 180, 200)  ; ace location
       R2 = SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #BJ, 330, 200)  ; blue joker location
       R3 = SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #QH, 480, 200) ; queen hearts location
       
            If R1 <> 0
              StartDrawing(ScreenOutput())  
                DrawText(330,10,"ACE of SPADES...")
                    StopDrawing()
              DisplaySprite(#AS, 180, 200)
            EndIf
            If  R2 <> 0
                StartDrawing(ScreenOutput())  
                    DrawText(330,10,"Blue Joker...  ")
                        StopDrawing()
                DisplaySprite(#BJ, 330, 200)
            EndIf
            If R3 <> 0
                StartDrawing(ScreenOutput())  
                    DrawText(330,10,"Da Queen of HEARTS!!!")
                        StopDrawing()
                DisplaySprite(#QH, 480, 200)
            EndIf
EndProcedure

Procedure CheckBump2()
; ** same as the other using SELECT
       R1 = 0
       R2 = 0
       R3 = 0 ; make sure all flags are reset
       ; *** Remember: X and Y were GLOBAL so we just check them against other sprites
       If SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #AS, 180, 200)  ; ace location
        RR = 1
       ElseIf SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #BJ, 330, 200)  ; blue joker location
        RR = 2
       ElseIf SpriteCollision(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2, #QH, 480, 200) ; queen hearts location
        RR = 3
       EndIf
       
       Select RR
        Case 1
              StartDrawing(ScreenOutput())  
                DrawText(330,10,"LOSER...")
                    StopDrawing()
              DisplaySprite(#AS, 180, 200)
        Case 2
                StartDrawing(ScreenOutput())  
                    DrawText(330,10,"WINNER!")
                        StopDrawing()
                DisplaySprite(#BJ, 330, 200)
        Case 3
                StartDrawing(ScreenOutput())  
                    DrawText(330,10,"LOSER...")
                        StopDrawing()
                DisplaySprite(#QH, 480, 200)
       EndSelect
EndProcedure

OpenScreen(800,600, 16, "Card Game 0")

LoadSprite(#AS, "C:\Program Files\PureBasic\DEVEL\CARDZ\DECK\out.png")
LoadSprite(#BJ, "C:\Program Files\PureBasic\DEVEL\CARDZ\DECK\tap.png")
LoadSprite(#QH, "C:\Program Files\PureBasic\DEVEL\CARDZ\DECK\out.png")
LoadSprite(#CURSOR, "C:\Program Files\PureBasic\DEVEL\CARDZ\DECK\cursor.png");, #PB_Sprite_Alpha)   ; Load nice small Logo 32 X 32
LoadSprite(#BACK, "C:\Program Files\PureBasic\DEVEL\CARDZ\DECK\b1.png")

Repeat

FlipBuffers()
ClearScreen(RGB(0,0,0))

DisplaySprite(#BACK, 180, 200)
DisplaySprite(#BACK, 330, 200)
DisplaySprite(#BACK, 480, 200)

ExamineMouse()
ExamineKeyboard()
  
x = MouseX()
y = MouseY()

    DisplayTransparentSprite(#CURSOR, x-SpriteWidth(0)/2, y-SpriteHeight(0)/2)
    Result = 0
    If MouseButton(#PB_MouseButton_Left) 
        ;CheckBump()
        CheckBump2()
   EndIf

  Until KeyboardPushed(#PB_Key_Escape)
        
End
Second Example uses ImageGadgets... in a windowed screen and still returns values:

Code: Select all

; mouse collision with image example
; Ralph W Dunn

Global outchunk$

UsePNGImageDecoder()

#J= "DECK\"
DataSection
  Image1:
  IncludeBinary #J + "out.png"
  Image2:
  IncludeBinary #J + "tap.png"
  Image3:
  IncludeBinary #J + "out.png"
  Image4:
  IncludeBinary #J + "b1.png"
EndDataSection

Global Dim cardpos(3,3)
Global Dim cards.l(3) ;holds imageId's indexed by card#
Global Dim cardid.s(3)

cards(0)= CatchImage(#PB_Any, ?Image1) ; Ace Spades
cards(1)= CatchImage(#PB_Any, ?Image2)
cards(2)= CatchImage(#PB_Any, ?Image3)
Image4 = CatchImage(#PB_Any, ?Image4)

;arrange cards
cardPos(0,0) = 1 :cardPos(0,1) = 180 :cardPos(0,2) = 150 : cardid(0) = "YOU LOOSE"
cardPos(1,0) = 2 :cardPos(1,1) = 330 :cardPos(1,2) = 150 : cardid(1) = "WINNER"
cardPos(2,0) = 3 :cardPos(2,1) = 480 :cardPos(2,2) = 150 : cardid(2) = "LOOSER"

Procedure CheckBump(XX,YY)
  For h = 0 To 2
  Debug "reading: "+Str(h)
  cardid = cardPos(h,w)
    Xloc = cardPos(h,w+1)
    Yloc = cardPos(h,w+2)
    cardid$ = cardid(h)
    Select XX
        Case Xloc To Xloc + 140
            Select YY
                Case Yloc To Yloc + 200
                    Debug  "CARD: "+Str(cardid)
                    Debug "** THAT CARD IS "+cardid$
                     StartDrawing(WindowOutput(0))
                        DrawImage(ImageID(cards(h)), Xloc, Yloc)
                        DrawText(350,10,"That Card: "+cardid$)
                     StopDrawing()
                     Delay(350) ; necessary to see the card
            EndSelect
    EndSelect
  Next 
EndProcedure

OpenWindow(0, 0, 0, 800, 600, "CARD Drawing Test")

Repeat

 StartDrawing(WindowOutput(0))
 DrawText(350,10,"                                      ") ; i am far too lazy to clear the screen
  For Crd = 0 To 2
    DrawImage(ImageID(Image4), cardPos(Crd,1), cardPos(Crd,2))
  Next
 
StopDrawing()
 
  event  =  WaitWindowEvent() ; WaitWindowEvent(22)
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()

  Select Event
    Case #PB_Event_Gadget
      Debug "Event: "+Str(#PB_Event_Gadget)
    Case #PB_Event_Menu 
       Debug "Menu: "+Str(#PB_Event_Menu)
    Case  #WM_LBUTTONDOWN 
        Debug "LM button pressed..."
        xx = WindowMouseX(0)
        yy = WindowMouseY(0)
            checkbump(xx,yy)
        
  EndSelect

Until Event = #PB_Event_CloseWindow

End
Note that this one has two different CheckBump() examples to use... I wasn't getting fancy; I simply like to use Select Case if I can... it seems in my mind to execute faster!

8)

What I would love to do is used a Structure or Linked List to both check collision and get info... I have to learn how to use that!