_TITLE "Tanks Battle! by bplus 2018-02-03" 'from: Tanks Battle.sdlbas (B+=MGA) 2016-10-29
' let the projectiles fly!
'screen stuff
'tank stuff
CONST tN
= 15 'number of tanks CONST tNm1
= tN
- 1 ' for loops and arrays CONST tW
= 20 'width of tank CONST tH
= 8 'height of tank
'hole stuff
'projectile stuff
ff$ = "arial" ' I have loaded and tested many fonts from Windows Folder and disappointed how few work with QB64
'load and check Big size font
bArial&
= _LOADFONT("C:\windows\fonts\" + ff$
+ ".ttf", 48, "BOLD")'bFW = _FONTWIDTH(bArial&): bFH = _FONTHEIGHT(bArial&)
'_FONT bArial&
'LOCATE 1, 1: PRINT "This is BIG font."
'SLEEP
'get holes set up
holeIndex = -1
drawLandscape
initializeTanks
hotTank = tNm1
change = 1
WHILE change
'get tanks landed before start shooting change = 0
_PUTIMAGE , land&
, 0 'land the tanks and reland the tanks if the dirt is shot out under them IF POINT(tanks
(i
).x
+ tW
/ 2, tanks
(i
).y
+ tH
+ 1) = skyC
THEN tanks(i).y = tanks(i).y + 2
change = 1
drawTank i
WHILE 1 '< main loop start
'the land with holes
drawHole holes(ii).x, holes(ii).y
'reland the tanks if the dirt is shot out under them
WHILE POINT(tanks
(i
).x
+ tW
/ 2, tanks
(i
).y
+ tH
+ 1) = skyC
tanks(i).y = tanks(i).y + 2
'repoint barrels and reset velocitys
IF RND < .5 THEN 'avoid straight up and down suicide shots tanks(i).da = rand(leftA, -92)
tanks(i).da = rand(rightA, -88)
tanks(i).v = rand(lVel, hVel) 'velocity
drawTank i
''whose turn to shoot
lastMan = hotTank
hotTank = hotTank + 1
WHILE tanks
(hotTank
).f
= 1 'look for a tank still alive hotTank = hotTank + 1 'whose turn to shoot
'did we cycle through all the dead tanks?
IF hotTank
= lastMan
THEN 'game over, last man standing
'setup hotTank's shot
rAngle = tanks(hotTank).da * rad 'convert here to radians for SIN and COS
pX = tanks(hotTank).bx
pY = tanks(hotTank).by
pX_change
= tanks
(hotTank
).v
* COS(rAngle
) 'this is the cuurent X vector of the projectile pY_change
= tanks
(hotTank
).v
* SIN(rAngle
) ' this is the current Y vector of the projectile pActive = 0 ' do not Activate until projectile sees the skyC
pY_change = pY_change + gravity ' pY starts in upward direction but will eventually fall due to gravity
pX = pX + pX_change
pY = pY + pY_change
'show projectile progress, hit or air
'check for tank hit
IF tanks
(iTank
).f
<> 1 AND pActive
THEN 'tanks can blow up themselves IF dist
(pX
, pY
, tanks
(iTank
).x
+ tW
/ 2, tanks
(iTank
).y
+ tH
/ 2) < hR
THEN tanks(iTank).f = 1
fcirc pX, pY, rr
holeIndex = holeIndex + 1
holes(holeIndex).x = pX
holes(holeIndex).y = pY
drawHole pX, pY
pX = SW + 10
pY = SH + 10
pActive = 1
fcirc pX, pY, 2 ' <<<<<<<<<<<<<<<< to see round projectiles that could be replaced by image
'still hot but cant see
fcirc pX, pY, rr
holeIndex = holeIndex + 1
holes(holeIndex).x = pX
holes(holeIndex).y = pY
drawHole pX, pY
pX = SW + 10
pY = SH + 10
fcirc xx, i, hR
'the sky
LINE (0, 0)-(SW
, SH
), skyC
, BF
'the land
startH = SH - 100
rr = 70: gg = 70: bb = 90
Xright = 0
y = startH
' upDown = local up / down over range, change along Y
' range = how far up / down, along X
upDown
= (RND * (.8) - .35) * (mountain
* .5) range = Xright + rand%(15, 25) * 2.5 / mountain
FOR x
= Xright
- 1 TO range
y = y + upDown
Xright = range
rr = rand(rr - 15, rr): gg = rand(gg - 15, gg): bb = rand(bb - 25, bb)
startH = startH + rand(5, 20)
SUB initializeTanks
' x, y, barrel angle, velocity, color tl = (SW - tW) / tN: tl2 = tl / 2: tl4 = .8 * tl2
tanks(i).x = rand%(tl2 + tl * i - tl4 - tW, tl2 + tl * i + tl4 - tW)
tanks(i).y = 300 '<<<<<<<<<<<<<<<<<<<<<<<<<< for testing
tanks(i).da = rand%(-180, 0) 'degree Angle
tanks(i).v = rand%(10, 20) 'velocity
IF tanks
(i
).da
< -90 THEN 'barrel is pointed left tanks(i).v = -1 * tanks(i).v
tc
= i
* INT(200 / (3 * tN
)) 'maximize color difference between tanks tanks
(i
).c
= _RGB32(55 + 2 * tc
, 13 + tc
, 23 + tc
) ' first tank is darkest 'shuffle color order
r = rand%(0, i)
SWAP tanks
(i
).x
, tanks
(r
).x
'ink(tanks(i, "c"))
'turret
fEllipse tanks(i).x + tW / 2, tanks(i).y + tH / 3, tW / 4 + 1, tH / 4 + 1
bX
= tW
/ 2 * COS(rad
* tanks
(i
).da
) bY
= tW
/ 2 * SIN(rad
* tanks
(i
).da
) LINE (tanks
(i
).x
+ tW
/ 2, tanks
(i
).y
+ tH
/ 3)-(tanks
(i
).x
+ tW
/ 2 + bX
, tanks
(i
).y
+ tH
/ 4 + bY
) LINE (tanks
(i
).x
+ tW
/ 2 + 1, tanks
(i
).y
+ tH
/ 3 + 1)-(tanks
(i
).x
+ tW
/ 2 + bX
+ 1, tanks
(i
).y
+ tH
/ 4 + bY
+ 1) tanks(i).bx = tanks(i).x + tW / 2 + bX
tanks(i).by = tanks(i).y + tH / 4 + bY
fEllipse tanks(i).x + tW / 2, tanks(i).y + .75 * tH, tW / 2, tH / 4
ellipse tanks(i).x + tW / 2, tanks(i).y + .75 * tH, tW / 2 + 1, tH / 4 + 1
ellipse tanks(i).x + tW / 2 + 1, tanks(i).y + .75 * tH, tW / 2 + 1, tH / 4 + 1
rand%
= (RND * (hi%
- lo%
+ 1)) \
1 + lo%
dist# = ((x1% - x2%) ^ 2 + (y1% - y2%) ^ 2) ^ .5
'Steve McNeil's copied from his forum note: Radius is too common a name
RadiusError = -subRadius
X = subRadius
Y = 0
' Draw the middle span here so we don't draw it twice in the main loop,
' which would be a problem with blending turned on.
LINE (CX
- X
, CY
)-(CX
+ X
, CY
), , BF
RadiusError = RadiusError + Y * 2 + 1
LINE (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), , BF
LINE (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), , BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
LINE (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), , BF
LINE (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), , BF
scale = yRadius / xRadius
LINE (CX
, CY
- yRadius
)-(CX
, CY
+ yRadius
), , BF
y
= scale
* SQR(xRadius
* xRadius
- x
* x
) LINE (CX
+ x
, CY
- y
)-(CX
+ x
, CY
+ y
), , BF
LINE (CX
- x
, CY
- y
)-(CX
- x
, CY
+ y
), , BF
scale = yRadius / xRadius: xs = xRadius * xRadius
PSET (CX
, CY
- yRadius
):
PSET (CX
, CY
+ yRadius
) lastx = 0: lasty = yRadius
y
= scale
* SQR(xs
- x
* x
) LINE (CX
+ lastx
, CY
- lasty
)-(CX
+ x
, CY
- y
) LINE (CX
+ lastx
, CY
+ lasty
)-(CX
+ x
, CY
+ y
) LINE (CX
- lastx
, CY
- lasty
)-(CX
- x
, CY
- y
) LINE (CX
- lastx
, CY
+ lasty
)-(CX
- x
, CY
+ y
) lastx = x: lasty = y