Author Topic: Shoot The Alien With Your Tank Mouse Game  (Read 3213 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Shoot The Alien With Your Tank Mouse Game
« on: November 13, 2020, 04:58:00 pm »
Petr gave me this tank code the other day so I figured I should make something out of it. It's not a complicating game, random alien appears, you shoot him. lol I could probably make something more complicating sometime but I just threw this together today. Do you like the alien? lol I also used my new circle fill-in SUB that B+ gave me for the alien. Most of the code in this is from Petr. Thanks Petr! Oh also, I added the tank movement, so it goes toward where you aim at with the mouse.
I should also say that there's no real objective to this game except shooting the alien as many times as you want. You don't die and there's no time limit. But it does keep a record of how many times you shot him in the Title bar.

To play: Use the mouse to move and aim. Shoot with the left mouse button.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 768, 32)
  2.  
  3. TYPE xy
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.  
  7. DIM position AS INTEGER 'which fire image is last viewed?
  8. DIM firepoints(100) AS xy '  ---> is the same as 30 different varibles for X, and for Y. Number in parentheses say you, which number it is. so
  9. '                           firepoints(10).x is the same as x10, firepoints(10).y the same as y10, firepoints(1).time is the same as time1....
  10.  
  11. 'set canon position
  12. cannonX = 512
  13. cannonY = 384
  14.     playerX = _MOUSEX
  15.     playerY = _MOUSEY
  16.     mouseLeftButton = _MOUSEBUTTON(1)
  17.     CLS
  18.     aliens = INT(RND * 1000)
  19.     IF aliens > 992 OR nextalien = 1 THEN
  20.         nextalien = 0
  21.         al = 0
  22.         makealien:
  23.         cx = RND * _WIDTH
  24.         cy = RND * _HEIGHT
  25.         IF cx < cannonX + 60 AND cx > cannonX - 60 AND cy > cannonY + 60 AND cy < cannonY - 60 THEN GOTO makealien:
  26.         r = 15
  27.         c = _RGB32(0, 255, 0)
  28.     END IF
  29.  
  30.  
  31.  
  32.     'get angle between player and canon
  33.     angle = _ATAN2(playerY - cannonY, playerX - cannonX)
  34.  
  35.     'draw canon
  36.     CIRCLE (cannonX, cannonY), 30, _RGB32(127)
  37.     LINE (cannonX, cannonY)-(cannonX + COS(angle) * 50, cannonY + SIN(angle) * 50)
  38.  
  39.     'calculate points for fire (points in line between current cannon angle and last position. Let say, maximal fire lenght is 300 (calculating here is for 30 steps)
  40.     FOR calcFireRoute = 1 TO 30
  41.         RouteLenght = 30 + (calcFireRoute * 20) '10 * 30 = 300
  42.         IF firepoints(calcFireRoute).x = 0 THEN
  43.             firepoints(calcFireRoute).x = cannonX + COS(angle) * RouteLenght
  44.  
  45.         END IF
  46.         'calcFireRoute is just number. Number 1 to 30. So is this number is 1, you do X1 and Y1. If is 2, you do X2 and Y2....
  47.  
  48.         IF firepoints(calcFireRoute).y = 0 THEN
  49.             firepoints(calcFireRoute).y = cannonY + SIN(angle) * RouteLenght
  50.         END IF
  51.         'is used the same principle as calculating for barrel
  52.  
  53.     NEXT
  54.     cannonX = cannonX + COS(angle)
  55.     cannonY = cannonY + SIN(angle)
  56.     position = position + 1
  57.  
  58.     IF position > 30 THEN
  59.         position = 1 'number in parenthesis is named "index". Maximal index number for array firepoints is 30. This number say us, which position is actual
  60.  
  61.         'previous position must be set back to zero, for new calculating (see conditions in FOR NEXT loop)
  62.         firepoints(30).x = 0 'set first X to zero
  63.         FOR setZero = 1 TO 30
  64.             firepoints(setZero).x = 0
  65.             firepoints(setZero).y = 0
  66.         NEXT
  67.     END IF
  68.     IF mouseLeftButton THEN
  69.         'draw fire
  70.         CIRCLE (firepoints(position).x, firepoints(position).y), 10, &HFFFF0000 'fire color is red (FF = 255, it is &H ALPHA, RED, GREEN, BLUE), here is full ALPHA and full RED. How find, what is FF: Try PRINT VAL("&HFF")
  71.         PAINT (firepoints(position).x, firepoints(position).y), &HFF506070, &HFFFF0000
  72.         IF cx > firepoints(position).x - 20 AND cx < firepoints(position).x + 20 AND cy > firepoints(position).y - 20 AND cy < firepoints(position).y + 20 THEN
  73.             al = 1: score = score + 1: score$ = STR$(score): _TITLE score$
  74.             FOR snd = 600 TO 500 STEP -50
  75.                 SOUND snd, .5
  76.             NEXT snd
  77.             nextalien = 1
  78.         END IF
  79.     END IF
  80.  
  81.  
  82.     'draw player
  83.     CIRCLE (playerX, playerY), 20, _RGB32(127, 65, 78)
  84.     'draw alien
  85.     IF al = 0 THEN fillCircle cx, cy, r, c
  86.     LINE (cx - 5, cy - 5)-(cx - 3, cy - 3), _RGB32(0, 0, 0), BF
  87.     LINE (cx + 5, cy - 5)-(cx + 3, cy - 3), _RGB32(0, 0, 0), BF
  88.     LINE (cx - 5, cy + 7)-(cx + 5, cy + 5), _RGB32(0, 0, 0), BF
  89.     LINE (cx - 5, cy - 7)-(cx - 10, cy - 20), _RGB32(0, 255, 0)
  90.     CIRCLE (cx - 12, cy - 22), 3, _RGB32(0, 255, 0)
  91.     LINE (cx + 5, cy - 7)-(cx + 10, cy - 20), _RGB32(0, 255, 0)
  92.     CIRCLE (cx + 12, cy - 22), 3, _RGB32(0, 255, 0)
  93.     _DISPLAY
  94.  
  95.     _LIMIT 120
  96.  
  97.  
  98. 'from Steve Gold standard
  99. SUB fillCircle (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  100.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  101.     DIM X AS INTEGER, Y AS INTEGER
  102.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  103.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  104.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  105.     WHILE X > Y
  106.         RadiusError = RadiusError + Y * 2 + 1
  107.         IF RadiusError >= 0 THEN
  108.             IF X <> Y + 1 THEN
  109.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  110.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  111.             END IF
  112.             X = X - 1
  113.             RadiusError = RadiusError - X * 2
  114.         END IF
  115.         Y = Y + 1
  116.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  117.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  118.     WEND
  119.  
« Last Edit: November 13, 2020, 05:04:50 pm by SierraKen »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Shoot The Alien With Your Tank Mouse Game
« Reply #1 on: November 14, 2020, 03:18:21 am »
Nice work, SierraKen :)

Offline mynameispaul

  • Newbie
  • Posts: 49
    • View Profile
Re: Shoot The Alien With Your Tank Mouse Game
« Reply #2 on: November 14, 2020, 11:43:54 am »
very entertaining
thanks

Offline Aurel

  • Forum Regular
  • Posts: 167
    • View Profile
Re: Shoot The Alien With Your Tank Mouse Game
« Reply #3 on: November 15, 2020, 03:54:10 am »
yes ..this one work in my win7 --- 1.3 qb64_win32 version
//////////////////////////////////////////////////////////////////
https://aurelsoft.ucoz.com
https://www.facebook.com/groups/470369984111370
//////////////////////////////////////////////////////////////////

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Shoot The Alien With Your Tank Mouse Game
« Reply #4 on: November 15, 2020, 02:02:14 pm »
Thanks everyone. :)