Author Topic: Tank Walls  (Read 8543 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Tank Walls
« on: June 30, 2019, 08:45:22 pm »
Hi all. You might have guessed it, but I decided to make a game out of my graphical plotting program and wall detection example. It took me a few days to make this. I got the idea mostly from the old Atari 2600 game COMBAT from the 1980's. But this time your tank is smaller, will lesser sounds. But the awesome thing is, you get to make your own map layout of walls before you start playing. You will need both programs to make your own and play the game, but I will try to include an example layout picture on this forum thread as well as the game. The object of the game is to shoot him with your blue tank. You use the arrow keys and press the space bar to shoot. I apologize if you run into problems, I've had to do quite a number of fixes on it the last few days but I hope to have them all fixed. It's not a perfect game in that you will see some extra graphics here and there that don't belong there anymore, such as your shot from your cannon and a leftover dot of the tank you shot. You also will see holes in the walls if you shoot the tank near a wall, but I left that there for the looks of destruction. lol Please tell me what you think. If you can make it better, then that is great! Do anything you wish to it for yourself. But I doubt I will do much more if anything, I rattled my brain for days over this, as you can see in all the code lol. But have fun! :) I'll add it to my website soon. *Note: You can also re-direct your tank shot, after you have shot, using your arrow keys, like a guided missile. :)

Here is the graphical plotting program first again, so you have that to make your own layouts. The game asks you for the name of your graphical creation when you start. I'll add the game itself on the 3rd post. The next post will have an example picture layout to use with it if you wish.

