Spawning Blocks and Destroying Them

Blitz3D Forums/Blitz3D Beginners Area/Spawning Blocks and Destroying Them

Shuffles(Posted 2011) [#1]
Hello Blitz3D programmers. I'm a novice to this language so all help is appreciated. I need help randomly spawning blocks on the screen. The cyclops shoots bullets and when the bullets hit the blocks I want them to disappear. This should be a pretty easy fix for experienced programmers. All help is appreciated. Here is the code:


Graphics 640, 450


;constant variables that won't change
Const uparrow = 200
Const downarrow = 208
Const leftkey = 203
Const rightkey = 205
Const spacebar = 57

;Images

stickmancyclops = LoadImage("StickmanCyclops.bmp") ; the stickman cyclops himself...
bulletimage = LoadImage("bullet.bmp")
BlockImage = LoadImage("block.bmp")
MaskImage bulletimage,0,0,0
MaskImage stickmancyclops,0,0,0 
MaskImage BlockImage,0,0,0


;The Types

Type CyclopsType
Field x,y
End Type 

Type BulletType
Field x,y
End Type 

Type BlockType
Field x,y
End Type

Global cyclops.CyclopsType = New CyclopsType 
 cyclops\x = 50
 cyclops\y = 150


SetBuffer BackBuffer()

;------------THE MAIN LOOP--------------
While Not KeyDown(1)

Cls 

If KeyHit(spacebar)
bullet.bullettype = New bulletType
bullet\x = cyclops\x+325
bullet\y = cyclops\y+50
EndIf

For bullet.bullettype = Each bullettype
bullet\y = bullet\y - 5
DrawImage bulletimage,bullet\x,bullet\y
Next  

DrawImage stickmancyclops,cyclops\x,cyclops\y
MoveCyclops() ;the function that moves the stickman cyclops...
SpawningBlocks()

Flip

Wend 

;-------THE END OF THE MAIN LOOP-----------


;Functions
Function MoveCyclops() ;moves the STICKMAN CYCLOPS!!!!

	If KeyDown(leftkey) Then cyclops\x = cyclops\x - 3 
	If KeyDown(rightkey) Then cyclops\x = cyclops\x + 3
	If KeyDown(uparrow) Then cyclops\y = cyclops\y - 3
	If KeyDown(downarrow) Then cyclops\y = cyclops\y + 3

End Function

Function SpawningBlocks();Function that's supposed to spawn blocks randomly    

Repeat 

Delay 300;I want to spawn 1 block every 3 seconds.

block.blocktype = New blocktype 
block\x = Rand(10,300)
block\y = Rand(10,300)


For block.blocktype = Each blocktype
	DrawImage blockimage,block\x,block\y 
	If ImagesCollide(blockimage,block\x,block\y,0,bulletimage,bullet\x,bullet\y,0) Then Delete block 
Next


Until KeyHit(1)
End Function 


Last edited 2011


Matty(Posted 2011) [#2]
I seem to remember answering something very similar by yourself a couple of years ago...have a look through this old code of yours from back then...it is much the same problem:

http://blitzbasic.com/Community/posts.php?topic=85550#968136


Shuffles(Posted 2011) [#3]
Yes but this time I wanted to make it using a function to spawn them. There are so many ways to make certain things happen in programming. So I try to be very creative. So I'm wondering, in this instance, how can I fix it?


Shuffles(Posted 2011) [#4]
Question is still open.


Rob the Great(Posted 2011) [#5]
Delay 300;I want to spawn 1 block every 3 seconds.


Never use Delay for that purpose. Use a timer instead. It's silly to force the program to pause everything for a third of a second just to get the timing right. I only use Delay for debug purposes, like when Blitz is too fast for me to see what's happening in every frame.

Now, for your question, you've got some strange things going on in your spawn function. You realize that because it's a loop waiting for the escape key and there are no flip commands in that loop, you won't see anything but a black screen when you run this?

Try this instead, it will work a lot better:

Function SpawningBlocks();Function that's supposed to spawn blocks randomly    

;Replace Delay with a timer here. Read up on the Millisecs() command for more info

block.blocktype = New blocktype 
block\x = Rand(10,300)
block\y = Rand(10,300)


For block.blocktype = Each blocktype
	DrawImage blockimage,block\x,block\y 
	If ImagesCollide(blockimage,block\x,block\y,0,bulletimage,bullet\x,bullet\y,0) Then Delete block
Next

End Function 


This is a rough pass, and there are a lot of things I would change. Remember, when you delete a type without freeing the image using FreeImage, it will still exist in memory, so don't be surprised if your program slows down the longer it runs. To fix this, make a new field for your types, something like /imagehandle, and load the image to that field. Then, you can free the image and delete the type when it's no longer needed.

Also, I would consider making your spawn function specific to spawning, nothing else. You've got some drawing commands in there, which should be in their own function.

For now, this is a starting point.

EDIT:

On a second look, I see that while you're creating new types in the spawn function, it doesn't appear to be copying a separate image for each new type. Honestly, I'm not sure how this program will run, but my guess is that you're still only going to see one image, just moving around crazily with the new random locations being generated.

I would say that you should study up on programming structure. That will help you so much in the long run, and once you've got a good feel for it, the rest of your game is going to be tenfold easier to manage and design.

Last edited 2011


Shuffles(Posted 2011) [#6]
Ok. Thank you.