QB64.org Forum

Active Forums => Programs => Topic started by: bplus on June 11, 2021, 07:51:51 pm

Title: Missile Command
Post by: bplus on June 11, 2021, 07:51:51 pm
A simple little game (I just learned about at LB):
Code: QB64: [Select]
  1. W = 800: H = 600
  2. Screen _NewImage(800, 600, 32)
  3. a:
  4. a = Rnd * W: b = 0: c = Rnd * 6 - 3: d = Rnd * 3 + 3: u = 0: v = 0: x = 400: y = H
  5.     _Title "MC hits:" + Str$(t) + ", misses:" + Str$(m)
  6.     If _MouseButton(1) Then e = _MouseX - 400: f = _MouseY - H: z = (e ^ 2 + f ^ 2) ^ .5: u = 5 * e / z: v = 5 * f / z
  7.     x = x + u: y = y + v: a = a + c: b = b + d
  8.     If x < 0 Or y < 0 Or a < 0 Or b < 0 Or x > W Or a > W Or b > H Then
  9.         If b > H Or x < 0 Or y < 0 Or x > W Then m = m + 1
  10.         GoTo a:
  11.     End If
  12.     If ((x - a) ^ 2 + (y - b) ^ 2) ^ .5 < 20 Then
  13.         For r = 1 To 20 Step 4
  14.             Circle ((x + a) / 2, (y + b) / 2), r
  15.             _Limit 60
  16.         Next
  17.         t = t + 1: GoTo a:
  18.     Else
  19.         PSet (x, y): PSet (a, b)
  20.     End If
  21.     _Limit 20
  22.  

Your missile base is at bottom, middle of screen. When you click mouse you want your missile to strike the incoming one before it reaches the bottom of screen. If incoming is not going to reach bottom don't worry about it unless you can hit it for sure (not likely).
Title: Re: Missile Command
Post by: Dav on June 12, 2021, 11:32:30 am
Nice compact code.  My first try of 20 was horrible.  4 hits and 16 misses.  No wonder my grand-dad never took me hunting. 

By LB do you mean Liberty Basic?

- Dav
Title: Re: Missile Command
Post by: bplus on June 12, 2021, 01:10:32 pm
Nice compact code.  My first try of 20 was horrible.  4 hits and 16 misses.  No wonder my grand-dad never took me hunting. 

By LB do you mean Liberty Basic?

- Dav

Yes Liberty but actually I am too cheap to spring for Liberty so I use Just Basic the trainer for Liberty which is pretty standard Basic until you get into the Windows part then it is very idiosyncratic.

I am planning heat seeking missile code next so we hopeless human shooters have a better shot at survival.

Can you believe 25 lines of code could offer such an intriguing challenge? :)
Title: Re: Missile Command
Post by: bplus on June 12, 2021, 01:14:27 pm
BTW you can click again and change course, usually it's too late but sometimes it helps.

PS compact code because the challenge was 1K bytes.
Title: Re: Missile Command
Post by: bplus on June 12, 2021, 01:26:53 pm
QB64 version comes in at 842 bytes because IDE formats with spaces, Steve could probably get down to 500 :)
JB version was 757 bytes but different commands for their Windows (what we call Screens over here).

Title: Re: Missile Command
Post by: bplus on June 13, 2021, 01:10:06 am
AIM - AI Missiles Defense AKA War and Peace

Code: QB64: [Select]
  1. W = 800: H = 600
  2. Screen _NewImage(800, 600, 32)
  3. Color &HFFFFFFFF, &HFF6666DD
  4. a:
  5. a = Rnd * W: b = 0: c = Rnd * 6 - 3: d = Rnd * 3 + 3: x = 400: y = H: lastx = x: lasty = y: lasta = a: lastb = b: l = 5
  6. l = l * 10
  7.     _Title "AIM-AI Missile Defense - Hits:" + Str$(t) + ", Misses:" + Str$(m)
  8.     a = a + c: b = b + d: ang = _Atan2(b - y, a - x)
  9.     x = x + 7 * Cos(ang): y = y + 7 * Sin(ang)
  10.     If x < 0 Or y < 0 Or a < 0 Or b < 0 Or x > W Or a > W Or b > H Then
  11.         If b > H Or x < 0 Or y < 0 Or x > W Then m = m + 1
  12.         GoTo a:
  13.     End If
  14.     If ((x - a) ^ 2 + (y - b) ^ 2) ^ .5 < 10 Then
  15.         For r = 1 To 20 Step 4
  16.             Circle ((x + a) / 2, (y + b) / 2), r, &HFFFF0000
  17.             _Limit 10
  18.         Next
  19.         t = t + 1: GoTo a:
  20.     Else
  21.         Line (x, y)-(lastx, lasty), &HFF006600: Line (a, b)-(lasta, lastb), &HFFDDDDFF
  22.         lastx = x: lasty = y: lasta = a: lastb = b
  23.     End If
  24.     _Limit l
  25. Loop Until t = 50
  26.  
  27.  

It stops after 50 hits, note the number of misses.
Title: Re: Missile Command
Post by: johnno56 on June 13, 2021, 07:51:27 am
I let it run "as is" for all 50 "shots". Impressive. If this program does not develop into a full game, the program as it stands, makes a pretty good version of a digital flower arranger... lol

Nah seriously... It would be cool to develop a "basic" based MC game...  I like the "heat seekers"... Perhaps can be added as a bonus for reaching either a higher score or "hitting" specific "power ups"... Too much too soon? I am intrigued in this program. Time to research other "basic" versions for ideas... or are you trying to produce a militaristic (as is file size) game?
Title: Re: Missile Command
Post by: bplus on June 13, 2021, 09:46:43 am
Quote
the program as it stands, makes a pretty good version of a digital flower arranger... lol

Yes, that was the Peace part of War and Peace :)