Code: QB64: [Select]
  1. 'This plotter was made in 2 days by Ken G. and finished on June 27, 2019 using QB64 language.
  2. 'Thank you for some ideas from some people at the QB64.org Forum!
  3. 'Code to use the pictures made with this can be found marked out toward the end of this program.
  4. 'This program is Freeware and will always be Freeware.
  5. 'Feel free to use any of this code in your own programs.
  6. '-----------------------------------------------------------------------------------------------
  7. _TITLE "Ken's Plotter"
  8. _LIMIT 400
  9. start:
  10. DIM x$(10000), y$(10000), x(10000), y(10000)
  11. p = 0
  12. t = 0
  13. pn = 0
  14. e = 0
  15. SCREEN _NEWIMAGE(640, 480, 32)
  16. PRINT "                     Ken's Plotter"
  17. PRINT "                       By Ken G."
  18. PRINT "Use the arrow keys to move the red box."
  19. PRINT "Use Space Bar to move Pen Down to draw and Pen Up to just move."
  20. PRINT "Use E to turn the Eraser off and on."
  21. PRINT "Use Esc to start over."
  22. PRINT "**** You will need to have Pen Down to use Eraser."
  23. PRINT "There is a 10,000 plot limit. Don't worry about this limit"
  24. PRINT "because you can fill the entire screen and more and still be"
  25. PRINT "less than 10,000 plots."
  26. PRINT "Press S to save and L to load an already saved picture."
  27. PRINT "The saved .txt file will have X,Y plots on each line that"
  28. PRINT "can be used in your own programs."
  29. PRINT "This is a simple plotting/drawing program that doesn't change colors."
  30. PRINT "The saved files don't have any colors on them, so when"
  31. PRINT "you use them for your own programs, you will need to add a color."
  32. PRINT "The window scale is 640 x 480 pixels with each plot every 10 pixels."
  33. PRINT "So when using in your own programs, make sure and make X = X + 10 and"
  34. PRINT "Y = Y + 10 to display with LINE command."
  35. PRINT "Code to load your picture is marked out in the code of this program."
  36. PRINT "The coordinates and Pen Up, Pen Down, Eraser On, Eraser Off will be"
  37. PRINT "in the top Window title bar."
  38. INPUT "Press enter to start.", st$
  39. x = 320: y = 240
  40. FOR gridx = 0 TO 640 STEP 10
  41.     FOR gridy = 0 TO 480 STEP 10
  42.         LINE (gridx, gridy)-(gridx + 10, gridy + 10), _RGB32(255, 255, 255), B
  43.     NEXT gridy
  44. NEXT gridx
  45. LINE (x, y)-(x + 10, y + 10), _RGB(255, 0, 0), B
  46. pen$ = "Up": eraser$ = "Off"
  47. xx$ = STR$(x): yy$ = STR$(y)
  48. titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  49. _TITLE titl$
  50. go:
  51. _LIMIT 400
  52. a$ = INKEY$
  53. IF a$ = CHR$(27) THEN GOTO start:
  54. IF a$ = "s" OR a$ = "S" THEN GOTO save:
  55. IF a$ = "l" OR a$ = "L" OR a$ = "m" OR a$ = "M" THEN GOTO load:
  56. IF a$ = CHR$(0) + CHR$(72) THEN
  57.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 255, 255), B
  58.     y = y - 10
  59.     IF y < 0 THEN y = 0
  60.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 0, 0), B
  61.     IF pn = 1 THEN
  62.         x2 = x + 10
  63.         y2 = y + 10
  64.         IF e = 0 THEN LINE (x, y)-(x2, y2), _RGB32(255, 255, 255), BF
  65.         t = t + 1
  66.         x$(t) = STR$(x): y$(t) = STR$(y)
  67.         IF e = 1 THEN
  68.             LINE (x, y)-(x2, y2), _RGB32(0, 0, 0), BF
  69.             GOSUB eraser:
  70.         END IF
  71.         GOSUB check:
  72.     END IF
  73.     xx$ = STR$(x): yy$ = STR$(y)
  74.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  75.     _TITLE titl$
  76. IF a$ = CHR$(0) + CHR$(80) THEN
  77.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 255, 255), B
  78.     y = y + 10
  79.     IF y > 470 THEN y = 470
  80.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 0, 0), B
  81.     IF pn = 1 THEN
  82.         x2 = x + 10
  83.         y2 = y + 10
  84.         LINE (x, y)-(x2, y2), _RGB32(255, 255, 255), BF
  85.         t = t + 1
  86.         x$(t) = STR$(x): y$(t) = STR$(y)
  87.         IF e = 1 THEN
  88.             LINE (x, y)-(x2, y2), _RGB32(0, 0, 0), BF
  89.             GOSUB eraser:
  90.         END IF
  91.         GOSUB check:
  92.     END IF
  93.     xx$ = STR$(x): yy$ = STR$(y)
  94.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  95.     _TITLE titl$
  96. IF a$ = CHR$(0) + CHR$(77) THEN
  97.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 255, 255), B
  98.     x = x + 10
  99.     IF x > 630 THEN x = 630
  100.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 0, 0), B
  101.     IF pn = 1 THEN
  102.         x2 = x + 10
  103.         y2 = y + 10
  104.         LINE (x, y)-(x2, y2), _RGB32(255, 255, 255), BF
  105.         t = t + 1
  106.         x$(t) = STR$(x): y$(t) = STR$(y)
  107.         IF e = 1 THEN
  108.             LINE (x, y)-(x2, y2), _RGB32(0, 0, 0), BF
  109.             GOSUB eraser:
  110.         END IF
  111.         GOSUB check:
  112.     END IF
  113.     xx$ = STR$(x): yy$ = STR$(y)
  114.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  115.     _TITLE titl$
  116. IF a$ = CHR$(0) + CHR$(75) THEN
  117.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 255, 255), B
  118.     x = x - 10
  119.     IF x < 0 THEN x = 0
  120.     LINE (x, y)-(x + 10, y + 10), _RGB(255, 0, 0), B
  121.     IF pn = 1 THEN
  122.         x2 = x + 10
  123.         y2 = y + 10
  124.         LINE (x, y)-(x2, y2), _RGB32(255, 255, 255), BF
  125.         t = t + 1
  126.         x$(t) = STR$(x): y$(t) = STR$(y)
  127.         IF e = 1 THEN
  128.             LINE (x, y)-(x2, y2), _RGB32(0, 0, 0), BF
  129.             GOSUB eraser:
  130.         END IF
  131.         GOSUB check:
  132.     END IF
  133.     xx$ = STR$(x): yy$ = STR$(y)
  134.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  135.     _TITLE titl$
  136. IF a$ = "e" OR a$ = "E" THEN
  137.     e = e + 1
  138.     IF e = 2 THEN e = 0
  139.     IF e = 1 THEN
  140.         IF pen$ = "Down" THEN eraser$ = "On"
  141.         titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  142.         _TITLE titl$
  143.     END IF
  144.     IF e = 0 THEN
  145.         eraser$ = "Off"
  146.         titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  147.         _TITLE titl$
  148.     END IF
  149. IF a$ = " " THEN
  150.     pn = pn + 1
  151.     IF pn = 2 THEN pn = 0
  152. IF pn = 1 THEN
  153.     pen$ = "Down"
  154.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  155.     _TITLE titl$
  156.  
  157. IF pn = 0 THEN
  158.     pen$ = "Up"
  159.     titl$ = "Ken's Plotter X=" + xx$ + " Y=" + yy$ + "     Pen: " + pen$ + "     Eraser: " + eraser$
  160.     _TITLE titl$
  161. GOTO go:
  162.  
  163. save:
  164. save2:
  165. INPUT "Type file name of maze to create (without the .txt ending):", nm$
  166. nm$ = nm$ + ".txt"
  167. 'Checking to see if the file already exists on your computer.
  168. theFileExists = _FILEEXISTS(nm$)
  169. IF theFileExists = -1 THEN
  170.     PRINT "File Already Exists"
  171.     PRINT "Saving will delete your old maze."
  172.     PRINT
  173.     PRINT "Would you like to still do it?"
  174.     PRINT "(Y/N). Esc ends program."
  175.     llloop:
  176.     _LIMIT 10
  177.     ag2$ = INKEY$
  178.     IF ag2$ = "" THEN GOTO llloop:
  179.     IF ag2$ = "y" OR ag$ = "Y" THEN GOTO save3:
  180.     IF ag2$ = CHR$(27) THEN END
  181.     GOTO save2:
  182. save3:
  183. OPEN nm$ FOR OUTPUT AS #1
  184. FOR plots = 1 TO t
  185.     _LIMIT 400
  186.     IF x$(plots) = "" OR y$(plots) = "" THEN GOTO skip2:
  187.     PRINT #1, x$(plots); ","; y$(plots)
  188.     skip2:
  189. NEXT plots
  190. PRINT nm$; " has been saved to your computer."
  191. INPUT "Press enter to start again."; a$
  192. GOTO start:
  193.  
  194. '------------------------------------------------------------------------------
  195. 'The following is the code you can use to load the graphic to your own program.
  196. '------------------------------------------------------------------------------
  197.  
  198. load:
  199. INPUT "Type file name to load (without the .txt ending):", nm$
  200. nm$ = nm$ + ".txt"
  201. OPEN nm$ FOR INPUT AS #1
  202. ggo:
  203. _LIMIT 400
  204. p = p + 1
  205. INPUT #1, x$(p), y$(p)
  206. x(p) = VAL(x$(p)): y(p) = VAL(y$(p))
  207. LINE (x(p), y(p))-(x(p) + 10, y(p) + 10), _RGB(255, 255, 255), BF
  208. IF EOF(1) THEN CLOSE #1: GOTO done:
  209. GOTO ggo:
  210. done:
  211. a$ = INKEY$
  212. IF a$ = "" THEN GOTO done:
  213. IF a$ = CHR$(27) THEN END
  214.  
  215. '------------------------------------------------------------------------------
  216.  
  217. GOTO start:
  218.  
  219. check:
  220. IF t = 10000 THEN
  221.     INPUT "Limit has been reached, do you want to save (Yes/No):"; yn$
  222.     IF yn$ = "YES" OR yn$ = "yes" OR yn$ = "Yes" OR yn$ = "YEs" OR yn$ = "yES" OR yn$ = "Y" OR yn$ = "y" THEN GOTO save:
  223.     GOTO start:
  224.  
  225. eraser:
  226. FOR ee = 1 TO t
  227.     IF x$(ee) = x$(t) AND y$(ee) = y$(t) THEN
  228.         x$(ee) = "": y$(ee) = ""
  229.     END IF
  230. NEXT ee
  231.  

 



