Just did this a while back as an example of how to do a worms like game/aim system.
It generates all the gfx(if you can call them that) in code.
Mouse wheel changes power, left mouse in to aim and let go to fire. The only remotely cool thing is the bullet projection, which draws a line showing where the bullet will go and marks a red x where it'll land...
It's open source/freeware/whatever. Didn't feel like something worth putting in code archives.
;-
;-Tank V Tank.
;-
;-The Ancient Art of Modern Warfare.
;-
;
;-
Graphics 640,480,16,2
SetBuffer BackBuffer()
HidePointer
;-
Dim land#(640)
Global lStrip
;--Define variables. Using locals rather than types, so it's easier to 'get'.
genLevel()
;-
newTank(Input("Your name>"),cHuman)
numBots=5
For j=1 To numBots
newTank("Ai Bot "+j,cAi)
Next
;--
;-[ Game States]
Const gPlayer=1,gAi=2 ;Bullets are indepent
;-[ Game state]
Global gState =gPlayer
;-
cTank.Tank =First tank ;ctank = current tank whose turn it is.
While Not KeyDown(1):Cls
;-
mx#=MouseX()
my#=MouseY()
;-Update AI - Kept within main loop as it shows
;how to find a 'ideal' arc etc.
;--
Select gState
End Select
;-
renderLand() ;<- Render level graphics
renderTanks()
;- draw target cursor
Color 255,255,255
Line mx-4,my-4,mx+4,my+4
Line mx+4,my-4,mx-4,my+4
dx#=mx-ctank\x
dy#=my-ctank\y
ang#=ATan2(dy,dx)
If ang<0 ang=ang+360
pwr#=MouseZ()*0.01
pwr=pwr+powI#
lx#=ctank\x
ly#=ctank\y-10
xi#=Cos(ang)*pwr
yi#=Sin(ang)*pwr
xi=xi*20
yi=yi*20
col=False
skip=False
cx#=0
cy#=0
ctank\ang=ang
For j=1 To 1860 Step 10
ax#=lx+xi
ay#=ly+yi
If ax>640 Or ax<0 Or ay>480 skip=True
If land(ax)<ay
;-find col spot.
If xi>0
For cx=ax To 0 Step -1
If land(cx)>ay
Else
colX#=cx
colY#=land(cx)
Exit
EndIf
Next
Else
For cx=ax To 639
If land(cx)>ay
Else
colX#=cx
colY#=land(cx)
Exit
EndIf
Next
EndIf
Color 255,0,0
Line colX-5,colY-5,colX+5,colY+5
Line colX-5,coly+5,colX+5,colY-5
skip=True
ax=colX
ay=colY
EndIf
Line lx+xi*0.25,ly+yi*0.25,ax-xi*0.25,ay-yi*0.25
If yi#<6
yi=yi+0.1
EndIf
lx=ax
ly=ay
If skip Exit
Next
Select cTank\cpu ;
Case cHuman
If MouseDown(1)
If firstS=True
powI#=Cos(pang#)*0.01
pang#=pang+2
Else
powI#=0
pAng#=0
firstS=True
EndIf
Else
If firsts
bul(cTank,pwr)
firsts=0
Else
EndIf
EndIf
Case cAi
End Select
updateBuls()
renderBul()
;-
;-
Flip
Wend
Type bul
Field x#,y#,xi#,yi#
Field lx#,ly#,age#
End Type
Function bul(from.tank,pwr#=0)
n.bul=New bul
pwr=pwr*6
n\x=from\x
n\y=from\y-10
n\xi=Cos(from\ang)*pwr
n\yi=Sin(from\ang)*pwr
n\lx=n\x
n\ly=n\y
n\age#=1
End Function
Function updateBuls()
For b.bul =Each bul
b\lx=b\x
b\ly=b\y
b\x=b\x+b\xi
b\y=b\y+b\yi
If b\yi<6 b\yi=b\yi+0.01
b\age=b\age*0.995
If b\x<0 Or b\x>640
Delete b
Else If b\y>land(b\x)
;explode
Delete b
EndIf
Next
End Function
Function renderBul()
For b.bul=Each bul
cv#=255.*b\age
Color cv,cv,cv
Line b\x+b\xi*8,b\y+b\yi*8,b\lx,b\ly
Next
End Function
;-Engine funcs and globs
Function genLevel(seed=-1)
Local sy#=128
If seed=-1 SeedRnd MilliSecs() Else SeedRnd seed
Dim land#(640) ;reset land col array.
lStrip=CreateImage(640,250)
SetBuffer ImageBuffer(lSTrip)
LockBuffer ImageBuffer(lStrip)
For x=0 To 639
If Rand(5)=5
yAnc#=yAnc+Rnd(10)
EndIf
sy#=sy+Cos(yAnc)
If sy>245 sy=245 Else If sy<5 sy=5
land(x)=sy+230
cI#=(250-sy)/255.
cv#=64
For ay=sy To 250
WritePixelFast x,ay,(cv) Or (cv Shl 8) Or (cv Shl 16)
cv=cv+ci
If cv>255 cv=255 ;a just in case..
Next
Next
UnlockBuffer
SetBuffer BackBuffer()
Return RndSeed ;return unique id.
End Function
;-
Type tank
Field x#,y# ;Y is determined by map.
Field ang#,cpu,name$
End Type
Const cHuman=1,cAI=2
Function newTank(name$="",cpu=cHuman,failOn=0)
out.tank=New tank
If failOn>512 RuntimeError "Screen is too cluttered. Less tanks!"
out\name=name
out\cpu=cpu
out\x=Rand(5,635)
For c.tank=Each tank
If c<>out
If Abs(c\x-out\x)<40 ;
bail=True
newTank(name,cpu,failon+1) ;recursive, doesn't create more than requested.
Exit
EndIf
EndIf
Next
out\y=land(out\x)
If bail
Delete out
Else
Return Handle(out)
EndIf
End Function
Function renderLand()
SeedRnd 102 ;
LockBuffer
For j=0 To Rnd(500) ;draw crappy little starfield to give it a bit o depth.
x#=Rnd(640)
y#=Rnd(380)
cv=Rnd(100,255)
WritePixelFast x,y,cv Or (cv Shl 8) Or (cv Shl 16)
Next
UnlockBuffer
DrawImage lStrip,0,230
End Function
;-
Const green=0 Or (255 Shl 8) Or (0 Shl 16)
Const white=255 Or (255 Shl 8) Or (255 Shl 16)
;-
Function renderTanks()
Color 0,255,0
For t.tank=Each tank
Color 255*(t\cpu=cAi),0,green*(t\cpu=cHuman)
Line t\x-10,t\y,t\x,t\y-10
Line t\x+10,t\y,t\x,t\y-10
Line t\x-10,t\y,t\x+10,t\y
Color 0,0,white
Line t\x,t\y-10,t\x+Cos(t\ang)*8,(t\y-10)+Sin(t\ang)*8
Text t\x-10,t\y-25,t\name
Next
End Function
|