hi,
I am having trouble with a text game I am working on. I am creating it using blitz basic.
the game is basically a space invaders clone.
i am going to paste my code below my question
heres my problem, i have created two type statements, one for bullets and one for enemies(blocks). i the used to seperate functions to create them both and draw them to the screen. I want to check for collisons between the two but when i try to check for any collision between the two i get a memory access violation so i am assuming that they are both not in existance at the same time? i know this is a really vague description but i am having trouble understanding the problem. Thank you for any help.
- webfaction
Graphics 640,480,0,0 Set graphics mode and denote resoultion
SetBuffer BackBuffer() ;Set the backbuffer
;-------------------------------------------------------------------------------------------------------------------------------------
Global ship$="}" ;The string that will be used to represnt the ship
Global shipx=0 ;The ships x cord variable
Global shipy=0 ;The ships y cord variable
Global xspeed=2 ;The ships x speed variable
Global yspeed=2 ;The ships y speed variable
Global bspeed=10 ;The bullets speed variable
;-------------------------------------------------------------------------------------------------------------------------------------
Type block ;Start The block type
Field x ;The blocks x variable
Field y ;The blocks y variable
Field i$ ;The blocks image placeholder variable
Field h ;The variable that represents weather the block hit an object
End Type ;End type statement
;-------------------------------------------------------------------------------------------------------------------------------------
Type bullet
Field x
Field y
Field h
Field i$
Field s
Field f
End Type
;-------------------------------------------------------------------------------------------------------------------------------------
Function ship() ;Function that handles all ship actions
If KeyDown(208) Then shipy=shipy + yspeed ;If the down arrow is pressed move ship down
If KeyDown(200) Then shipy=shipy - yspeed ;If the up arrow is pressed move ship up
If KeyDown(203) Then shipx=shipx - xspeed ;If the left arrow is pressed move ship left
If KeyDown(205) Then shipx=shipx + xspeed ;If the right arrow is pressed move ship right
End Function ;Close Function
;-------------------------------------------------------------------------------------------------------------------------------------
Function draw() ;Function that draws everything to the screen
Text shipx,shipy,ship$ ;Draws the player to screen
End Function ;Close Function
;-------------------------------------------------------------------------------------------------------------------------------------
Function createblock() ;Function that handles the creation of the blocks
block.block= New block ;Create new block
block\x=400 ;Set blocks x variable
block\y= block\y+10 ;Set blocks y variable
block\i$="[]" ;Set the blocks Image placeholder value
block\h=0
For block.block = Each block ;Starts loop that goes through each block created
block\y=block\y+15 ;Adds 15 to the block\y value
Text block\x,block\y,block\i ;Draws the block at its new position
t=t + 1 ;Temp value = temp value + 1
If block\y>300 Then ;If the blocks hit bottom screen restart at the top
block\x=block\x-20 block\y=0 ;Move the starting x position over 20
End If ;Ends the current statement
Next ;Ends the loop for the blocks
End Function ;Close Function
;-------------------------------------------------------------------------------------------------------------------------------------
Function bullet() ;Function that Handles all bullet aspects
bullet.bullet= New bullet
bullet\x=0
bullet\y=shipy
bullet\i$="."
bullet\h=0
bullet\s=5
bullet\f=0
If KeyHit(57) Then bullet\f=1
For bullet.bullet = Each bullet
If bullet\f=1 Then
bullet\x=bullet\x+ bullet\s
Text bullet\x,bullet\y,bullet\i$
End If
Next
if bullet\x > block\x then end
****( the above is the line i am using just to see that both are recognized if the bullet passes the block, i have tried placing it in several places but with no luck, i think its because the variables are both local and not global but they are in functions so i can't make them global. is there a way i can get a collision test to work without getting rid of the functions and making everything global?
End Function ;Close Function
;-------------------------------------------------------------------------------------------------------------------------------------
Function collide() ;Function that handles all collisions
End Function ;Close Function
;-------------------------------------------------------------------------------------------------------------------------------------
While KeyDown(1)=0 ;Start the Main Game Loop(MGL)
Cls ;Clears screen
createblock ;Calls the Function to create blocks
ship ;Calls the Function that controls all ship aspects
draw ;Calls the Function to draw the game
bullet ;Calls the Function that handles the bullet
collide ;Calls the Function that handles game collisons
Flip ;Flip everythingthe backbuffer to the front and draw to screen
Wend ;End the MGL
;-------------------------------------------------------------------------------------------------------------------------------------
edit : hartnell : code formatting fixed.