« Last Edit: June 30, 2019, 08:56:42 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #1 on: June 30, 2019, 08:49:02 pm »
Here is an attachment file of a pretty simple but neat graphical layout for Tank Walls. You can try this before making your own if you wish. Put this "halls.txt" file in the same directory as the game. I'll put the game on the next post. Just remember, you need a graphic, like this one, to be used with the Tank Walls game.

* halls.txt (Filesize: 5.63 KB, Downloads: 184)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #2 on: June 30, 2019, 08:51:49 pm »
And here is the Tank Walls game itself :). Please don't be picky on it, I just started on QB64 and still not as good as you guys yet I bet, although I used to program a lot decades ago. But I've wanted to make a game like this ever since the 80's. :)

(Code deleted from this post, a better version is below on this same thread.)
« Last Edit: July 01, 2019, 12:47:29 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Tank Walls
« Reply #3 on: June 30, 2019, 11:26:38 pm »
Cool game... Sneaky little mongrel that enemy tank... I managed to blow him away more time than he did me.... Nicely done!
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #4 on: June 30, 2019, 11:40:13 pm »
Thanks Johno! Am glad you like it. Will be interesting to see how it works with different maps. And of course anyone can change the input command asking people for a map, to just plugging a map or more directly to the code.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tank Walls
« Reply #5 on: June 30, 2019, 11:42:44 pm »
Dang Ken you whipped up 500+ lines of code into a working game in a day or two?!?!

