Main Menu

Space Invaders Help?

Started by Scott_Bro, November 23, 2020, 07:35:45 AM

Previous topic - Next topic

Scott_Bro

Here is the start of my version of space invaders.
Can anyone see how I can reduce the size of it so far?
It's not done but I am open to optimizations at this point.
It does not include everything yet but I am wondering...
How can I do this in blocks of code?
Please feel free to have a look and help if you can.
Thanks,
Scott B.

kevin


Scott_Bro

Does anyone know where on earth I can get some descent 2D-Graphics for this game???
It needs some eye candy bad.  I'm not a graphic art designer through I know lots of formulas.
There graphics are great but primitive to original game which is cool but I looked all around and find much but...
I would love it if someone else could help here. During the making of monstarz this kinda thing was rectified.

Kevin, Can you explain to me pixel-perfect collision detection with a code snippet too please?

Thanks,
& Happy Computing!
Scott B.

kevin

#3
  Set the sprite Collision mode to 6.  Then when you check for collisions it'll compare the regions and if they overlap it checks the pixels.



  links:

    * Manually Doing Pixel Perfect Sprite Collisions (Examples & Video)

kevin

#4
 Sprite Collision - Pixel Perfect - Mode 6
Kev Picone / PlayBASIC.com



I've mocked up a sprite scene that uses your sprite sheet.  There's a couple of things about that image,  the main one is the maskcolour isn't colour zero, but rather it's ARGB(255,0,0,0)  so the Alpha channel is set to full on every pixel.    In the code bellow, I choose to mask that alpha channel out completely (rgbmaskimage img, $00ffffff ) so colour zero is used when drawing a transparent sprite and for collision.  

Just on collision I did noticed some oddities with collision when not using a mask colour of zero in sprite collision mode 6.   Not sure what the go with that is, feels buggy..





PlayBASIC Code: [Select]
   // -------------------------------------------------------------
// -------------------------------------------------------------
// ----------------------->> LOAD MEDIA <<----------------------
// -------------------------------------------------------------
// -------------------------------------------------------------

Dim SpriteFrames(0)


LoadSpaceInvadersImageStripToArray("sprite_sheet_1.bmp", SpriteFrames())


for lp=1 to SpriteFrames(0)
drawimage SpriteFrames(lp),0,(lp-1)*16,true
next

// -------------------------------------------------------------
// -------------------------------------------------------------
// ------------->> RANDOM SPRITE SCENE SETUP <<-----------------
// -------------------------------------------------------------
// -------------------------------------------------------------

Frames =SpriteFrames(0)
For lp =0 to 10

ThisImage = SpriteFrames(int(rndrange#(1,frames)))


Sprite=CreateInvaderSprite( rnd(799),rnd(600), ThisImage)
if Sprite
ScaleSprite Sprite, rndrange#(1,5)

endif
next



//




// -------------------------------------------------------------
// -------------------------------------------------------------
// ------------->> MAIN LOOP <<-----------------
// -------------------------------------------------------------
// -------------------------------------------------------------


UserSprite = CreateInvaderSprite( 0,0, SpriteFrames(9))


do

cls $113355

// position user sprite at the mouse position this frame
mx=mousex()
my=mousey()
positionSprite UserSprite,mx,my


// Check if our user sprite hit another sprite ?
ImpactSprite= SpriteHit(UserSprite,getFirstSprite(), $ffff)

// Draw the impact sprite (if any)
print ImpactSprite


// Draw All the sprites in the sprite list
drawallsprites


// Flip Back buffer to front.. (Show this newly rendered frame)
sync


loop Spacekey()=true

end



// -------------------------------------------------------------
// -------------------------------------------------------------
// ------------>> UTILITY FUNCTIONS <<-----------------
// -------------------------------------------------------------
// -------------------------------------------------------------



Function CreateInvaderSprite( Xpos,Ypos, ThisImage)

// Check if this image exists ??
if GetImageStatus(ThisImage)

// Create a new sprite with this image at this position
// in 2d space
Sprite=NewSprite(Xpos,Ypos,ThisImage)

// Turn on sprite rotation draw mode
SpriteDrawMode Sprite, 2


// Set sprite handle to middle of image
CenterSpriteHandle Sprite

// Turn Collision On
SpriteCollision Sprite, On

// Set Collision Mode to Pixel Perfect
SpriteCollisionMode Sprite,6

endif

EndFunction Sprite



// -------------------------------------------------
// -------------------------------------------------
// -------------------------------------------------
// Load this sheet into our frames array above
// -------------------------------------------------
// -------------------------------------------------
// -------------------------------------------------

function LoadSpaceInvadersImageStripToArray(Filename$, ThisArray())
if FileExist(Filename$)
img=LoadNewImage(Filename$,2)

if getimagestatus(img)

// CLEAN OUT THE ALPHA CHANNEL and KEEP the R,G,B channels
// of the source image
rgbmaskimage img, $00ffffff
frames = getimagewidth(img)/32
Dim ThisArray(frames)
OldSurface = getsurface()
rendertoimage img
for xlp=0 to GetimageWidth(img)-1 step 32
Image=GetFreeimage()
getimage Image,Xlp,0,Xlp+32,16
Index++
ThisArray(Index)=Image
next
rendertoimage OldSurface
Login required to view complete source code




  Download


  Attached project & media files

kevin

#5
PlayBASIC LIVE -  Sprite Collision (Pixel Perfect) Mode 6 - (2020-12-23)

  Today we review some example source  code written to help Scott lon his journey to build his very own Space Invaders clone.   The video  shows how to that set up and performs pixel perfect collision using SpriteCollision Mode 6.


 
Video:





 
Sprite Collision Links:



  Manually Doing Pixel Perfect Sprite Collisions (Examples & Video)
  https://www.underwaredesign.com/forums/index.php?topic=4432.0



Credits:


     Video By:
  Kevin Picone
  http://playbasic.com  
  https://underwaredesign.com

   Music:
 Spirit of Fire  by  Jesse Gallagher
 Mer-Ka-Ba  by  Jesse Gallagher





PlayBASIC LIVE PLAYLIST
https://www.youtube.com/playlist?list=PL_dvm0gvzzIVGlAhx34N6z2ce0ffMMOZ8


PlayBASIC BLOG PLAYLIST
https://www.youtube.com/playlist?list=PL_dvm0gvzzIU0Hnr6veV5UvwkSapHCo1J

PlayBASIC on FACEBOOK
http://www.facebook.com/pages/PlayBasic/127905400584274   (Facebook Page)

PlayBASIC on TWITTER
https://twitter.com/PlayBasic



#SpriteCollision #SpaceInvaders #PixelPerfect #PlayBASIC #coding #basic #programming #sourcecode