Author Topic: Square Survivor  (Read 1461 times)

JSM

  • Regular Member
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Eggplant on Wheels
Square Survivor
« on: 22. June 2013, 12:37:07 »
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: 22. June 2013, 20:23:32 by Mopz »
I certainly am an eggplant on wheels ...
http://www.eggplantonwheels.net

cvirus

  • Regular Member
  • Jr. Member
  • *
  • Posts: 76
    • View Profile
Re: Square Survivor
« Reply #1 on: 22. June 2013, 12:51:08 »
Gives me an error, and should it be survive instead of survice?

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

 ;D

Marcus

  • Globaler Moderator
  • Full Member
  • *****
  • Posts: 241
    • View Profile
    • NaaLaa
Re: Square Survivor
« Reply #2 on: 22. June 2013, 14:25:59 »
Did you miss the first code line while copying, cvirus? SPD_HoldFrameDraw is in the Speed lib :)

Survived for 65 seconds ;)

cvirus

  • Regular Member
  • Jr. Member
  • *
  • Posts: 76
    • View Profile
Re: Square Survivor
« Reply #3 on: 22. June 2013, 14:42:41 »
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....

Marcus

  • Globaler Moderator
  • Full Member
  • *****
  • Posts: 241
    • View Profile
    • NaaLaa
Re: Square Survivor
« Reply #4 on: 22. June 2013, 15:17:13 »
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: 22. June 2013, 15:27:04 by Mopz »

Newbian

  • Regular Member
  • Newcomer
  • *
  • Posts: 11
    • View Profile
Re: Square Survivor
« Reply #5 on: 23. June 2013, 06:05:50 »

My finger hurts now.  ;D

Newbian

  • Regular Member
  • Newcomer
  • *
  • Posts: 11
    • View Profile
Re: Square Survivor
« Reply #6 on: 24. June 2013, 06:18:57 »
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: 24. June 2013, 06:23:17 by Newbian »