Author Topic: Cave Fighter - Side Scrolling Action Game  (Read 7685 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Cave Fighter - Side Scrolling Action Game
« on: November 09, 2020, 07:08:27 pm »
For almost a week I've been working hard on this game. I wanted to see if I could use the new scrolling map method that Felippe told us about recently and turn it into a spaceship game. In this game you control your tiny red ship with the arrow keys or WASD keys and press the space bar to fire. The object is to get to the end of each cave level. There's 3 cave levels included here that I made using my graphics program Paint.Net. I'm no artist but I tried my best :). One trick you will probably need to do is shoot your laser through the mountains to hit some of the shooting cannons. You don't destroy rocks but the laser goes straight through anything and you can shoot it in the 4 directions you can move with. I hope you all enjoy this game, it's one of the biggest games I've ever made. I realize that most likely the lines of code could be reduced dramatically (it has around 1000 lines) but I don't know how to reduce the number of variables yet like many of you guys do. The player.x, player.y, camera.x, camera.y variables I got directly from Felippe's original code. Which was a trick in itself to figure out what used 800 x 600 and what used the 4000 x 600 level picture files when adding more graphics to it with the game. I still don't understand it completely but I figured it out enough to use. Enjoy the game and please leave comments, thanks. All the files are in the zip file in attachments as well as a picture I will add. Oh also, I'm sure you all will laugh at the look of the ship, but like I said I'm no artist and I think it goes good with the laser sound. lol Oh one more thing, I added a lot of comments in the code on how it's very easy to add more levels. I used a separate program I made to detect coordinates with the mouse on the picture files so I could plot and display each cannon and their cannonballs.

Edit: I forgot to mention that to Pause the game, press P. I just used the SLEEP command for that. So you can press any other key to turn Pause off.

Edit Again: The zip file is updated to now have 5 levels of caves and a new looking spaceship that B+ let me use. :)

Edit Again: This zip file has been updated again to fix a problem that slows down the game if you sit in one place for a long time.
Cave Fighter by SierraKen 2.png
* Cave Fighter by SierraKen 2.png (Filesize: 32.46 KB, Dimensions: 799x627, Views: 202)
* Cave Fighter.zip (Filesize: 192.21 KB, Downloads: 134)
« Last Edit: November 14, 2020, 12:47:09 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #1 on: November 10, 2020, 01:07:00 am »
Cool game. Took me a few lives to figure out the timing of enemy bullets... Perhaps, to make it a little harder - after a few levels, placing some enemies on the cave ceiling firing down to the right or perhaps increasing bullet speed based on level number? But still a cool game!
Logic is the beginning of wisdom.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #2 on: November 10, 2020, 12:20:30 pm »
Hi, nice game. It is a pity that the SOUND command suspends the execution of the program, unlike QBasic 4.5. I sent you something in a private message, maybe it's not too hard. You inspired me with this game for it :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #3 on: November 10, 2020, 12:29:07 pm »
Thanks guys! :) Johnno, adding more cannons would make it a lot harder, I probably won't.
Thanks Petr, I'll check and see what you gave me.
B+ is probably having a hard time with the keys again. I might try to make a mouse version for him if I can.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #4 on: November 10, 2020, 12:59:22 pm »
It's OK Ken, WASD is actually better than arrow keys specially with practice.

Since you are a friend I will lend you my ship you can use. I am sure the explosions and crashes will buff out ;-))

