Page 1 of 2

n00b sprite related issues

Posted: Sun Oct 02, 2005 5:18 am
by wolfwood2x
Im still learning PB and have only been messing with it since Friday. That being said i am working on a pac man clone with a slight twist in relation to a co-worker. The problem I am having is with sprite collision and score keeping . However for the sake of making this simple i want to be able to collide with the pellets to eat them have them dissapear and accrue points.


My original code looked like
"DisplayTransparentSprite(1001,100,100)
if SpriteCollision(1, imagex, imagey, 1001, 100, 100)<>0
freesprite(1001)
endif"

which crashed the program since its in part of a loop and it cant display what doesnt exist. Next i tried this:

"While SpriteCollision(1, imagex, imagey, 1001, 100, 100)<>0
DisplayTransparentSprite(1001,100,100)
Wend


If SpriteCollision(1, imagex, imagey, 1001, 100, 100)<>0
points = points + 10
StartDrawing(ScreenOutput())
DrawingMode(1)
DrawingFont(UseFont(1))
FrontColor(255,255,255):Locate(400,400):DrawText(Str(points))
StopDrawing()
EndIf
"


and the problem here is that the while statement causes my screen to go black when i try to run and my pc hangs for several minutes until i alt-tab alt-f4 repeatedly. If i comment it out the program runs fine just doesnt do what i want it to. I can post all my code if that will help i just didnt want to stick 100 lines in here if i didnt need to. Can anyone help me with this?

Posted: Sun Oct 02, 2005 5:36 am
by wolfwood2x
Well Now that i see the code option here im gonna post the whole thing so its easier to trouble shoot.

Code: Select all

imagex.l=384
imagey.l=384
points.l=0
b1.w=0

If InitSprite() = 0 Or InitKeyboard() = 0
 MessageRequester("error", "Can't open", 0)
 End
EndIf

