You were kidnapped by an alien.
You were shocked and fainted.
When you woke up, you were in a tunnel.
Beware, aliens are coming to eat you.
Code:
'========================================
' TUNNEL THE GAME
'----------------------------------------
' Background story :
' You were kidnapped by an alien.
' You were shocked and fainted.
' When you woke up, you were in a tunnel.
' Beware, aliens are coming to eat you.
' How long can you survive from them ?
'
' Just remember, ...
' it's a never ending nightmare.
'
' Control :
' LEFT, RIGHT = move left, right
'
' References (Marcus)
' - sleepy_tunnel.n7
' - raster_scaling.n7
' - sound library
'
'========================================
'----------------
' INITIALIZATION
'----------------
include "sfx.n7"
visible sfx = SFX() 'create an sfx instance
constant WIN_W = 320, WIN_H = 240
set window "Sleepy Tunnel", WIN_W, WIN_H,false,2
font1 = createfont("arial", 24, 1,0,0,0 ) 'bold, italic, underlined, smoot
set redraw off
'RGB color definition
black = [0,0,0]
white = [255,255,255]
white_alpha = [255,255,255,0]
red = [255,0,0]
green = [0,168,0]
'sprite class definition
Sprite = [
'--- Properties ---
img:0,x:0,y:0,w:0,h:0,dx:50,dy:0,n:8,f:1,
'--- Methods ------
Create:function(x,c1,c2,b)
imgsize = 8; this.img = createimage(imgsize,imgsize); set image this.img
set color b;cls'background color
select x
case 1 'draw You
set color c2;draw ellipse imgsize/2,imgsize/2,imgsize/2,imgsize/2,1
set color c1;draw ellipse imgsize/2,imgsize/2,imgsize/2,imgsize/2,0
case 2 'draw alien
data =[
0,1,1,1,1,0,2,0,2,1,4,1,4,0,5,0,5,1,6,1,
6,3,5,3,5,4,6,4,6,6,
5,6,5,5,1,5,1,6,
0,6,0,4,1,4,1,3,0,3]
set color c2;draw poly data,1
set color c1;draw poly data,0
endsel
set image primary; set image colorkey this.img,255,0,0 'transparent background
return this.img
endfunc,
Scale:function(c,x,y,w,h)
set color c; du = 1/w; u = 0
for x = x to x + w - 1
draw vraster this.img, x, y, y + h - 1, u, 0, u, 1; u = u + du
next
endfunc
]
'initial value
you = copy(Sprite)
you.img = you.Create(1,white,green,red)
you.x = (WIN_W-width(you.img))/4-you.dx/2
you.y = WIN_H-height(you.img)/2
you.w = 60
you.h = 15
alien = fill(Sprite,Sprite.n)
for i = 0 to Sprite.n-1
alien[i].img = alien[i].Create(2,white,green,red)
alien[i].x = (WIN_W-width(alien[i].img))/2+rnd(10)
alien[i].y = (WIN_H-height(alien[i].img))/2+rnd(10)
next
TitleScreen(white)
visible score = 0
bb = 0; life = 10; collide = false
'-----------
' GAME LOOP
'-----------
while not keydown(KEY_ESCAPE, true)
'Prepare screen
set color black;cls
set color white
'Your score and life
set font font1
score = score + 1
set caret 5,5;wln "Score :"+score
set caret WIN_W-80,5;wln "Life :"+life
'Action Controls
if keydown(KEY_RIGHT,true) and you.x<=250 then
you.x = you.x + you.dx
elseif keydown(KEY_LEFT,true) and you.x>=40 then
you.x = you.x - you.dx
endif
'Draw Objects
DrawTunnel(bb);bb=bb+0.01
'set color white;draw rect you.x,you.y,you.w,you.h 'hide it
you.Scale(white_alpha,you.x,you.y,you.w,you.h)
foreach i in alien
i.x = i.x + rnd(-10,10)
i.y = i.y + rnd(-10,10)
i.w = width(i.img)*i.f
i.h = height(i.img)*i.f
i.f = i.f + 0.1
if i.f >12 then i.f = 1
'check screen boundary
if i.x > WIN_W or i.x < 0 then
i.x = (WIN_W-width(alien[i].img))/2+rnd(10)
i.f = 1
endif
if i.y > WIN_H or i.y < 0 then
i.y = (WIN_H-height(alien[i].img))/2+rnd(10)
i.f = 1
endif
'collision detection
if (you.x >= i.x and you.x<=i.x+i.w-20) and (you.y>=i.y and you.y<=i.y+i.h-20) then collide=true
if (you.x+you.w>= i.x and you.x+you.w<=i.x+i.w-20) and (you.y>=i.y and you.y<=i.y+i.h-20) then collide=true
if (you.x >= i.x and you.x<=i.x+i.w-20) and (you.x+you.w>=i.x and you.x+you.w<=i.x+i.w-20) then collide=true
if (you.x+you.w>=i.x and you.x+you.w<=i.x+i.w-20) and (you.y+you.h>=i.y and you.y+you.h<=i.y+i.h-20) then collide=true
if collide then
Sound(0.2,392*2)
'temp = rln() 'hide
life = life - 1
if life < 0 then Ending(white,red,font1)
do
set color red
draw rect 0,0,WIN_W,WIN_H,1
set color white
set caret WIN_W/2,5;center "Alien attacked you";center"Press SPACE BAR"
i.x = -1000;i.y = -1000
redraw
wait 1
until keydown(KEY_SPACE,true)
set color white
collide = false
endif
'draw alien
'set color white;draw rect i.x,i.y,i.w,i.h 'hide
i.Scale(white_alpha,i.x,i.y,i.w,i.h)
next
Sound(0.01,261.63);Sound(0.01,329.63)
redraw
wend
'free memory
free image you.img
foreach i in alien free image i.img
'-----------
' FUNCTIONS
'-----------
function TitleScreen(c)
set color c
wln "TUNNEL THE GAME"
wln
wln "You were kidnapped by an alien."
wln "You were shocked and fainted."
wln "When you woke up, you were in a tunnel."
wln "Beware, aliens are coming to eat you."
wln "How long can you survive from them ?"
wln "Just remember, ..."
wln "It's a never ending nightmare."
wln
wln "Control :"
wln "LEFT and RIGHT"
wln
wln "ENTER to continue"
Sound(0.2,261.63);Sound(0.2,293.66);Sound(0.2,329.63)
Sound(0.2,261.63);Sound(0.2,293.66);Sound(0.2,329.63)
temp = rln()
endfunc
function DrawTunnel(bb)
aa = aa + bb
zoffset = (zoffset + 0.001)%0.01
maxz = 0.1 + 150*0.01
for i = 150 to 0
z = 0.1 + i*0.01 - zoffset
s = 50/z
x = cos(z*5 + aa)*(z - 0.1)*50
y = sin(z*5 + aa)*(z - 0.1)*25
intens = 255 - 255*z*1.5/maxz
set color intens*0.5, intens, intens*0.75
a = 15
while a <= 360
draw line to WIN_W/2 + x/z + cos(rad(a))*s, WIN_H/2 + y/z + sin(rad(a))*s
a = a + 15
wend
next
endfunc
function Ending(c1,c2,fnt)
set color c2; cls
set font fnt; set color c1; set caret WIN_W/2,WIN_H/2-50
center "The End"
center "Score :"+score
center
center "ENTER to quit"
redraw
wait 1
temp = rln()
end
endfunc
function Sound(b,x)
'SquareWave(duration,freq,volume)
mytune = sfx.SquareWave(b,x,0.05)
play sound mytune
wait 50
free sound mytune 'release sound from memory
endfunc