News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

okay um collisions with images

Started by sparob, June 13, 2008, 10:04:47 PM

Previous topic - Next topic

sparob

okay i am making a game which all source looks like this loadimage "C:\Program Files\PlayBasic\Hero.jpg",1
loadimage "C:\Program Files\PlayBasic\Projects\badguy.jpg",2
loadimage "C:\Program Files\PlayBasic\Missile.jpg",3
loadimage "C:\Program Files\PlayBasic\Bullet4.jpg",4
loadimage "C:\Program Files\PlayBasic\stars.bmp",5
loadmusic "C:\Program Files\PlayBasic\mm2-wy1.mid",1
tileimage 5,640,480,false   
sprite = newsprite(BulletPosX2,BulletPosY2,3)
sprite2 = newsprite(BulletPosY,BulletPosX,4)
sprite3 = newsprite(herox,heroy,1)
sprite4 = newsprite(badguyy,badguyx,2)
badguyy = 300
badguyx = 80
herox = 500
heroy = 200
herox = 580   
    playmusic 1
          do
             ImageHitSpritePixels
          cls 255
   setfps 30
   positionsprite 1,herox,heroy
   positionsprite 2,badguyx,badguyy
   if heroy=BulletPosX2 then end
drawimage 1,heroy,herox,true
drawimage 2,badguyy,badguyx,true
if leftkey()=1 then heroy = heroy - 10
if rightkey()=1 then heroy = heroy + 10
if leftkey()=1 then badguyy = badguyy + 5
if rightkey()=1 then badguyy = badguyy - 5
if upkey()=1 then heroyy = heroyy + 5
if downkey()=1 then heroyy = heroyy - 5
If Spacekey()=1 and FiredBullet=0
    FiredBullet=1
    BulletPosX = heroy + 5
    BulletPosY = herox-16
    BulletPosY2 = badguyx - 5
    BulletPosX2 = badguyy+16
  Endif
  If FiredBullet=1
    Gosub MoveBullet
    gosub enemybullet
endif
sync   
loop
MoveBullet:
  BulletPosY=BulletPosY-20
  drawimage 3,BulletPosX,BulletPosY,false
  Rem Check to see if we hit anything here and if we did
  Rem handle it. Reset flag if bullet is done with
  If BulletPosY<=0 Then FiredBullet=0
Return
enemybullet:
  BulletPosY2=BulletPosY2+20
  drawimage 4,BulletPosX2,BulletPosY2,false
  Rem Check to see if we hit anything here and if we did
  Rem handle it. Reset flag if bullet is done with
  If BulletPosY2<=0 Then FiredBullet=0
Return




i have been looking for tuts on the bullet and the hero or player having a collision you blowing up and the game ends but unforutnaley i am so screwed i have been attempting to look for something on collision everywhere for a week but i am really stuck help is apperactied thank you -sparob

kevin

  Without the media we can't test it.  So make it easier on us to help you, by attaching something a we can easily download and run.


There's a lot of problems, but here's a few tidbits for starters.

  *  The function  ImageHitSpritePixels requires the following parameters.     

            CollisionFlag =ImageHitSpritePixels(ThisIMage,Xpos,Ypos,ThisSprite,Accuracy#)

            To use it, requires  comparing the image (at a given position) with the destination sprite of your choice.  So to test a collection of sprites such as sprite 1, sprite 4 and sprite 20,  you'd need to call it 3 times to resolve each collision separately.

            However, I suspect you could just make the Image a sprite also, then use SpriteHit and or SpriteOverlap.   There's slabs on info on these in the Help section.  As well as about 20 examples under the Projects/Examples/Sprites/_Collision in the example pack.
         

  * There seems to be some confusion about Images and Sprites in the program.  Initially you load the image media then build some sprites, but then you never draw the sprites (ie DrawAllSprites).  Rather you manually render the images.  I'd suggest using an all sprite solution. 



  * Why is SetFPS in the main loop ?  - Move it out of the main loop, there's no reason to calculate it eah refresh.



  * Both Charcter movements are connect to the same keys.


if leftkey()=1 then heroy = heroy - 10
if rightkey()=1 then heroy = heroy + 10
if leftkey()=1 then badguyy = badguyy + 5
if rightkey()=1 then badguyy = badguyy - 5


   Here, when the user presses the Left Arrow key,  then 99% of the time code will move  both the  HERO and the BadGuy.   If you want them to be both controlled by the arrow keys then it'd better to merge then into an IF/ENDIF statements.  this will ensure both traps are successful every update. 


if leftkey()=1
       heroy = heroy - 10
       badguyy = badguyy + 5
endif

if rightkey()=1
       heroy = heroy + 10
       badguyy = badguyy - 5
endif



  However, if you to use different keys for both characters, then i recommend looking into the KEYSTATE() function.  This function lets you poll the state of any key on the keyboard.  KeySTate uses scancode to determine the keys.  The PlayBAsic IDE has a scancode picker built into it.    Shift F12 to bring it up.


 
 
if leftkey()=true
       heroy = heroy - 10
endif

if rightkey()=true
       heroy = heroy + 10
endif

; check if the A key is down
if KeyState(30)=true
       badguyy = badguyy + 5

endif

; check if the S key is down
If KeyState(31)=true
       badguyy = badguyy - 5
endif