Here is SUB drawShip demo with 2 handy helper subs specially fcirc for fast draw of circle fills:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1024, 700, 32)
  2. _DELAY .25
  3.  
  4. DIM sc AS _UNSIGNED LONG ' ship color
  5. sc = _RGB32(RND * 215 + 40, RND * 255, RND * 255)
  6.     CLS
  7.     lc = lc + 1 'loop counter
  8.     IF lc > 3 * 60 THEN sc = _RGB32(RND * 215 + 40, RND * 255, RND * 255): lc = 0 'every 3 secs change ship color
  9.     drawShip _MOUSEX, _MOUSEY, sc ' here is how to call drawShip   easier than using circle
  10.     _DISPLAY
  11.     _LIMIT 60
  12.  
  13. SUB drawShip (x, y, colr AS _UNSIGNED LONG) 'shipType     collisions same as circle x, y radius = 30
  14.     STATIC ls
  15.     DIM light AS LONG, r AS LONG, g AS LONG, b AS LONG
  16.     r = _RED32(colr): g = _GREEN32(colr): b = _BLUE32(colr)
  17.     fellipse x, y, 6, 15, _RGB32(r, g - 120, b - 100)
  18.     fellipse x, y, 18, 11, _RGB32(r, g - 60, b - 50)
  19.     fellipse x, y, 30, 7, _RGB32(r, g, b)
  20.     FOR light = 0 TO 5
  21.         fcirc x - 30 + 11 * light + ls, y, 1, _RGB32(ls * 50, ls * 50, ls * 50)
  22.     NEXT
  23.     ls = ls + 1
  24.     IF ls > 5 THEN ls = 0
  25.  
  26.  
  27. ' ======== helper subs for drawShip that you can use for other things specially fcirc = fill_circle  x, y, radius, color
  28.  
  29. SUB fellipse (CX AS LONG, CY AS LONG, xr AS LONG, yr AS LONG, C AS _UNSIGNED LONG)
  30.     IF xr = 0 OR yr = 0 THEN EXIT SUB
  31.     DIM x AS LONG, y AS LONG
  32.     w2 = xr * xr: h2 = yr * yr: h2w2 = h2 * w2
  33.     LINE (CX - xr, CY)-(CX + xr, CY), C, BF
  34.     DO WHILE y < yr
  35.         y = y + 1
  36.         x = SQR((h2w2 - y * y * w2) \ h2)
  37.         LINE (CX - x, CY + y)-(CX + x, CY + y), C, BF
  38.         LINE (CX - x, CY - y)-(CX + x, CY - y), C, BF
  39.     LOOP
  40.  
  41. SUB fcirc (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG) 'vince version  fill circle x, y, radius, color
  42.     DIM x0 AS LONG, y0 AS LONG, e AS LONG
  43.     x0 = R: y0 = 0: e = 0
  44.     DO WHILE y0 < x0
  45.         IF e <= 0 THEN
  46.             y0 = y0 + 1
  47.             LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
  48.             LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
  49.             e = e + 2 * y0
  50.         ELSE
  51.             LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
  52.             LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
  53.             x0 = x0 - 1: e = e - 2 * x0
  54.         END IF
  55.     LOOP
  56.     LINE (x - R, y)-(x + R, y), C, BF
  57.  
  58.  

Maybe 30 Radius too big, we can shrink down?
« Last Edit: November 10, 2020, 01:05:07 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #5 on: November 10, 2020, 05:20:00 pm »
THANKS B+! Your spaceship really livens up my game a lot. I had to shrink it by 1/3 to fly easier in the caves but it works great now. I also widened the tops of the caves some. I also added 2 more levels making 5 total (70 cannons total!) Plus I shrank the cannonballs from 5 pixels to 2 pixels to make it easier as well. I moved the laser to fit the ship better too.
Petr gave me code on how to make a 360 degree turning cannon and I added code to his making the tank (or ship) move toward where it was pointing. I tried adding that to this game but using a mouse with a scrolling map is very hard and I know it wouldn't work well with that. So I'll use Petr's code on another game sometime.

Tell me what you all think, I'll replace the last zip with this one too and also it will be below on this post.

Cave Fighter by SierraKen 2.png
* Cave Fighter by SierraKen 2.png (Filesize: 32.46 KB, Dimensions: 799x627, Views: 144)
* Cave Fighter.zip (Filesize: 192.21 KB, Downloads: 121)
« Last Edit: November 14, 2020, 12:47:40 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #6 on: November 10, 2020, 05:33:27 pm »
Hey I like what you've done with the ship!

"Mark what's that noise?" (as the dog is running upstairs WTH?)

Just playing a game, mom! ;-))

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #7 on: November 10, 2020, 05:35:53 pm »
LOL Thanks B+!. :)) I just added a picture to my last post and also an updated picture on the first post.
« Last Edit: November 10, 2020, 05:36:55 pm by SierraKen »

Marked as best answer by SierraKen on November 10, 2020, 03:20:54 pm

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #8 on: November 10, 2020, 08:18:30 pm »
I came across a small problem that slows the game down tremendously if you let it sit for around 10 minutes. I believe it was because I put the limit on cannonballs way too high, it was at around 4000 for each cannon, so I reduced each one to 100 (dimmed at 200) and I added code to erase them all once it gets to 100 and then it starts over. It looks like I fixed it mostly. I haven't done a big test on it but it's better than it was at least.

Here is the updated attachment zip file. I'll update the other ones as well.

* Cave Fighter.zip (Filesize: 192.21 KB, Downloads: 121)
« Last Edit: November 14, 2020, 12:48:10 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #9 on: November 11, 2020, 08:05:41 am »
Cool... Much improved. I got through all 5 levels but still lost a life....

