TGC Codebase Backup



Star War by trasko

16th Apr 2005 14:12
Summary

A little space shooter 2D game



Description

This is my first game, i hope you enjoy play with it.
Use the spacekey for shoot, upkey for speed up, left-right key for turn your ship.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    remstart
************************************************************
*              SPACE WAR by Trasko                         *
*                                                          *
* This game is an old-styled space game for my Amiga 500   *
* computer. I don't remember who was the author.           *
* The Original game need of 3 boundary instead 1 like mine *
* but unfortunatly i wasn't able to make a better job      *
*                                                          *
* The point - line distance function was taken from        *
* www.thegamescreator.com site.                            *
* Thank to the author KyleTDBoy                            *
*                                                          *
* For suggest, problem contact me at:                      *
* trasko@traskosfriends.it                                 *
*                                                          *
* P.S. Sorry for my bad english! But i come from Italy     *
************************************************************
remend
sync rate 300
sync on
hide mouse
randomize timer()

rem Array for Bullet
dim xp#(1,60)
dim yp#(1,60)
dim pa(1,60)
dim vxp#(1,60)
dim vyp#(1,60)
rem Array for Boundary
dim angb(9)
dim angba(9)
rem Array for Star Field
dim xs#(150)
dim ys(150)
dim ms(150)
dim ps#(150)

init:
for i=1 to 150
xs#(i)=rnd(640)
ys(i)=rnd(480)
ms(i)=rnd(5)
ps#(i)=rnd(4)/4
next i
ok=1
repeat
for i=1 to 150
   ink rgb(51*ms(i),51*ms(i),51*ms(i)),0
   dot xs#(i),ys(i)
   xs#(i)=xs#(i)+ps#(i)
   if xs#(i)>640 then xs#(i)=0
next i
ink rgb(255,255,0),0
set text size 30
center text 320,50,"SPACE WAR"
set text size 8
center text 320,100,"Scopo del gioco è distruggere l'avversario situato nel centro dello schermo."
center text 320,120,"L'avversario è protetto da una barriera rotante che può resistere a molti"
center text 320,140,"colpi. Inizialmente sparerà un colpo ogni tanto per mantere le distanze"
center text 320,160,"ma con il cadere delle barriere il nemico si sentirà minacciato e comincierà"
center text 320,180,"a sparare sempre più velocemente."
center text 320,200,"Puoi resistere fino a 5 colpi, dopodichè la tua astronave esploderà."
center text 320,300,"Premi invio per iniziare la partita"
sync
cls 0
if returnkey()=1 and ok=0 then ok=2
if returnkey()=0 then ok=0
until returnkey()=1 and ok=2
rem Human player Setting
x#=100
y#=240
velx#=0
vely#=0
a=wrapvalue(rnd(360))
energia=5
punti=0

nemico:
rem Cpu player setting
xn#=320
yn#=240
an=wrapvalue(rnd(360))
vivo=1
for i=0 to 1
   for j=0 to 60
   xp#(i,j)=0
   yp#(i,j)=0
   pa(i,j)=0
   next j
next i

rem boundary setting
for i=0 to 9
   angb(i)=wrapvalue(i*36)
   angba(i)=16
next i

