Author Topic: Square Survivor  (Read 4307 times)

JSM

  • Guest
Square Survivor
« on: June 22, 2013, 10:37:07 AM »
for how long can you survive?

Code: [Select]
import "Speed.lib"
import "Keycodes.lib"

constant:
MAX_ENEMIES 256
MAX_BULLETS 64
visible:
enemies?[MAX_ENEMIES]
bullets?[MAX_BULLETS]
hidden:

set redraw off

for i = 0 to MAX_ENEMIES - 1
enemies[i].act = false
next

for i = 0 to MAX_BULLETS - 1
bullets[i].act = false
next

bulletTimer = 0

enemyDelay = 60
enemyTimer = enemyDelay

rem shouldDraw = true

life = 16

startTime = time()
do
bulletTimer = max(bulletTimer - 1, 0)

rem shoot.
mx = mousex()
my = mousey()
if mousebutton(0) and bulletTimer = 0 and not (mx = 320 and my = 240)
bulletTimer = 4
for i = 0 to MAX_BULLETS - 1
if not bullets[i].act
bullets[i].act = true
bullets[i].x# = 320.0
bullets[i].y# = 240.0
dx# = float(mx) - 320.0
dy# = float(my) - 240.0
d# = sqr(dx*dx + dy*dy)
k# = 4.0/d
bullets[i].dx# = dx*k
bullets[i].dy# = dy*k
break
endif
next
endif

rem update bullets.
for i = 0 to MAX_BULLETS - 1
if bullets[i].act
rem move.
bullets[i].x# = bullets[i].x# + bullets[i].dx#
bullets[i].y# = bullets[i].y# + bullets[i].dy#
rem out of screen.
if bullets[i].x# < -16.0 or bullets[i].x# > 656.0 or bullets[i].y# < -16.0 or bullets[i].dy# > 496.0
bullets[i].act = false
endif
endif
next

rem add enemy.
enemyTimer = max(enemyTimer - 1, 0)
if enemyTimer = 0
enemyTimer = enemyDelay
enemyDelay = max(enemyDelay - 1, 20)
for i = 0 to MAX_ENEMIES - 1
if not enemies[i].act
enemies[i].act = true
r = rnd(4)
if r = 0
enemies[i].x# = -16.0
enemies[i].y# = float(rnd(480))
elseif r = 1
enemies[i].y# = -16.0
enemies[i].x# = float(rnd(640))
elseif r = 2
enemies[i].x# = 656.0
enemies[i].y# = float(rnd(480))
else
enemies[i].y# = 496.0
enemies[i].x# = float(rnd(640))
endif
dx# = 320.0 - enemies[i].x#
dy# = 240.0 - enemies[i].y#
k# = 1.0/sqr(dx*dx + dy*dy)
enemies[i].dx# = dx*k
enemies[i].dy# = dy*k

if rnd(8) = 0
enemies[i].st = 32
else
enemies[i].st = 16
endif

enemies[i].r = 128 + rnd(128)
enemies[i].g = 128 + rnd(128)
enemies[i].b = 128 + rnd(128)
break
endif
next
endif

rem update enemies.
for i = 0 to MAX_ENEMIES - 1
if enemies[i].act
enemies[i].x# = enemies[i].x# + enemies[i].dx#
enemies[i].y# = enemies[i].y# + enemies[i].dy#
dx# = abs#(enemies[i].x# - 320.0)
dy# = abs#(enemies[i].y# - 240.0)
if dx < 16.0 and dy < 16.0
enemies[i].act = false
life = life - 1
else
for j = 0 to MAX_BULLETS - 1
if bullets[j].act
dx = abs#(enemies[i].x# - bullets[j].x#)
dy = abs#(enemies[i].y# - bullets[j].y#)
if dx < 16.0 and dy < 16.0
bullets[j].act = false
enemies[i].st = enemies[i].st - 6
if enemies[i].st <= 0
enemies[i].act = false
endif
break
endif
endif
next
endif
endif
next

rem if shouldDraw
set color 0, 0, 0, 16
cls

rem draw player.
set color 255, 255, 255
draw rect 320 - life, 240 - life, life*2, life*2, true

rem draw bullets.
set color 255, 255, 0
for i = 0 to MAX_BULLETS - 1
if bullets[i].act
draw rect int(bullets[i].x#) - 2, int(bullets[i].y#) - 2, 4, 4, true
endif
next

rem draw enemeies.
set color 0, 255, 255
for i = 0 to MAX_ENEMIES - 1
if enemies[i].act
set color enemies[i].r, enemies[i].g, enemies[i].b
s = enemies[i].st
draw rect int(enemies[i].x#) - s/2, int(enemies[i].y#) - s/2, s, s, true
endif
next

set color 255, 255, 255
set caret 320, 0
center "TIME"
center (time() - startTime)/1000

redraw
rem endif

rem shouldDraw = SPD_HoldFrameDraw(60)
   proc SPD_HoldFrame 60
until life <= 2 or keydown(VK_ESC, true)

set color 0, 0, 0
cls
set color 255, 255, 255
set caret 320, 200
center "You made it for ", (time() - startTime)/1000, " seconds."
redraw
wait keydown

EDIT: Corrected your spelling, you drunk sack of tomatoes.
« Last Edit: June 22, 2013, 06:23:32 PM by Mopz »

cvirus

  • Guest
Re: Square Survivor
« Reply #1 on: June 22, 2013, 10:51:08 AM »
Gives me an error, and should it be survive instead of survice?

Compiling ...
Error on line 174:
Undefined symbol "SPD_HoldFrameDraw"

 ;D

Mopz

  • Guest
Re: Square Survivor
« Reply #2 on: June 22, 2013, 12:25:59 PM »
Did you miss the first code line while copying, cvirus? SPD_HoldFrameDraw is in the Speed lib :)

Survived for 65 seconds ;)

cvirus

  • Guest
Re: Square Survivor
« Reply #3 on: June 22, 2013, 12:42:41 PM »
Nope, all like the code, i have got

Code: [Select]
shouldDraw = true
and then at almost in the end of the code

Code: [Select]
shouldDraw =  SPD_HoldFrameDraw(60)
Does this return true or false, this function i mean?

I have coment the
Code: [Select]
shouldDraw =  SPD_HoldFrameDraw(60) and now it works.

:)  12 secs....

Mopz

  • Guest
Re: Square Survivor
« Reply #4 on: June 22, 2013, 01:17:13 PM »
Without the SPD_HoldFrameDraw function no one'd last more than 12 seconds ;)

You can download the updated Speed library from http://www.naalaa.com/libraries.html . Put Speed.lib and Speed.libj in the bin/libs folder of your naalaa installation. Can't live without that function ;)

SPD_HoldFrameDraw works like SPD_HoldFrame, but returns false if the program is running too slow. So if it returns false you can skip drawing and just update the logic.

EDIT: 91 seconds this time.
« Last Edit: June 22, 2013, 01:27:04 PM by Mopz »

Newbian

  • Guest
Re: Square Survivor
« Reply #5 on: June 23, 2013, 04:05:50 AM »

My finger hurts now.  ;D

Newbian

  • Guest
Re: Square Survivor
« Reply #6 on: June 24, 2013, 04:18:57 AM »
Here's a screen shot of a game in progress running under XP.

Guess it helps to have a so-so computer/video card. It's still more intense than my old nerves can handle.
« Last Edit: June 24, 2013, 04:23:17 AM by Newbian »