I don't know if this feature was deliberate, to make it easier, but some of the 'shots' could pass through the cavern and hit the target. I'm not complaining... Got me out of some tricky spots at times... nudge, nudge, wink, wink... The 'ship' looks 'very' familiar... Any ideas bplus? lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #10 on: November 11, 2020, 11:18:21 am »
Quote
The 'ship' looks 'very' familiar... Any ideas bplus? lol

SmallBASIC Space Shooter Game - target practice with plasma laser cannon! I believe it was translated SdlBasic as well. Aurel requested a space shooter game, the ship got created then, so with Asteroids this is 4th appearance at least. When you find a good thing stick with it!

Speaking of plasma laser cannon... if you fly the ship with WASD you have mouse hand free to aim the plasma laser cannon. :)

@SierraKen are you using the the fcirc SUB to make solid circles, it is fastest method, faster than paint or drawing circles in a loop. Oh speaking of cannon balls... ;-))
« Last Edit: November 11, 2020, 11:22:42 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #11 on: November 11, 2020, 12:22:58 pm »
Johno, yes that's the trick I left in so people can shoot the cannons easier. :)

B+, yes but only on the saucer, the rest I just kept as my usual sz loops. :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #12 on: November 11, 2020, 01:18:37 pm »
Quote
B+, yes but only on the saucer, the rest I just kept as my usual sz loops. :)

Yes there is an opportunity to speed things up! Whenever you need a filled in circle use: fcirc x, y, r, color
You can cut some time of drawing.