When the little red monster learns to quit banging it's turret against the first wall it runs into, I am going to be in deep doo-doo! ;-))

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #6 on: July 01, 2019, 12:12:03 am »
LOL B+, yeah I can't figure out how to make much more intelligence than what he has. He will catch you if you are in the same hallway or room though, if you don't shoot him first. :) Glad you like it. Oh, it took almost a week. :) I'll put it on my website now.  http://ken.x10host.com/qb64/
« Last Edit: July 01, 2019, 12:33:51 am by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tank Walls
« Reply #7 on: July 01, 2019, 11:03:12 am »
Hi Ken,

I am feeling ambiguous about making the enemy smarter, but if the little red devil can remember it has been in same place 2 steps ago, it might turn 90 degrees and try another banger session of only once, turn 90 again... it must then find it's way out of 3 sided trap without need of detecting it's prey in the vicinity to give chase. Keep a memory of last 3 steps, if 1st = 3rd then turn.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #8 on: July 01, 2019, 12:18:56 pm »
B+ yes I've thought of that kind of stuff, but the problem is, it can change direction, but it will automatically fall back to the same thing just about, over and over. Because it is always looking for where you are too. I also thought about making it only half the time looking for where you are, but then it would be too easy when you are in the same room or hallway. Maybe I can make loop variables that expand out until they hit the next wall in 4 directions from the enemy and if it doesn't see you, then it won't look for you. Hmm I'll give it a try I think. Not sure yet how I can do this but I'll look.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tank Walls
« Reply #9 on: July 01, 2019, 12:26:26 pm »
Yeah, you'll get it soon enough :D

and then I will have to find another game easy to win ;-))

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #10 on: July 01, 2019, 12:46:19 pm »
LOL it was easier than I thought! I just made it so when the enemy tank hits a wall, it picks a random direction and goes another way. It wasn't like I thought, he doesn't keep falling back to the wall all the time. But when he does, he keeps randomly picking a different direction until he doesn't hit the wall and then goes and goes. :))) This is SO AWESOME! Thanks for the suggestion.
Here is the updated version:

(Code deleted again, even better version below.)
« Last Edit: July 01, 2019, 02:36:08 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tank Walls
« Reply #11 on: July 01, 2019, 01:30:08 pm »
I was think'in it could just check if a wall was where it was intending to step... easy too.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #12 on: July 01, 2019, 02:35:37 pm »
Well, I found ways to get rid of almost all the leftover graphics clutter. Except for a tiny bit which I'm just going to leave. Here is the better version:

(Code deleted again, I apologize. The updated code, with a limit on the shots, is on page 2 of this forum thread.)
« Last Edit: July 02, 2019, 03:47:51 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tank Walls
« Reply #13 on: July 01, 2019, 04:36:20 pm »
Here is a second map I've made if anyone is interested. I have both now on my website as well.
* halls2.txt (Filesize: 6.27 KB, Downloads: 195)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Tank Walls
« Reply #14 on: July 01, 2019, 05:52:00 pm »
In reference to wall collision. Bplus has a good idea to check to see if a wall exists in the direction of movement. I used to get "stuck" in walls as well. Using bplus's idea of detection, when you hit the wall, simply 'bounce' back at the same velocity as the collision. Voila! No more partial 'Caspers'. Just a thought...
Logic is the beginning of wisdom.