Hi guys,
I'm trying to make a little 2d top down tank game to practice what I'm reading in J. Harbour's dbpro book.
I can't figure out why these bullet sprites wont collide with the tank sprites. If I paste the bullets stationary to the screen then they will collide with the tanks without a problem (when I drive over them with the tanks), but when I try to test for the collision between moving bullets shot from a tank, it doesn't work. I also tried using the sprite command instead of the paste sprite command in the shoot() functions I wrote, but when I do that, the sprites don't show up on the screen at all.
If you want, you can get my image files from me website derekmims.com on the sidebar link labeled tank fight. The "whole thing in a zip" and "source" links arent working right now, but you can (of course) copy and paste from here.
I also left the stationary bullets on the screen so you can run over them and see "hit" print on the top left of the screen. You can also shoot and see how nothing happens with the right ctrl button for the first tank and spacebar for the second tank. wasd and dpad for movement.
Any advice would be sincerely appreciated.
remstart
TANK FIGHT!!!!!!!!!!!!!!!!!!!!!!
September 2008
Derek Mims
with artwork from J. Harbour's 'DarkBasic Pro Game Programming, 2nd ed.'
and original artwork from Mark Bristol
remend
`////////////////////////////////////////////////////////////////////////////////////////////
`INITIALIZATION
`///////////////////////////////////////////////////////////////////////////////////////////
sync on
sync rate 40
hide mouse
randomize timer()
set display mode 1440, 900, 32
set image colorkey 255, 0, 255
`make user-defined types
type TANKtype
x as integer
y as integer
speedx as integer
speedy as integer
health as integer
damage as integer
endtype
type GUNtype
x as integer
y as integer
speedx as integer
speedy as integer
angle as integer
endtype
global spr_tank2 as word = 2
global img_tank2 as word = 2
global spr_tank1 as word = 1
global img_tank1 as word = 1
global img_bullet2 as word = 5
global img_bullet1 as word = 6
global spr_bullet2 as word = 5
global spr_bullet1 as word = 6
`declare global variables
global angle as integer = 0
global angle2 as integer = 0
global angle3 as integer = 0
global angle4 as integer = 0
global tank1 as tanktype
global tank2 as tanktype
`variables for the checkCtrl and checkSpacebar functions
global isbtndown = 0
global pressedCode = 0
global isbtndown2 = 0
global pressedCode2 = 0
global score1 = 0
global score2 = 0
`load background
load bitmap "sandlot.bmp", 4
`load and create the tank and bullet sprites
create animated sprite 1, "tank8.bmp", 8, 1, 1
clone sprite 1, 2
load image "bullet5.bmp", img_bullet2
load image "bullet5.bmp", img_bullet1
sprite spr_bullet2, 333, 333, img_bullet2
sprite spr_bullet1, 400, 400, img_bullet1
`define tank 1 and 2 initial positions and speeds
tank1.x = 200
tank1.y = 200
tank1.speedx = 10
tank1.speedy = 10
tank2.x = 1240
tank2.y = 700
tank2.speedx = 10
tank2.speedy = 10
`offsets tank origin for more realistic rotating
offset sprite 1, 15, 15
offset sprite 2, 15, 15
set current bitmap 0
`////////////////////////////////////////////////////////////////////////////////
` MAIN GAME LOOP
`////////////////////////////////////////////////////////////////////////////////
repeat
cls
copy bitmap 4, 0
ink rgb(255, 255, 255), 0
text 10, 10, get time$()
text 10, 20, "Player 1 Score: " + str$(score1)
text 10, 30, "Player 2 Score: " + str$(score2)
movetank1()
movetank2()
if checkCtrl()
shoot1()
endif
if checkSpacebar()
shoot2()
endif
checkCollisions()
sync
until (escapekey() = 1)
`/////////////////////////////////////////////////////////////////////////////////
`SUBROUTINES
`/////////////////////////////////////////////////////////////////////////////////
function movetank1()
`rotate the tank
if leftkey() then angle = wrapvalue(angle - 6)
if rightkey() then angle = wrapvalue(angle + 6)
rotate sprite spr_tank1, angle
`rotate the bullet with the tank
rotate sprite spr_bullet1, angle
if upkey()
tank1.x = tank1.x + 10*cos(angle-90)
tank1.y = tank1.y + 10*sin(angle-90)
endif
if downkey()
tank1.x = tank1.x - 10*cos(angle-90)
tank1.y = tank1.y - 10*sin(angle-90)
endif
`stop tank at edges of screen
if tank1.x < 15 then tank1.x = 15
if tank1.x > screen width() - 15 then tank1.x = screen width() - 15
if tank1.y < 15 then tank1.y = 15
if tank1.y > screen height() - 15 then tank1.y = screen height() - 15
sprite spr_tank1, tank1.x, tank1.y, img_tank1
endfunction
function movetank2()
if keystate(30) then angle2 = wrapvalue(angle2 - 6)
if keystate(32) then angle2 = wrapvalue(angle2 + 6)
`rotate the bullet with the tank
rotate sprite spr_bullet2, angle2
rotate sprite spr_tank2, angle2
if keystate(17)
tank2.x = tank2.x + 10*cos(angle2-90)
tank2.y = tank2.y + 10*sin(angle2-90)
endif
if keystate(31)
tank2.x = tank2.x - 10*cos(angle2-90)
tank2.y = tank2.y - 10*sin(angle2-90)
endif
`stop tank2 at the edges of the screen
if tank2.x < 15 then tank2.x = 15
if tank2.x > screen width() - 15 then tank2.x = screen width() - 15
if tank2.y < 15 then tank2.y = 15
if tank2.y > screen height() - 15 then tank2.y = screen height() - 15
sprite spr_tank2, tank2.x, tank2.y, img_tank2
endfunction
function shoot2()
angle3 = angle2
xstart = tank2.x
ystart = tank2.y
for i = 1 to 1700 step 100
paste sprite spr_bullet2, xstart + i*cos(angle3-90), ystart + i*sin(angle3-90)
checkCollisions()
next i
endfunction
function shoot1()
angle4 = angle
xstart2 = tank1.x
ystart2 = tank1.y
for i = 1 to 1700 step 100
paste sprite spr_bullet1, xstart2 + i*cos(angle4-90), ystart2 + i*sin(angle4-90)
checkCollisions()
next i
endfunction
function checkSpacebar()
pressedCode = spacekey()
if (pressedCode <> 1) and (isbtndown = 0) then exitfunction 0
select pressedCode
case 0:
if isbtndown = 1 then isbtndown = 0
exitfunction 1
: endcase
case 1:
isbtndown = 1
exitfunction 0
: endcase
endselect
endfunction 0
function checkCtrl()
pressedCode2 = keystate(157)
if (pressedCode2 <> 1) and (isbtndown2 = 0) then exitfunction 0
select pressedCode2
case 0:
if isbtndown2 = 1 then isbtndown2 = 0
exitfunction 1
: endcase
case 1:
isbtndown2 = 1
exitfunction 0
: endcase
endselect
endfunction 0
function checkCollisions()
if sprite collision(spr_tank1, spr_bullet2) = 1
text 10, 60, "hit"
score2 = score2 + 1
endif
if sprite collision(spr_tank2, spr_bullet1) = 1
score1 = score1 + 1
text 10, 70, "hit"
endif
endfunction
Thanks