Here is Plasma Laser Cannon demo with some balls for targets:
Code: QB64: [Select]
  1. _TITLE "Plasma Laser Cannon demo" 'b+ 2020-11-11
  2. SCREEN _NEWIMAGE(1024, 700, 32)
  3. _DELAY .25
  4.  
  5. DIM SHARED tx, ty, tr, tc AS _UNSIGNED LONG
  6. newTarget
  7.     CLS
  8.     PRINT tx, ty, tr, tc
  9.     drawBall tx, ty, tr, tc
  10.     WHILE _MOUSEINPUT: WEND 'aim with mouse
  11.     mx = _MOUSEX: my = _MOUSEY: mb = _MOUSEBUTTON(1)
  12.     IF mb THEN
  13.         PLC _WIDTH / 2, _HEIGHT / 2, _MOUSEX, _MOUSEY, tr
  14.         drawShip _WIDTH / 2, _HEIGHT / 2, &HFF66FF88
  15.         _DISPLAY
  16.     ELSE
  17.         drawShip _WIDTH / 2, _HEIGHT / 2, &HFF66FF88
  18.     END IF
  19.     IF _HYPOT(mx - tx, my - ty) < tr AND mb THEN
  20.         FOR r = 0 TO 255
  21.             fcirc tx, ty, r, _RGBA32(255, 255 - r, 0, 10)
  22.             _DISPLAY
  23.             _LIMIT 400
  24.         NEXT
  25.         newTarget
  26.     END IF
  27.     IF INKEY$ = " " THEN newTarget
  28.     _DISPLAY
  29.     _LIMIT 60
  30.  
  31. SUB newTarget
  32.     IF RND < .5 THEN
  33.         IF RND < .5 THEN tx = RND * 200 + 50 ELSE tx = _WIDTH - 250 + RND * 200
  34.         ty = RND * (_HEIGHT - 100) + 50
  35.     ELSE
  36.         IF RND < .5 THEN ty = RND * 200 + 50 ELSE ty = _HEIGHT - 250 + RND * 100
  37.         tx = RND * (_WIDTH - 100) + 50
  38.     END IF
  39.     tr = RND * 50 + 20
  40.     tc = _RGB32(60 + RND * 195, RND * 255, RND * 255)
  41.  
  42. SUB PLC (baseX, baseY, targetX, targetY, targetR) ' PLC for PlasmaLaserCannon
  43.     r = RND ^ 2 * RND: g = RND ^ 2 * RND: b = RND ^ 2 * RND: hp = _PI(.5) ' red, green, blue, half pi
  44.     ta = _ATAN2(targetY - baseY, targetX - baseX) ' angle of target to cannon base
  45.     dist = _HYPOT(targetY - baseY, targetX - baseX) ' distance cannon to target
  46.     dr = targetR / dist
  47.     FOR r = 0 TO dist STEP .25
  48.         x = baseX + r * COS(ta)
  49.         y = baseY + r * SIN(ta)
  50.         c = c + .3
  51.         fcirc x, y, dr * r, _RGB32(128 + 127 * SIN(r * c), 128 + 127 * SIN(g * c), 128 + 127 * SIN(b * c))
  52.     NEXT
  53.     FOR rr = dr * r TO 0 STEP -.5
  54.         c = c + 1
  55.         fcirc x, y, rr, _RGB32(128 + 127 * SIN(r * c), 128 + 127 * SIN(g * c), 128 + 127 * SIN(b * c))
  56.     NEXT
  57.  
  58. SUB fcirc (x AS LONG, y AS LONG, R AS LONG, C AS _UNSIGNED LONG) 'vince version  fill circle x, y, radius, color
  59.     DIM x0 AS LONG, y0 AS LONG, e AS LONG
  60.     x0 = R: y0 = 0: e = 0
  61.     DO WHILE y0 < x0
  62.         IF e <= 0 THEN
  63.             y0 = y0 + 1
  64.             LINE (x - x0, y + y0)-(x + x0, y + y0), C, BF
  65.             LINE (x - x0, y - y0)-(x + x0, y - y0), C, BF
  66.             e = e + 2 * y0
  67.         ELSE
  68.             LINE (x - y0, y - x0)-(x + y0, y - x0), C, BF
  69.             LINE (x - y0, y + x0)-(x + y0, y + x0), C, BF
  70.             x0 = x0 - 1: e = e - 2 * x0
  71.         END IF
  72.     LOOP
  73.     LINE (x - R, y)-(x + R, y), C, BF
  74.  
  75. SUB drawBall (x, y, r, c AS _UNSIGNED LONG)
  76.     DIM rred AS LONG, grn AS LONG, blu AS LONG, rr AS LONG, f
  77.     rred = _RED32(c): grn = _GREEN32(c): blu = _BLUE32(c)
  78.     FOR rr = r TO 0 STEP -1
  79.         f = 1 - rr / r
  80.         fcirc x, y, rr, _RGB32(rred * f, grn * f, blu * f)
  81.     NEXT
  82.  
  83. SUB drawShip (x, y, colr AS _UNSIGNED LONG) 'shipType     collisions same as circle x, y radius = 30
  84.     STATIC ls
  85.     DIM light AS LONG, r AS LONG, g AS LONG, b AS LONG
  86.     r = _RED32(colr): g = _GREEN32(colr): b = _BLUE32(colr)
  87.     fellipse x, y, 6, 15, _RGB32(r, g - 120, b - 100)
  88.     fellipse x, y, 18, 11, _RGB32(r, g - 60, b - 50)
  89.     fellipse x, y, 30, 7, _RGB32(r, g, b)
  90.     FOR light = 0 TO 5
  91.         fcirc x - 30 + 11 * light + ls, y, 1, _RGB32(ls * 50, ls * 50, ls * 50)
  92.     NEXT
  93.     ls = ls + 1
  94.     IF ls > 5 THEN ls = 0
  95.  
  96. SUB fellipse (CX AS LONG, CY AS LONG, xr AS LONG, yr AS LONG, C AS _UNSIGNED LONG)
  97.     IF xr = 0 OR yr = 0 THEN EXIT SUB
  98.     DIM x AS LONG, y AS LONG
  99.     w2 = xr * xr: h2 = yr * yr: h2w2 = h2 * w2
  100.     LINE (CX - xr, CY)-(CX + xr, CY), C, BF
  101.     DO WHILE y < yr
  102.         y = y + 1
  103.         x = SQR((h2w2 - y * y * w2) \ h2)
  104.         LINE (CX - x, CY + y)-(CX + x, CY + y), C, BF
  105.         LINE (CX - x, CY - y)-(CX + x, CY - y), C, BF
  106.     LOOP
  107.  
  108.  

EDIT: forgot I switched to circles instead of lines so removed x1, y1, x2, y2 calcs for PLC
« Last Edit: November 11, 2020, 02:30:27 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #13 on: November 11, 2020, 02:02:56 pm »
Wow incredible... so Subs don't always run at the same time as the program, but they keep it in memory to be used as many times as needed for the main program. I finally get it! LOL Thanks B+.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cave Fighter - Side Scrolling Action Game
« Reply #14 on: November 11, 2020, 02:24:57 pm »
Wow incredible... so Subs don't always run at the same time as the program, but they keep it in memory to be used as many times as needed for the main program. I finally get it! LOL Thanks B+.

Yes they are just like PRINT, INPUT, CIRCLE, LINE only you put the code for them in your program.

Just imagine them as extensions to QB64 command keywords. They are meant to be used over and over just like command keywords of QB64.