Space War
Posted: Wed Sep 11, 2013 7:13 am
Code: Select all
'SPACE WAR for iPAD only
'USE THE SQUARES ON THE SCREEN TO CONTROL DE SPACECRAFT
'Hit the opposite space crafts 10 times to achieve victory
'---------
'You can ask the COMPUTER to control the blue spacecraft
'STAR gravity takes effect on spacecrafts
'The MODE button switches limited to continuous space
'SIZE: Large spacecrafts move slowly but do 5 times more damage
' Updates
' 28-dec 2015 - time reset fixed abd updated for supporting retina screen
' 30-dec 2015 - one player game
' 3-jan 2016 - play options, winner effect
' next planned updates: ai for the robot, smoothening the game (sprites), hiperspace, cleaning code.
START:
players=2 'set number of players, 1 or 2, button computer
star=0 'set star yes or not
mode=0 'sets continuity of space
siz1=0 'sets size of craft 1
siz2=0 'sets size of craft 2
extra=0 'sets an extra shooting button
graphics
graphics clear 0,0,0
set toolbar off
' graphics mode xor
set orientation left
option angle degrees
scal=SCREEN_SCALE()
set buttons custom
draw linecap round
toc1=0!toc2=0
t1=0!t2=0
draw color 0.5,0.5,0.5! draw alpha 0.5
fill color 0,0,0! fill alpha 0.5
button "players" text "Computer" at 650,725
button "star" text " Star " at 569,725
button "mode" text " Mode " at 474,725
button "siz1" text "Size1" at 326,725
button "siz2" text "Size2" at 401,725
draw color 0.65, 0.65, 0.65
button "exit" text "EXIT" at 440,4
button "restart" text "RESTART" at 505,4
draw alpha 1
fill alpha 1
INICI:
dim m(6751,2)
for a =1 to 6750
m(a,0)=rnd(1023*scal)!m(a,1)=rnd(760*scal)
if m(a,1)>723*scal and m(a,0)<200*scal then a=a-1
next a
draw size 1.5
a1=0!x1=100!y1=100
a2=20!x2=800!y2=500
ix1=0!iy1=0!ix2=0!iy2=0
amp=0!amt=0
time reset
t=timer()
Gosub DRAWBUTTON
k=0!k2=0!k3=0
points1=0.05
points2=0.05
gosub POINTS
MAIN:
gosub CLEARCRAFT
gosub CLEARCRAFT 'This is needed
gosub BUTTONS
if players=1 then gosub ROBOT
if star=1 then gosub STAR
x1=x1+ix1!y1=y1+iy1
x2=x2+ix2!y2=y2+iy2
if mode=0 then
if x1>1024 or x1<0 then
ix1=-ix1!x1=x1+ix1
end if
if y1>700 or y1<0 then
iy1=-iy1!y1=y1+iy1
end if
if x2>1024 or x2<0 then
ix2=-ix2!x2=x2+ix2
end if
if y2>700 or y2<0 then
iy2=-iy2!y2=y2+iy2
end if
end if
if mode=1 then
if x1>1024 then x1=x1-1024
if x1<0 then x1=x1+1024
if y1>700 then y1=y1-700
if y1<0 then y1=y1+700
if x2>1024 then x2=x2-1024
if x2<0 then x2=x2+1024
if y2>700 then y2=y2-700
if y2<0 then y2=y2+700
end if
if ix1>15-siz1/2 or ix1<-15+siz1/2 or iy1>15-siz1/2 or iy1<-15+siz1/2 then
ix1=ix1*0.9!iy1=iy1*0.9
end if
if ix2>15-siz2/2 or ix2<-15+siz2/2 or iy2>15-siz2/2 or iy2<-15+siz2/2 then
ix2=ix2*0.9!iy2=iy2*0.9
end if
rem SHOTS
fill color 0,0,0! fill circle xt1,yt1 size 4! fill circle xt2,yt2 size 4
rem draw rect xt1,yt1 size 1!draw rect xt2,yt2 size 1
if t1=60 then
xt1=x1!yt1=y1
ixt1=cos(a1)*40+ix1!iyt1=sin(a1)*40+iy1
end if
if t1>0 then
xt1=xt1+ixt1!yt1=yt1+iyt1!t1=t1-1
if mode=0 then
if xt1>1024 or xt1<0 then ixt1=-ixt1
if yt1>700 or yt1<0 then iyt1=-iyt1
end if
if mode=1 then
if xt1>1024 then xt1=xt1-1024
if xt1<0 then xt1=xt1+1024
if yt1>700 then yt1=yt1-700
if yt1<0 then yt1=yt1+700
end if
fill color 0.5,0,0!fill circle xt1,yt1 size 3.5
fill color 1,0.5,0!fill circle xt1,yt1 size 2
draw color 1,1,0!draw pixel xt1*scal,yt1*scal
end if
if t2=60 then
xt2=x2!yt2=y2
ixt2=cos(a2)*40+ix2!iyt2=sin(a2)*40+iy2
end if
if t2>0 then
xt2=xt2+ixt2!yt2=yt2+iyt2!t2=t2-1
if mode=0 then
if xt2>1024 or xt2<0 then ixt2=-ixt2
if yt2>700 or yt2<0 then iyt2=-iyt2
end if
if mode=1 then
if xt2>1024 then xt2=xt2-1024
if xt2<0 then xt2=xt2+1024
if yt2>700 then yt2=yt2-700
if yt2<0 then yt2=yt2+700
end if
fill color 0,0,1!fill circle xt2,yt2 size 3.5
fill color 0,0.5,1!fill circle xt2,yt2 size 2
draw color 0,1,1!draw pixel xt2*scal,yt2*scal
end if
if xt1>x2-20-siz2 and xt1<x2+20+siz2 and yt1>y2-20-siz2 and yt1<y2+20+siz2 and t1>0 then
toc2=40
points1=points1+1+siz1/5
gosub POINTS
t1=1
end if
if xt2>x1-20+siz1 and xt2<x1+20+siz1 and yt2>y1-20-siz1 and yt2<y1+20+siz1 and t2>0 then
toc1=40
points2=points2+1+siz2/5
gosub POINTS
t2=1
end if
if button_pressed("players")=1 then
if players=1 then players=2 else players=1
if players=2 then fill color 0, 0, 0
if players=2 then draw color 0.25, 0.25, 0.25
if players=1 then fill color 0.25, 0.25, 0.25
if players=1 then draw color 0, 0, 0
button "players" text "Computer" at 650,725
end if
if button_pressed("star")=1 then
if star=1 then star=0 else star=1
if star=0 then fill color 0, 0, 0
if star=0 then draw color 0.25, 0.25, 0.25
if star=1 then fill color 0.25, 0.25, 0.25
if star=1 then draw color 0, 0, 0
button "star" text " Star " at 569,725
if star=0 then fill circle 512,350 size 7
end if
if button_pressed("mode")=1 then
if mode=1 then mode=0 else mode=1
if mode=0 then fill color 0, 0, 0
if mode=0 then draw color 0.25, 0.25, 0.25
if mode=1 then fill color 0.25, 0.25, 0.25
if mode=1 then draw color 0, 0, 0
button "mode" text " Mode " at 474,725
end if
if button_pressed("siz1")=1 then
if siz1=20 then siz1=0 else siz1=20
if siz1=0 then fill color 0, 0, 0
if siz1=0 then draw color 0.25, 0.25, 0.25
if siz1=20 then fill color 0.25, 0.25, 0.25
if siz1=20 then draw color 0, 0, 0
button "siz1" text "Size1" at 326,725
end if
if button_pressed("siz2")=1 then
if siz2=20 then siz2=0 else siz2=20
if siz2=0 then fill color 0, 0, 0
if siz2=0 then draw color 0.25, 0.25, 0.25
if siz2=20 then fill color 0.25, 0.25, 0.25
if siz2=20 then draw color 0, 0, 0
button "siz2" text "Size2" at 401,725
end if
if button_pressed("exit")=1 then end
if button_pressed("restart")=1 then goto START
gosub DRAWCRAFT
graphics unlock
rem fill color 0.5,0,0!'fill rect 0,0 to timer(),10
rem fill color 0,0.5,0.5!'fill rect timer(),0 to 50,10
LOOP1: if timer()<40 then LOOP1 else time reset
graphics lock
k=k+1!if k>6 then k=1
on k gosub S1,S3,S2,S3,DRAWBUTTON,S3
goto MAIN
ROBOT:
'shooting solution
if t2>0 then goto NOSHOT
'draw color 0,0,0
'draw circle x3,y3 size 10 for vistualizing shooting solution
shoot=1
' part one: where they will meet
if ix1=0 then ix1=0.001
p1= iy1/ix1
p2= (sin(a2)*40+iy2)/(cos(a2)*40+ix2)
b1= y1-p1*x1
b2= y2-p2*x2
if p1=p2 then p1=p1+0.001
x3= (b2-b1)/(p1-p2) 'Main formula according to my high-school equation solutions
y3= x3*p1+b1
' draw color 1,1,1
' draw circle x3,y3 size 10 to see shooting solution
' part two: meeting place is on the screen and in the right derection?
if mode=0 or star=1 then
if x3<0 or x3>1024 or y3<0 or y3>750 then goto NOSHOT
end if
if mode=1 and star=0 then
if x3<-1024 or x3>2048 or y3<-750 or y3>1500 then goto NOSHOT
end if
if cos(a2)>=0 and x3<x2 then goto NOSHOT
if cos(a2)<=0 and x3>x2 then goto NOSHOT
' part two: will the shot arrive in time?
timeshot1= (x3-x1)/ix1! timeshot2=(x3-x2)/(cos(a2)*40+ix2)
if timeshot1>timeshot2-1 and timeshot1<timeshot2+1 and rnd(2)<3 then t2=60 '=FINALLY SHOOTING!
NOSHOT:
'Manovers
'mp=power mt=turn
'amp=aim speed amt= aim turn
'0 =do nothing 0= do nothing
'1 =accelerate 1= rotate clockwise
'2 =slowdown(cheat) 2=rotate anticlockwise
zis2=siz2
'power
if amp=0 and then
mp=rnd(4)-1! if mp<0 then mp=0
amp=rnd(35)+1!
end if
amp=amp-1
if mp=1 then
ix2=ix2+cos(a2)*(1-siz2/40)
iy2=iy2+sin(a2)*(1-siz2/40)
end if
if mp=2 then
ix2=ix2*0.95
iy2=iy2*0.95
end if
if star=1 then
if x2>490 and x2<535 and y2>345 and y2<375 then
mp=1!amp=60!mt=0!amt=60
end if
end if
'turning
if amt=0 and then
mt=rnd(3)
amt=rnd(50)+1!
end if
amt=amt-1
if mt=1 and t2<100 then a2=a2+(10-siz2/3)
if mt=2 and t2<100 then a2=a2-(10-siz2/3)
return
STAR:
fill color 1,1,0
fill circle 512,350 size 6
dist=(x1-512)*(x1-512)+(y1-350)*(y1-350)
dist=dist/25000! if dist<1 then dist=1
if x1<512 then ix1=ix1+0.4/dist else ix1=ix1-0.4/dist
if y1<350 then iy1=iy1+0.4/dist else iy1=iy1-0.4/dist
dist=(x2-512)*(x2-512)+(y2-350)*(y2-350)
dist=dist/25000! if dist<1 then dist=1
if x2<512 then ix2=ix2+0.4/dist else ix2=ix2-0.4/dist
if y2<350 then iy2=iy2+0.4/dist else iy2=iy2-0.4/dist
return
DRAWCRAFT:
draw size 1.5
draw color 1,0,0
draw line x1+cos(a1)*(20+siz1), y1+sin(a1)*(20+siz1) to x1+cos(a1+135)*(20+siz1), y1+sin(a1+135)*(20+siz1)
draw line to x1-cos(a1)*(10+siz1/2), y1-sin(a1)*(10+siz1/2)
draw line to x1+cos(a1-135)*(20+siz1), y1+sin(a1-135)*(20+siz1)
draw line to x1+cos(a1)*(20+siz1), y1+sin(a1)*(20+siz1)
draw color 0,1,1
draw line x2+cos(a2)*(20+siz2), y2+sin(a2)*(20+siz2) to x2+cos(a2+135)*(20+siz2), y2+sin(a2+135)*(20+siz2)
draw line to x2-cos(a2)*(10+siz2/2), y2-sin(a2)*(10+siz2/2)
draw line to x2+cos(a2-135)*(20+siz2), y2+sin(a2-135)*(20+siz2)
draw line to x2+cos(a2)*(20+siz2), y2+sin(a2)*(20+siz2)
if toc2>0 then
fill color rnd(1),rnd(1),rnd(1)!fill circle x2,y2 size (20+siz2)
a2=rnd(180)
end if
if toc1>0 then
fill color rnd(1),rnd(1),rnd(1)!fill circle x1,y1 size (20+siz1)
a1=rnd(180)
end if
return
CLEARCRAFT:
draw color 0,0,0! draw size 2
draw line x1+cos(a1)*(20+siz1), y1+sin(a1)*(20+siz1) to x1+cos(a1+135)*(20+siz1), y1+sin(a1+135)*(20+siz1)
draw line to x1-cos(a1)*(10+siz1/2), y1-sin(a1)*(10+siz1/2)
draw line to x1+cos(a1-135)*(20+siz1), y1+sin(a1-135)*(20+siz1)
draw line to x1+cos(a1)*(20+siz1), y1+sin(a1)*(20+siz1)
draw line x2+cos(a2)*(20+siz2), y2+sin(a2)*(20+siz2) to x2+cos(a2+135)*(20+siz2), y2+sin(a2+135)*(20+siz2)
draw line to x2-cos(a2)*(10+siz2/2), y2-sin(a2)*(10+siz2/2)
draw line to x2+cos(a2-135)*(20+siz2), y2+sin(a2-135)*(20+siz2)
draw line to x2+cos(a2)*(20+siz2), y2+sin(a2)*(20+siz2)
if toc2>0 then
toc2=toc2-1
fill color 0,0,0
fill circle x2,y2 size 21+siz2
end if
if toc1>0 then
toc1=toc1-1
fill color 0,0,0
fill circle x1,y1 size 21+siz1
end if
return
DRAWCRAFTS:
draw size siz/15
draw line x4+cos(a4)*siz, y4+sin(a4)*siz to x4+cos(a4+135)*siz, y4+sin(a4+135)*siz
draw line to x4-cos(a4)*siz/2, y4-sin(a4)*siz/2
draw line to x4+cos(a4-135)*siz, y4+sin(a4-135)*siz
draw line to x4+cos(a4)*siz, y4+sin(a4)*siz
return
DRAWBUTTON:
mod=0
draw linecap round
draw size 1
draw alpha 0.5! draw color 0.2,0.2,0.2
draw rect 2,262+mod to 82,342+mod
draw rect 2,352+mod to 82,432+mod
draw rect 2,442+mod to 82,522+mod
draw rect 92,352+mod to 172,432+mod
if extra=1 then draw rect 2,60 to 82,202
draw rect 1022,262+mod to 942,342+mod
draw rect 1022,352+mod to 942,432+mod
draw rect 1022,442+mod to 942,522+mod
draw rect 852,352+mod to 932,432+mod
if extra=1 then draw rect 1022,582 to 942,700
draw alpha 1
return
BUTTONS:
p1=0!p2=0
for a = 0 to 7
x=touch_x(a)!if x<0 then FIN1
y=touch_y(a)
if x<80 then
if y>262 and y<342 then a1=a1-(10-siz1/2.7)
if y>342 and y<442 and t1=0 then t1=60
if y>442 and y<522 then a1=a1+(10-siz1/2.7)
end if
if x>87 and x<200 and y>342 and y<442 then
ix1=ix1+cos(a1)*(1-siz1/40)
iy1=iy1+sin(a1)*(1-siz1/40)
p1=0
end if
if x>937 then
if y>262 and y<342 then a2=a2+(10-siz2/2.7)
if y>342 and y<442 and t2=0 then t2=60
if y>442 and y<522 then a2=a2-(10-siz2/2.7)
end if
if x>848 and x<937 and y>342 and y<442 then
ix2=ix2+cos(a2)*(1-siz2/40)
iy2=iy2+sin(a2)*(1-siz2/40)
p2=0
end if
FIN1:
next a
return
POINTS:
fill color 1,0,0! fill rect 0,729 to points1*20,738
fill color 0,0.5,1! fill rect 0,741 to points2*20,750
'winner effect
If points1>10 or points2>10 then
fill color 0,0,0
fill circle xt1,yt1 size 4
fill circle xt2,yt2 size 4
for s=1 to 50
if points1>10 then
x4=((x1*(50-s))+(512*s))/50
y4=((y1*(50-s))+(350*s))/50
a4=a1-5*s!
draw color 1,0,0
else
x4=((x2*(50-s))+(512*s))/50
y4=((y2*(50-s))+(350*s))/50
a4=a2-5*s!
draw color 0,1,1
end if
siz=20+s*4
if star=1 then gosub star
gosub DRAWCRAFTS
k=k+1!if k>6 then k=1
on k gosub S1,S3,S2,S3,DRAWBUTTON,S3
on k gosub S1,S3,S2,S3,DRAWBUTTON,S3
draw color 0,0,0
siz=20+s*4
graphics unlock
graphics lock
gosub DRAWCRAFTS
gosub DRAWCRAFTS
if points1>10 then draw color 1,0,0 else draw color 0,1,1
siz=20+s*4
fill color rnd(1),rnd(1),rnd(1)
if points1>10 then fill circle x2,y2 size 20+siz2 else fill circle x1,y1 size 20+siz1
if button_pressed("exit")=1 then end
if button_pressed("restart")=1 then goto START
next s
fill color 0,0,0
if points1>10 then fill circle x2,y2 size 22+siz2 else fill circle x1,y1 size 22+siz1
gosub DRAWCRAFTS
graphics unlock
option text pos central
draw color 1,1,1
'set output font size 16
if points1>10 and players=2 then draw text "PLAYER 1 WINS !!" at 512,650
if points2>10 and players=2 then draw text "PLAYER 2 WINS !!" at 512, 650
if points1>10 and players=1 then draw text "YOU WIN !!" at 512,650
if points2>10 and players=1 then draw text "COMPUTER WINS !!" at 512, 650
draw text "click on EXIT or RESTART" AT 512,670
for a=1 to 10
if button_pressed("exit")=1 then end
if button_pressed("restart")=1 then goto START
a=a-1
next A
end
end if
return
'STARS
S3:
draw color 0.3,0.3,0.3
draw size 1
k3=k3+1!if k3>19 then k3=0
l=k3*250+2000
for a =k3*250+1750 to l
draw pixel m(a,0),m(a,1)
next a
draw alpha 1
return
S2:
draw color 0.6,0.6,0.6
draw size 1
k2=k2+1!if k2>5 then k2=0
l=k2*250+500
for a =k2*250+250 to l
draw pixel m(a,0),m(a,1)
next a
return
S1:
draw color 0.99,0.99,0.99
draw size 1
for a =26 to 250
draw pixel m(a,0),m(a,1)
next a
draw color 1,1,1
draw size 1.6
for a =1 to 25
draw pixel m(a,0),m(a,1)
next a
draw size 1
return