rem Init
disable escapekey
#constant false 0
#constant true 1
global BulletD : BulletD = timer() + BulletDelay
global scrhgt : scrhgt = screen height()
global scrwid : scrwid = screen width()
global paused = false
global BulletDelay = 200
sync on : sync rate 100
backdrop off
color backdrop rgb(0,0,0)
set window title "Zombie Game"
randomize timer()
rem Types
type entity
x, y, id, hitsReq, hits, lives, amount, delay, health, state as integer
endtype
type shot
x, y, id, state, amount as integer
endtype
rem Globals
player as entity
player.id = 1
dim enemy(5) as entity
for b = 1 to 5
enemy(b).amount = 5
next b
dim bullets(6) as shot
for b = 1 to 6
for y = 17 to 23
bullets(b).state = false
bullets(b).id = y
next y
next b
global gamestate = 0
global enemydelay = 5000
global Htmr
global desttime
global currbullet = 6
global currenemy = 1
global char=1
global bullet=2
global GameOver=3
global enemyimg=11
global healthbackground=12
global healthbar=13
global healthbarS=24
global avbl=0
global roundDelay=20000
global roundTimer=0
global debug = false
dim MM(4)
for mm = 1 to 4
MM(mm) = mm + 5
next mm
dim MMS(4)
for n = 1 to 4
MMS(n) = n + 8
next n
gosub LoadImages
game:
gosub SetupPlayer
gosub SetupEnemies
mainloop:
rem Main loop
do
select gamestate
case 0
gosub mainmenu
endcase
case 1
rem Init area
if player.state = false
fadeIn(player.id, 500, 0, 0)
roundTimer=timer()+roundDelay
player.state = true
else
playerinput()
if enemymovedelay => 10
MoveEnemies()
enemymovedelay = 0
else
inc enemymovedelay
endif
for b = 1 to 5
if timer() > roundTimer
dec enemydelay, 500
inc roundDelay, 5000
inc enemy(b).hitsReq, 1
gamestate = 3
center text scrwid/2, scrhgt/2, "ROUND OVER!"
hide all sprites
sync
wait 10000
cls
roundTimer = timer() + roundDelay
gosub mainloop
else
`inc roundTimer
endif
next b
TrackStats()
movebullet()
enemytimer()
check()
for b = 1 to 5
if sprite collision(enemy(b).id, player.id) = true
if enemy(b).delay < timer()
dec player.health, 1
enemy(b).delay = timer() + 200
else
inc enemy(b).delay
endif
endif
next b
endif
ink rgb(255,255,255)
sync
endcase
case 2
open to read 1, "save.dat"
rem loading stuff goes here.
endcase
case 3 `Round Over, save and get upgrades.
rem Save.
center text scrwid/2, scrhgt/2, "This is the upgrade menu. Expect more."
if file exist("save.dat") = true then delete file "save.dat"
make file "save.dat"
open to write 1, "save.dat"
`write long 1, player
endcase
endselect
loop
end
rem Main Menu
mainmenu:
alpha2 = 255 : alpha3 = 255 : alpha4 = 255
sprite MMS(1), 0, 0, MM(1)
set sprite priority MMS(1), 0
sprite MMS(2), 284, 320, MM(2): // exit
set sprite priority MMS(2), 1
sprite MMS(3), 266, 221, MM(3): // load
set sprite priority MMS(3), 1
sprite MMS(4), 283, 138, MM(4): // new
set sprite priority MMS(4), 1
ink rgb(255,255,0),0
do
mx = mousex() : my = mousey() : mc = mouseclick()
if mx => 276 and mx =< 353 and my => 314 and my =< 353
if mc = 1
for mmss = 1 to 4
hide sprite MMS(mmss)
next mmss
end : // exit
else
dec alpha2,5
if alpha2 < 0 then alpha2 = 255
set sprite alpha MMS(2), alpha2
endif
else
alpha2 = 255
set sprite alpha MMS(2), alpha2
endif
if mx => 264 and mx =< 362 and my => 219 and my =< 282
if mc = 1
for mmss = 1 to 4
hide sprite MMS(mmss)
next mmss
gamestate = 2
gosub mainloop
else
dec alpha3,5
if alpha3 < 0 then alpha3 = 255
set sprite alpha MMS(3), alpha3
endif
else
alpha3 = 255
set sprite alpha MMS(3), alpha3
endif
if mx => 271 and mx =< 381 and my => 134 and my =< 191
if mc = 1
gamestate = 1
for mmss = 1 to 4
hide sprite MMS(mmss)
next mmss
gosub mainloop
else
dec alpha4, 5
if alpha4 < 0 then alpha4 = 255
set sprite alpha MMS(4), alpha4
endif
else
alpha4 = 255
set sprite alpha MMS(4), alpha4
endif
sync
loop
return
LoadImages:
rem Make images/Load images
circle 10,10,10
get image char, 0, 0, 22, 22
cls
ink rgb(25,250,10)
circle 10,10,10
get image enemyimg, 0, 0, 22, 22
ink rgb(255,255,255)
cls
box 0,0,112,30
ink rgb(10,10,10),0
box 1,1,111,29
get image healthbackground,0,0,112,30,1
cls
ink rgb(255,0,0),0
box 0,0,110,28
get image healthbar,0,0,110,28,1
cls
set text font "Impact"
set text size 50
ink rgb(255,255,255)
center text screen width()/2, screen height()/2, "Game Over!"
center text screen width()/2, screen height()/3, "Press 'escape' to exit!"
get image GameOver,0,0,640,480
cls
set text font "Ariel"
set text size 15
load bitmap "media\Main_Menu.png", 1 `Main Menu
get image MM(2), 284, 354, 348, 383 `Exit Button
get image MM(3), 266, 274, 356, 333 `Load Game Button
get image MM(4), 283, 208, 380, 258 `New Game Button
delete bitmap 1
load image "media\Main_Menu_Blank.png", MM(1)
load image "media\Bullet.png", bullet
for b = 1 to 6
bullets(b).id = 16 + b
next b
return
SetupPlayer:
sprite player.id, screen width()/2, 400, char
player.health = 100
player.lives = 3
player.x = sprite x(player.id)
player.y = sprite y(player.id)
player.state = false
return
SetupEnemies:
px = player.x : py = player.y
for b = 1 to 5
enemy(b).id = b + 3
okay = false
repeat
x = rnd(scrwid - 10) : y = rnd(scrhgt - 500)
if abs(px - x) > 30 and abs(py - y) > 30 then okay = true : // be sure it is a ways away from player
until okay = true
enemy(b).x = x-10 : enemy(b).y = y-25
enemy(b).state = true : // active
enemy(b).hits = 0 : enemy(b).hitsReq = 1
sprite enemy(b).id, enemy(b).x, enemy(b).y, enemyimg
next b
return
rem Functions
function playerinput()
` Check for right arrow
if rightkey() = 1
player.x = sprite x(player.id)
if player.x => 617
player.x = sprite x(player.id)
else
player.x = player.x + 2
endif
endif
` Check for left arrow
if leftkey() = 1
player.x = sprite x(player.id)
if player.x =< 3
player.x = sprite x(player.id)
else
player.x = player.x - 2
endif
endif
if timer() > bulletD
if spacekey() = 1 and currbullet > 0
avbl = 0
for b = 1 to 6
if bullets(b).state = false and avbl = 0 then avbl = b
next b
if avbl > 0
bullets(avbl).state = true
bullets(avbl).x = sprite x(player.id)+7
bullets(avbl).y = sprite y(player.id)-10
sprite bullets(avbl).id, bullets(avbl).x, bullets(avbl).y, bullet
show sprite bullets(avbl).id
dec currbullet
endif
endif
bulletD = timer() + BulletDelay
endif
if keystate(19) = 1 `The R button
currbullet = 6
endif
` Show the player (after the x and y may have changed)
sprite player.id, player.x, player.y, char
endfunction
function check() `Checks a whole load of things.
if player.health =< 0
dec player.lives
if player.lives <= 0
fadeOut(player.id, 500, 255)
hide all sprites
sprite GameOver, 0, 0, GameOver
fadeIn(GameOver, 500, 0, 0)
do
cls
sprite GameOver, 0, 0, GameOver
if escapekey() = 1 then end
sync
loop
endif
fadeOut(player.id, 0, 255)
sprite player.id, screen width()/2, 400, char
player.x = screen width()/2
player.y = 400
fadeIn(player.id, 500, 0, 1)
endif
if escapekey() = 1
wait 0150
if paused = false
paused = true
repeat
cls
center text screen width()/2, screen height()/2, "Paused!"
TrackStats()
sync
if escapekey() = 1
paused = false
TrackStats()
wait 0150
endif
until paused = false
endif
endif
set text size 5
if currbullet = 0 then center text scrwid/2, scrhgt/2, "PRESS R TO RELOAD!"
set text size 20
endfunction
rem Decides when an enemy should enter play
function enemytimer()
if timer() > desttime
for b = 1 to 5
if enemy(b).state = false
currenemy = b
enemy(currenemy).x = rnd(600)+20
enemy(currenemy).y = 0 - 16
enemy(currenemy).state = 1
sprite enemy(currenemy).id, enemy(currenemy).x, 0 - 16, char
show sprite enemy(currenemy).id
desttime = timer() + enemydelay
endif
next b
endif
endfunction
rem Handles bullets.
function movebullet()
for b = 1 to 5
if bullets(b).state = true
x = sprite x(bullets(b).id)
y = sprite y(bullets(b).id)
if x < 0 or x > ScrWid then DisableBullet(b)
if y < 0 or y > ScrHgt then DisableBullet(b)
dec bullets(b).y, 8
sprite bullets(b).id, bullets(b).x, bullets(b).y, bullet
for a = 1 to 5
if sprite collision(bullets(b).id, enemy(a).id) = true and enemy(a).state = true
` Increase the number of hits here.
` Checking later would let the enemy survive two hits and die on the third.
inc enemy(a).hits
if enemy(a).hits => enemy(a).hitsReq
hide sprite enemy(a).id : enemy(a).state = false : dec enemy(a).amount
enemy(a).hits = 0
endif
` Disable the bullet if it hit an enemy, not if it killed it.
DisableBullet(b)
endif
next a
endif
next b
endfunction
function DisableBullet(c)
hide sprite bullets(c).id
bullets(c).state = false
endfunction
function MoveEnemies()
for b = 1 to 5
for a = 1 to 5
if enemy(b).state = true and a <> b
if ( abs(enemy(a).y - enemy(b).y) < sprite height(enemy(b).id)+5 ) and ( abs(enemy(a).x - enemy(b).x) < sprite width(enemy(b).id)+5 )
if enemy(a).y > enemy(b).y - 5
dec enemy(b).y, 1
else
inc enemy(b).y, 1
endif
if enemy(a).x > enemy(b).x - 5
dec enemy(b).x, 1
else
inc enemy(b).x, 1
endif
else
if abs(enemy(b).y - player.y) > 4 and enemy(b).state = true
if enemy(b).y => player.y
dec enemy(b).y, 1
else
inc enemy(b).y, 1
endif
endif
if abs(enemy(b).x - player.x) > 4 and enemy(b).state = true
if enemy(b).x => player.x
dec enemy(b).x, 1
else
inc enemy(b).x, 1
endif
endif
endif
endif
sprite enemy(b).id, enemy(b).x, enemy(b).y, enemyimg
next a
next b
endfunction
function TrackStats()
ink rgb(255,255,255)
paste image healthbackground, 50, 20
for life = 1 to player.lives
paste image char, (life * 30)+35, 120
next life
for bullets = 1 to currbullet
paste image bullet, (bullets * 15)+50, 190
next bullets
if debug = true
for i = 1 to 5
text 200,i * 20,"bullets(" + str$(i) + ").state = " + str$(bullets(i).state)
text 200,(i * 20)+120,"bullets(" + str$(i) + ").id = " + str$(bullets(i).id)
text 550, 50, "FPS: "+str$(screen fps())
next i
endif
set text font "courier"
set text size 25
center text 105, 80, "LIVES"
center text 105, 0, "Health"
center text 105, 160, "Bullets"
sprite healthbarS, 51, 21, healthbar
if player.health < 0 then player.health = 0
stretch sprite healthbarS, player.health, 100
endfunction
function fadeIn(spr, tmp, alpha_value, death) `Sprite number, offset, and starting alpha value.
set sprite alpha spr, alpha_value
offset = tmp / 255
tmr = timer() + offset
repeat
if timer() > tmr then inc alpha_value : tmr = timer() + offset : set sprite alpha spr, alpha_value
if death = 1
if timer() > Htmr
inc player.health, 1
Htmr = timer() + 15
if player.health > 100 then player.health = 100
endif
endif
TrackStats()
sync
until alpha_value = 255
endfunction
function fadeOut(spr, tmp, alpha_value)
set sprite alpha spr, 255
offset = tmp / 255
tmr = timer() + offset
repeat
if timer() > tmr then dec alpha_value : tmr = timer() + offset : set sprite alpha spr, alpha_value
TrackStats()
sync
until alpha_value = 0
endfunction
When the enemies collide on the X way, they go into each other. This seems to have started after making the bullet system, But I'm not sure why. Collision is fine on the Y way.
EDIT: I have another question, I have this:
for bullets = 1 to currbullet
paste image bullet, (bullets * 15)+50, 190
next bullets
But what I want it to do is if currbullet (I should probably change the name, its what I use to keep track of how many bullets are left) equals more than a row limit (6 per row) then advance to the next row and keep pasting. But I'm not exactly sure how to do this.
-------------------------------------------------------------