rem Main loop
repeat
starfield()
gosub player_human
gosub player_cpu
gosub proiettili
astronave(x#,y#,a,rgb(255,0,0))
if vivo=1
   astronave(xn#,yn#,an,rgb(0,0,255))
else
   goto enemy_death
endif
if energia=0 then goto gameover
ink rgb(200,150,20),0
center text 320,0,"Punti: "+str$(punti)
text 0,0,"Energia:"
box 60,3,energia*50,13
sync
cls 0
until 0

player_human:
if leftkey()=1 then a=wrapvalue(a-1)
if rightkey()=1 then a=wrapvalue(a+1)
velx#=sin(a)
vely#=cos(a)
if upkey()=1 then f#=1
if upkey()=0
   f#=f#-0.001
   if f#<0 then f#=0
endif
if spacekey()=1
   relay=relay+1
   if relay=6
      relay=0
      shot=shot+1
      if shot>60 then shot=0
      if pa(0,shot)=0
         pa(0,shot)=1
         xp#(0,shot)=x#
         yp#(0,shot)=y#
         vxp#(0,shot)=sin(a)*1.5
         vyp#(0,shot)=cos(a)*1.5
      endif
   endif
endif
x#=x#-(velx#*f#)
y#=y#+(vely#*f#)
if x#>640 then x#=640
if x#<0 then x#=0
if y#>480 then y#=480
if y#<0 then y#=0
return

player_cpu:
rem Calculated the enemy position
ang#=wrapvalue((atanfull((xn#-x#),(yn#-y#))+180)*-1)
if wrapvalue(an-ang#)<179 then gosub sx
if wrapvalue(an-ang#)>181 then gosub dx
rem Where and when i can shoot?
if an<=(ang#+1) and an>=(ang#-1)
   relayn=relayn+1
   if relayn>=pericolo
      relayn=0
      shotn=shotn+1
      if shotn>60 then shotn=0
      if pa(1,shotn)=0
         pa(1,shotn)=1
         xp#(1,shotn)=xn#
         yp#(1,shotn)=yn#
         vxp#(1,shotn)=sin(an)*2
         vyp#(1,shotn)=cos(an)*2
      endif
   endif
endif
rem Draw the boundary
pericolo=0
for i=0 to 9
   succ=i+1
   if succ>9 then succ=0
   rem calculate if it's hit from an enemy bullet
   for k=1 to 60
      if PointLineDistance(cos(angb(i))*40+320,sin(angb(i))*40+240,cos(angb(succ))*40+320,sin(angb(succ))*40+240,xp#(0,k),yp#(0,k))<1 and angba(i)>0 and pa(0,k)=1
         angba(i)=angba(i)-1
         punti=punti+1
         pa(0,k)=0
      endif
   next k
   rem calculate if then enemy ship collide whith the boundary
   if PointLineDistance(cos(angb(i))*40+320,sin(angb(i))*40+240,cos(angb(succ))*40+320,sin(angb(succ))*40+240,x#,y#)<6 and angba(i)>0
      energia=0
   endif
   rem If energy boundary isn't 0 then draw it
   if angba(i)>0
      ink rgb(0,16*angba(i)-1,0),0
      line cos(angb(i))*40+320,sin(angb(i))*40+240,cos(angb(succ))*40+320,sin(angb(succ))*40+240
   endif
   angb(i)=wrapvalue(angb(i)+1)
   pericolo=pericolo+angba(i)
next i
return

rem Draw the ship (position x, position y, direction degree, color)
function astronave(x#,y#,a,c)
ink c,0
line cos(wrapvalue(a+90))*10+x#,sin(wrapvalue(a+90))*10+y#,cos(wrapvalue(a+315))*10+x#,sin(wrapvalue(a+315))*10+y#
line cos(wrapvalue(a+315))*10+x#,sin(wrapvalue(a+315))*10+y#,x#,y#
line x#,y#,cos(wrapvalue(a+225))*10+x#,sin(wrapvalue(a+225))*10+y#
line cos(wrapvalue(a+225))*10+x#,sin(wrapvalue(a+225))*10+y#,cos(wrapvalue(a+90))*10+x#,sin(wrapvalue(a+90))*10+y#
endfunction

rem turn right
dx:
an=wrapvalue(an+1)
return

rem turn left
sx:
an=wrapvalue(an-1)
return

rem calculate the bullet position
proiettili:
for i=0 to 1
if i=0 then ink rgb(255,0,0),0 else ink rgb(0,0,255),0
   for j=1 to 60
      if pa(i,j)=1
         xp#(i,j)=xp#(i,j)-vxp#(i,j)
         yp#(i,j)=yp#(i,j)+vyp#(i,j)
         dot xp#(i,j),yp#(i,j)
         if xp#(i,j)>640 then pa(i,j)=0
         if xp#(i,j)<0 then pa(i,j)=0
         if yp#(i,j)>480 then pa(i,j)=0
         if yp#(i,j)<0 then pa(i,j)=0
         if dis(xp#(0,j),yp#(0,j),xn#,yn#)<6 and vivo=1
            vivo=0
            punti=punti+500
            pa(0,j)=0
         endif
         if dis(xp#(1,j),yp#(1,j),x#,y#)<6 and energia>0
             energia=energia-1
             pa(1,j)=0
         endif
      endif
   next j
next i
return

enemy_death:
pressione=0
dd=0
repeat
starfield()
ink rgb(255,255,0),0
center text 320,240,"C O N G R A T U L A Z I O N I ! ! !"
center text 320,256,"Premi Invio per continuare"
for i=0 to 9
   succ=i+1
   if succ>9 then succ=0
   if angba(i)>0
      ink rgb(0,16*angba(i)-1,0),0
      line cos(angb(i))*(40+dd)+320,sin(angb(i))*(40+dd)+240,cos(angb(succ))*(40+dd)+320,sin(angb(succ))*(40+dd)+240
   endif
   angb(i)=wrapvalue(angb(i)+1)
next i
dd=dd+1
sync
cls 0
if returnkey()=1 then pressione=1
until pressione<>0
goto nemico

gameover:
dd=0
repeat
starfield()
if dd<60
   ink rgb(255,0,0),0
   circle x#,y#,dd
   dd=dd+1
endif
ink rgb(255,255,0),0
center text 320,200,"G A M E   O V E R"
center text 320,230,"Il tuo punteggio è: "+str$(punti)
center text 320,256,"Premi Invio per continuare"
sync
cls 0
until returnkey()=1
goto init
end


Function Dis(x1#,y1#,x2#,y2#)
   VX# = X2# - X1#
   VY# = Y2# - Y1#
   V# = SQRT(VX#^2 + VY#^2)
Endfunction V#

Function PointLineDistance(x1#,y1#,x2#,y2#,x3#,y3#)

   `First We Have To Find The Length Of The Line
   LineMag# = Dis(x2#,y2#,x1#,y1#)

   `U# Is A Percentage. Its The Percent Of The Line That Can Be Added To The Line To Find The Intersection
   U# = (((x3# - x1#) * (x2# - x1#)) + ((y3# - y1#) * (y2# - y1#))) / LineMag#^2

   `Lets Find The Intersection (Or The Closest Point On The Line To The Point)
   IntersectionX# = X1# + U# * ( X2# - X1#)
   IntersectionY# = Y1# + U# * ( Y2# - Y1#)

   `This Section of Code Is Incase The Second Point Is Less Than The First Point.
   `Normally, It Would Cause A Lot Of Problems, But We Solved Them
   If X2# > X1#
      If IntersectionX# > X2# then IntersectionX# = X2#
      If IntersectionX# < X1# then IntersectionX# = X1#
   Endif

   If X2# < X1#
      If IntersectionX# < X2# then IntersectionX# = X2#
      If IntersectionX# > X1# then IntersectionX# = X1#
   Endif

   If Y2# > Y1#
      If IntersectionY# > Y2# then IntersectionY# = Y2#
      If IntersectionY# < Y1# then IntersectionY# = Y1#
   Endif

   If Y2# < Y1#
      If IntersectionY# < Y2# then IntersectionY# = Y2#
      If IntersectionY# > Y1# then IntersectionY# = Y1#
   Endif

   `Get Our Distance From The Point To The Line
   Distance# = Dis(IntersectionX#,IntersectionY#,X3#,Y3#)
Endfunction Distance#

rem Draw the starfield
function starfield()
for i=1 to 50
   ink rgb(51*ms(i),51*ms(i),51*ms(i)),0
   dot xs#(i),ys(i)
   xs#(i)=xs#(i)+ps#(i)
   if xs#(i)>640 then xs#(i)=0
next i
endfunction