If OpenScreen(1024,768,16,"test")
    UseJPEGImageDecoder()
    
    ;--Sprite Loader--
    LoadSprite(0,"background.jpg",0)
    LoadSprite(1,"char.jpg",0)
    LoadSprite(1000, "smburger.jpg", 0)
    LoadSprite(1001, "smburger.jpg", 0)
    LoadFont(1,"Arial",10,#PB_Font_Underline)
      
      ;--Game Engine--  
      Repeat
         FlipBuffers()
         ClearScreen(0,0,0)
         ExamineKeyboard()
         
          If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
            Gosub Move_left
          EndIf
          
          If KeyboardPushed(#PB_Key_D) Or KeyboardPushed(#PB_Key_Right)
            Gosub Move_right
          EndIf

          If KeyboardPushed(#PB_Key_W) Or KeyboardPushed(#PB_Key_Up)
            Gosub Move_up
          EndIf
          
          If KeyboardPushed(#PB_Key_S) Or KeyboardPushed(#PB_Key_Down)
            Gosub Move_down
          EndIf
        
        Gosub Update_back 
        Gosub Update_char
        Gosub Burger_count
        Gosub Check_score
            
      
      Until KeyboardPushed(#PB_Key_Escape)
EndIf

End

;--Update Screen Functions--

Update_char:
   DisplaySprite(1,imagex, imagey)
Return

Update_back:
   DisplaySprite(0,0,0)
Return

Burger_count:                ; Burger checking sub (may need array here)
    DisplayTransparentSprite(1000,150,100) 
    ;While SpriteCollision(1, imagex, imagey, 1001, 100, 100)<>0
          DisplayTransparentSprite(1001,100,100)
    ;Wend
    
      
    If SpriteCollision(1, imagex, imagey, 1001, 100, 100)<>0
      points = points + 10 
    EndIf
Return
    


;--Movement--
Move_left:
   Imagex= imagex-1
   DisplaySprite(1, imagex, imagey)
 Return
 
Move_right:
   Imagex= imagex+1
   DisplaySprite(1, imagex, imagey)
Return

Move_up:
    Imagey= imagey-1
    DisplaySprite(1, imagex, imagey)
Return

Move_down:
    Imagey= imagey+1
    DisplaySprite(1, imagex, imagey)
Return


;--Scoring Algorithm--
Check_score:

        StartDrawing(ScreenOutput()) 
          DrawingMode(1)
          DrawingFont(UseFont(1)) 
          FrontColor(255,255,255):Locate(400,400):DrawText("Score: " + Str(points)) 
        StopDrawing()
Return

Posted: Sun Oct 02, 2005 11:52 am
by Comtois

Code: Select all

imagex.l=384
imagey.l=384
points.l=0
b1.w=0

Structure POINT
X.l
Y.L
EndStructure

NewList Burger.Point()

AddElement(burger())
Burger()\x=150
Burger()\y=100

AddElement(burger())
Burger()\x=100
Burger()\y=100

If InitSprite() = 0 Or InitKeyboard() = 0
 MessageRequester("error", "Can't open", 0)
 End
EndIf

If OpenScreen(1024,768,16,"test")
    UseJPEGImageDecoder()
   
    ;--Sprite Loader--
    CreateSprite(1,32,32)
    StartDrawing(SpriteOutput(1))
    Box(0,0,32,32,RGB(255,255,0))
    StopDrawing()
   
   
    CreateSprite(1000,32,32)
    StartDrawing(SpriteOutput(1000))
    Circle(16,16,16,RGB(50,255,50))
    StopDrawing()
   
    LoadFont(1,"Arial",10,#PB_Font_Underline)
     
      ;--Game Engine--
      Repeat
         FlipBuffers()
         ClearScreen(0,0,0)
         ExamineKeyboard()
         
          If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
            Imagex - 1
          EndIf
         
          If KeyboardPushed(#PB_Key_D) Or KeyboardPushed(#PB_Key_Right)
            Imagex + 1
          EndIf

          If KeyboardPushed(#PB_Key_W) Or KeyboardPushed(#PB_Key_Up)
            Imagey - 1
          EndIf
         
          If KeyboardPushed(#PB_Key_S) Or KeyboardPushed(#PB_Key_Down)
            imagey + 1
          EndIf
       

       DisplaySprite(1,imagex, imagey)
        Gosub Burger_count
        Gosub Check_score
           
     
      Until KeyboardPushed(#PB_Key_Escape)
EndIf

End



Burger_count:                ; Burger checking sub (may need array here)
ForEach  Burger()
    DisplayTransparentSprite(1000,burger()\x,Burger()\y)      
    If SpriteCollision(1, imagex, imagey, 1000, burger()\x, burger()\y)
      points + 10
      DeleteElement(Burger())   
      Continue   
    EndIf
Next     
Return
   

;--Scoring Algorithm--
Check_score:

        StartDrawing(ScreenOutput())
          DrawingMode(1)
          DrawingFont(UseFont(1))
          FrontColor(255,255,255):Locate(400,400):DrawText("Score: " + Str(points))
        StopDrawing()
Return

Posted: Sun Oct 02, 2005 3:26 pm
by wolfwood2x
that doesnt seem to work, debugger errors on line 6 with the
list burger.point but youve at the least given me a direction to try and made me realize i had a lot of unneccesary code. So for that i thank you.

On a side note is it possible the reason the pointer list didnt work is that i am using the demo compiler?

Posted: Sun Oct 02, 2005 4:34 pm
by Comtois
yes with demo version , you need to add this before line 6.

Code: Select all

Structure Point
x.l
y.l
EndStructure

Posted: Sun Oct 02, 2005 10:39 pm
by wolfwood2x
oh my thats embarassing ^_^ thanks for pointing that out haha

Posted: Mon Oct 03, 2005 12:53 am
by wolfwood2x
any tips on sprite collision for the background?
it is after all a maze with limits
I tried setting sprite collision for the background at certain coords but it seems to interpret any collision of the background and character sprite as one meaning its in a constant state of collision.

Posted: Mon Oct 03, 2005 12:11 pm
by MadMax
any tips on sprite collision for the background?
it is after all a maze with limits
a simple way is to have an image the same size as the background with the walls drawn on it, you don't need to display this image. Just use it to check collisions. (the bits without wall have to be maskedColor obviously).

Posted: Mon Oct 03, 2005 4:46 pm
by wolfwood2x
great idea thanks!!

Posted: Tue Oct 04, 2005 3:12 am
by wolfwood2x
still seems to be in a constant state of collision, how do i cut out the parts that are not part of the wall

(the bits without wall have to be maskedColor obviously).

im still a n00b at this so can you elaborate a bit? I have cut away all the un needed parts but how do i make it masked color? I am sorry if these are stupid questions. I am still learning

Posted: Tue Oct 04, 2005 3:07 pm
by wolfwood2x
I tried doing a cut out of the walls and putting them on a transparent background saving it to PNG format since jpeg wouldnt preserve the transparency. I tried adding a layer mask and doing the entire layer transparent but the problem there is the walls become invis as i place them so i cant tell if everything is lined up properly. The end result is still a constant state of collision.

I know im just going about this the wrong way so if a more educated user than myself could once again point me in the right direction I would appreciate it. Thanks again you guys are the best

Posted: Wed Oct 05, 2005 5:25 pm
by wolfwood2x
I am still having trouble with this if anyone knows the solution.
I have 1 image at 1024x768 that is the visible background image, and one 1024x768 image that has the walls on it and nothing else. Everything that is not a wall is part of the alpha channel. This image is loaded but not displayed on screen. When i try to use it to check for sprite collisions it seems to always think its in a collsion.
It seems like the only way to do it would be to take several small peices of the wall and position them on the map 1 at a time which would mean a lot more code and would be very very tedious. Can anyone help me here?

Posted: Wed Oct 05, 2005 11:26 pm
by MadMax
Leave the non-wall parts of the image pure black rgb(0,0,0) and to check collisions use SpritePixelCollision(#Sprite1, x1, y1, #Sprite2, x2, y2).

I've used this b4 so I know it works, I'll try and make an example, but you'll have to wait until Friday for that cos I'm very busy at the moment.

Posted: Thu Oct 06, 2005 12:47 am
by wolfwood2x
Thats what I did. Still is in a constant state of collision. The actual background is black though so does that have anything to do with it?
so the sections without the walls are the same color as the background
they are both 0,0,0 the walls are a light blue color and even if i am over 400 pixels away from one of the walls it says there is a collisionso id love to see one of your examples on friday.



EDIT

Oh i did

Code: Select all

Spritecollsion 
instead of spritepixelcol....
id still love to see your example if you can post it

Posted: Thu Oct 06, 2005 4:23 am
by dracflamloc
You should buy purebasic ;)