Author Topic: Ken's Snake Game  (Read 3854 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
Ken's Snake Game
« on: May 08, 2020, 12:26:54 am »
This is a small game, but it can be fun. :) Move around the snake on the screen with your mouse and avoid the red bombs while eating the yellow food to gain points. Once your health goes away the game ends. After a short amount of time the bombs and the food re-locate.
This is my first game using a snake tail I invented tonight. I know I could have used that one fading tail effect, but I wanted to try something different just to see if it could be done. If you look at the code, you might notice that I use CLS after 2500 plots in order to reset the variables of the snake. I tried it other ways without having to use CLS but couldn't do it for some reason. But that's OK, the player doesn't notice it anyway. Anyways, please tell me what you think. I hope you enjoy it.

Code: QB64: [Select]
  1. 'Use your mouse to guide the snake around the screen to eat the yellow food.
  2. 'Watch out for the red bombs or your health will go down.
  3. 'Made by Sierraken (Ken G.) on May 7, 2020.
  4. 'Freeware - Feel free to use any code but do not sell this game as a whole.
  5. DIM x(3000), y(3000)
  6. DIM xx(2000), yy(2000)
  7. DIM xxx(2000), yyy(2000)
  8. _TITLE "Snake Game     by SierraKen"
  9. lives = 100
  10. 'Setup screen.
  11. SCREEN _NEWIMAGE(800, 600, 32)
  12. 'Bombs
  13. FOR r = 1 TO 30
  14.     xx(r) = INT(RND * 750) + 25
  15.     yy(r) = INT(RND * 550) + 25
  16.     FOR sz = .25 TO 15 STEP .25
  17.         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  18.     NEXT sz
  19. 'Food
  20. FOR rr = 1 TO 30
  21.     xxx(rr) = INT(RND * 750) + 25
  22.     yyy(rr) = INT(RND * 550) + 25
  23.     FOR sz = .25 TO 5 STEP .25
  24.         CIRCLE (xxx(rr), yyy(rr)), sz, _RGB32(255, 255, 128)
  25.     NEXT sz
  26. NEXT rr
  27. 'Main Game Loop
  28.     mouseWheel = 0
  29.         mouseX = _MOUSEX
  30.         mouseY = _MOUSEY
  31.         t = t + 1
  32.         x(t) = mouseX
  33.         y(t) = mouseY
  34.         IF t > 50 THEN
  35.             FOR sz = .25 TO 5 STEP .25
  36.                 CIRCLE (x(t - 50), y(t - 50)), sz, _RGB32(0, 0, 0)
  37.             NEXT sz
  38.             'At a certain time, replace bombs and food and reset variable T.
  39.             IF t > 2500 THEN
  40.                 t = 1
  41.                 CLS
  42.                 FOR r = 1 TO 30
  43.                     RANDOMIZE TIMER
  44.                     xx(r) = INT(RND * 750) + 25
  45.                     yy(r) = INT(RND * 550) + 25
  46.                     FOR sz = .25 TO 15 STEP .25
  47.                         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  48.                     NEXT sz
  49.                     RANDOMIZE TIMER
  50.                     xxx(r) = INT(RND * 750) + 25
  51.                     yyy(r) = INT(RND * 550) + 25
  52.                     FOR sz = .25 TO 5 STEP .25
  53.                         CIRCLE (xxx(r), yyy(r)), sz, _RGB32(255, 255, 128)
  54.                     NEXT sz
  55.                 NEXT r
  56.             END IF
  57.         END IF
  58.         'Draw the snake.
  59.         FOR sz = .25 TO 5 STEP .25
  60.             CIRCLE (mouseX, mouseY), sz, _RGB32(255, 255, 255)
  61.         NEXT sz
  62.         'Check to see if you hit a bomb,
  63.         FOR ch = -7 TO 7
  64.             IF POINT(x(t) + ch, y(t) + ch) = _RGB32(255, 0, 0) THEN
  65.                 SOUND 150, .25
  66.                 lives = lives - 1
  67.                 lives$ = STR$(lives)
  68.                 score$ = STR$(sc)
  69.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  70.                 IF lives < 1 THEN
  71.                     LOCATE 20, 40: PRINT "G A M E    O V E R"
  72.                     END
  73.                 END IF
  74.                 FOR ex = 1 TO 35
  75.                     CIRCLE (x(t) + ch, y(t) + ch), ex, _RGB32(0, 0, 0)
  76.                 NEXT ex
  77.             END IF
  78.         NEXT ch
  79.         'Check to see if you eat food.
  80.         FOR ch2 = -14 TO 14
  81.             IF POINT(x(t) + ch2, y(t) + ch2) = _RGB32(255, 255, 128) THEN
  82.                 sc = sc + 10
  83.                 score$ = STR$(sc)
  84.                 lives$ = STR$(lives)
  85.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  86.                 FOR snd = 300 TO 400 STEP 25
  87.                     SOUND snd, .25
  88.                 NEXT snd
  89.                 FOR ex = .25 TO 8 STEP .25
  90.                     CIRCLE (x(t) + ch2, y(t) + ch2), ex, _RGB32(0, 0, 0)
  91.                 NEXT ex
  92.             END IF
  93.         NEXT ch2
  94.     LOOP
  95.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Ken's Snake Game
« Reply #1 on: May 08, 2020, 07:19:20 am »
Hi Ken
fine to meet another your program

I find it fine, the gameplay is good... I have seen that it is not necessary to get all the foodpoints on the screen to pass the next level.
I have taken some red bombs....
fine sound.
here a screenshot of my playing

 
Sierra's Snake game.jpg


Good job! And Thanks to share.
Programming isn't difficult, only it's  consuming time and coffee

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ken's Snake Game
« Reply #2 on: May 08, 2020, 12:48:19 pm »
Thanks TempodiBasic! Yeah the levels just change when your snake goes a certain distance. This game so far is just an example game to make a snake pretty much. I might enhance it soon. :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ken's Snake Game
« Reply #3 on: May 08, 2020, 06:16:31 pm »
I added enemies that chase you. :) Any comments are welcome as usual.

Code: QB64: [Select]
  1. 'Use your mouse to guide the snake around the screen to eat the yellow food.
  2. 'Watch out for the red bombs or your health will go down.
  3. 'Also watch out for the enemies that move toward you. They will also make your health decrease.
  4. 'Made by Sierraken (Ken G.) on May 8, 2020.
  5. DIM x(3000), y(3000)
  6. DIM xx(2000), yy(2000)
  7. DIM xxx(2000), yyy(2000)
  8. DIM x2(2000), y2(2000)
  9. _TITLE "Snake Game     by SierraKen"
  10. lives = 100
  11. 'Setup screen.
  12. SCREEN _NEWIMAGE(800, 600, 32)
  13. 'Bombs
  14. FOR r = 1 TO 30
  15.     xx(r) = INT(RND * 750) + 25
  16.     yy(r) = INT(RND * 550) + 25
  17.     FOR sz = .25 TO 15 STEP .25
  18.         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  19.     NEXT sz
  20. 'Food
  21. FOR rr = 1 TO 30
  22.     xxx(rr) = INT(RND * 750) + 25
  23.     yyy(rr) = INT(RND * 550) + 25
  24.     FOR sz = .25 TO 5 STEP .25
  25.         CIRCLE (xxx(rr), yyy(rr)), sz, _RGB32(255, 255, 128)
  26.     NEXT sz
  27. NEXT rr
  28. 'Enemies
  29. FOR rrr = 1 TO 10
  30.     x2(rrr) = INT(RND * 750) + 25
  31.     y2(rrr) = INT(RND * 550) + 25
  32.     FOR sz = .25 TO 5 STEP .25
  33.         CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(255, 0, 128)
  34.     NEXT sz
  35. NEXT rrr
  36.  
  37. 'Main Game Loop
  38.     _LIMIT 3000
  39.     mouseWheel = 0
  40.         mouseX = _MOUSEX
  41.         mouseY = _MOUSEY
  42.         t = t + 1
  43.         x(t) = mouseX
  44.         y(t) = mouseY
  45.         IF t > 50 THEN
  46.             FOR sz = .25 TO 5 STEP .25
  47.                 CIRCLE (x(t - 50), y(t - 50)), sz, _RGB32(0, 0, 0)
  48.             NEXT sz
  49.             'At a certain time, replace bombs, food, enemies, and reset variable T.
  50.             IF t > 2500 THEN
  51.                 t = 1
  52.                 CLS
  53.                 FOR r = 1 TO 30
  54.                     RANDOMIZE TIMER
  55.                     xx(r) = INT(RND * 750) + 25
  56.                     yy(r) = INT(RND * 550) + 25
  57.                     FOR sz = .25 TO 15 STEP .25
  58.                         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  59.                     NEXT sz
  60.                     RANDOMIZE TIMER
  61.                     xxx(r) = INT(RND * 750) + 25
  62.                     yyy(r) = INT(RND * 550) + 25
  63.                     FOR sz = .25 TO 5 STEP .25
  64.                         CIRCLE (xxx(r), yyy(r)), sz, _RGB32(255, 255, 128)
  65.                     NEXT sz
  66.                 NEXT r
  67.                 FOR r2 = 1 TO 10
  68.                     RANDOMIZE TIMER
  69.                     x2(r2) = INT(RND * 750) + 25
  70.                     y2(r2) = INT(RND * 550) + 25
  71.                     FOR sz = .25 TO 5 STEP .25
  72.                         CIRCLE (x2(r2), y2(r2)), sz, _RGB32(255, 0, 128)
  73.                     NEXT sz
  74.                 NEXT r2
  75.             END IF
  76.         END IF
  77.         'Draw the snake.
  78.         FOR sz = .25 TO 5 STEP .25
  79.             CIRCLE (mouseX, mouseY), sz, _RGB32(255, 255, 255)
  80.         NEXT sz
  81.         'Check to see if you hit a bomb,
  82.         FOR ch = -7 TO 7
  83.             IF POINT(x(t) + ch, y(t) + ch) = _RGB32(255, 0, 0) THEN
  84.                 SOUND 150, .25
  85.                 lives = lives - 1
  86.                 lives$ = STR$(lives)
  87.                 score$ = STR$(sc)
  88.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  89.                 IF lives < 0 THEN
  90.                     LOCATE 20, 40: PRINT "G A M E    O V E R"
  91.                     END
  92.                 END IF
  93.                 FOR ex = 1 TO 35
  94.                     CIRCLE (x(t) + ch, y(t) + ch), ex, _RGB32(0, 0, 0)
  95.                 NEXT ex
  96.             END IF
  97.         NEXT ch
  98.         'Check to see if you eat food.
  99.         FOR ch2 = -14 TO 14
  100.             IF POINT(x(t) + ch2, y(t) + ch2) = _RGB32(255, 255, 128) THEN
  101.                 sc = sc + 10
  102.                 score$ = STR$(sc)
  103.                 lives$ = STR$(lives)
  104.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  105.                 FOR snd = 300 TO 400 STEP 25
  106.                     SOUND snd, .25
  107.                 NEXT snd
  108.                 FOR ex = .25 TO 8 STEP .25
  109.                     CIRCLE (x(t) + ch2, y(t) + ch2), ex, _RGB32(0, 0, 0)
  110.                 NEXT ex
  111.             END IF
  112.         NEXT ch2
  113.         'Enemies
  114.         FOR rrr = 1 TO 10
  115.             FOR sz = .25 TO 5 STEP .25
  116.                 CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(0, 0, 0)
  117.             NEXT sz
  118.             again:
  119.             IF x2(rrr) > x(t) THEN x2(rrr) = x2(rrr) - .1
  120.             IF x2(rrr) < x(t) THEN x2(rrr) = x2(rrr) + .1
  121.             IF y2(rrr) > y(t) THEN y2(rrr) = y2(rrr) - .1
  122.             IF y2(rrr) < y(t) THEN y2(rrr) = y2(rrr) + .1
  123.             FOR ch = -7 TO 7
  124.                 IF POINT(x2(rrr) + ch, y2(rrr) + ch) = _RGB32(255, 0, 0) THEN GOTO again:
  125.             NEXT ch
  126.             FOR sz = .25 TO 5 STEP .25
  127.                 CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(255, 0, 128)
  128.             NEXT sz
  129.             'Check to see if you hit an enemy,
  130.             FOR ch = -3 TO 3
  131.                 IF POINT(x(t) + ch, y(t) + ch) = _RGB32(255, 0, 128) THEN
  132.                     lives = lives - .01
  133.                     lives$ = STR$(lives)
  134.                     score$ = STR$(sc)
  135.                     _TITLE "Score: " + score$ + "     Health: " + lives$
  136.                     IF lives < 0 THEN
  137.                         LOCATE 20, 40: PRINT "G A M E    O V E R"
  138.                         END
  139.                     END IF
  140.                     FOR ex = 1 TO 35
  141.                         CIRCLE (x(t) + ch, y(t) + ch), ex, _RGB32(0, 0, 0)
  142.                     NEXT ex
  143.                 END IF
  144.             NEXT ch
  145.         NEXT rrr
  146.     LOOP
  147.  


Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Ken's Snake Game
« Reply #4 on: May 09, 2020, 11:06:48 am »
Fun game.
Why is there no possibility of regaining lives ?

Why not yes ?

Marked as best answer by SierraKen on May 09, 2020, 09:08:34 am

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Ken's Snake Game
« Reply #5 on: May 09, 2020, 01:07:50 pm »
Thanks for the suggestion Euklides! I added the ability to get more health every 100 food pellets you get. I also made the game harder by speeding up the enemies every time the screen changes. It gets pretty hair raising after awhile. :)
Here is the update:

Code: QB64: [Select]
  1. 'Use your mouse to guide the snake around the screen to eat the yellow food pellets.
  2. 'Watch out for the red bombs or your health will go down.
  3. 'Also watch out for the enemies that move toward you. They will also make your health decrease.
  4. 'The game speeds up a little more when the screen changes.
  5. 'Your health increases by 50 points every 100 food pellets.
  6. 'Made by Sierraken (Ken G.) on May 9, 2020.
  7. DIM x(3000), y(3000)
  8. DIM xx(2000), yy(2000)
  9. DIM xxx(2000), yyy(2000)
  10. DIM x2(2000), y2(2000)
  11. _TITLE "Snake Game      by SierraKen"
  12. lives = 100
  13. et = .1
  14. 'Setup screen.
  15. SCREEN _NEWIMAGE(800, 600, 32)
  16. 'Bombs
  17. FOR r = 1 TO 30
  18.     xx(r) = INT(RND * 750) + 25
  19.     yy(r) = INT(RND * 550) + 25
  20.     FOR sz = .25 TO 15 STEP .25
  21.         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  22.     NEXT sz
  23. 'Food
  24. FOR rr = 1 TO 30
  25.     xxx(rr) = INT(RND * 750) + 25
  26.     yyy(rr) = INT(RND * 550) + 25
  27.     FOR sz = .25 TO 5 STEP .25
  28.         CIRCLE (xxx(rr), yyy(rr)), sz, _RGB32(255, 255, 128)
  29.     NEXT sz
  30. NEXT rr
  31. 'Enemies
  32. FOR rrr = 1 TO 10
  33.     x2(rrr) = INT(RND * 750) + 25
  34.     y2(rrr) = INT(RND * 550) + 25
  35.     FOR sz = .25 TO 5 STEP .25
  36.         CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(255, 0, 128)
  37.     NEXT sz
  38. NEXT rrr
  39.  
  40. 'Main Game Loop
  41.     _LIMIT 3000
  42.     mouseWheel = 0
  43.         mouseX = _MOUSEX
  44.         mouseY = _MOUSEY
  45.         t = t + 1
  46.         x(t) = mouseX
  47.         y(t) = mouseY
  48.         IF t > 50 THEN
  49.             FOR sz = .25 TO 5 STEP .25
  50.                 CIRCLE (x(t - 50), y(t - 50)), sz, _RGB32(0, 0, 0)
  51.             NEXT sz
  52.             'At a certain time, replace bombs, food, enemies, reset variable T, and speed up enemies.
  53.             IF t > 2500 THEN
  54.                 t = 1
  55.                 et = et + .005
  56.                 CLS
  57.                 FOR r = 1 TO 30
  58.                     RANDOMIZE TIMER
  59.                     xx(r) = INT(RND * 750) + 25
  60.                     yy(r) = INT(RND * 550) + 25
  61.                     FOR sz = .25 TO 15 STEP .25
  62.                         CIRCLE (xx(r), yy(r)), sz, _RGB32(255, 0, 0)
  63.                     NEXT sz
  64.                     RANDOMIZE TIMER
  65.                     xxx(r) = INT(RND * 750) + 25
  66.                     yyy(r) = INT(RND * 550) + 25
  67.                     FOR sz = .25 TO 5 STEP .25
  68.                         CIRCLE (xxx(r), yyy(r)), sz, _RGB32(255, 255, 128)
  69.                     NEXT sz
  70.                 NEXT r
  71.                 FOR r2 = 1 TO 10
  72.                     RANDOMIZE TIMER
  73.                     x2(r2) = INT(RND * 750) + 25
  74.                     y2(r2) = INT(RND * 550) + 25
  75.                     FOR sz = .25 TO 5 STEP .25
  76.                         CIRCLE (x2(r2), y2(r2)), sz, _RGB32(255, 0, 128)
  77.                     NEXT sz
  78.                 NEXT r2
  79.             END IF
  80.         END IF
  81.         'Draw the snake.
  82.         FOR sz = .25 TO 5 STEP .25
  83.             CIRCLE (mouseX, mouseY), sz, _RGB32(255, 255, 255)
  84.         NEXT sz
  85.         'Check to see if you hit a bomb,
  86.         FOR ch = -7 TO 7
  87.             IF POINT(x(t) + ch, y(t) + ch) = _RGB32(255, 0, 0) THEN
  88.                 SOUND 150, .25
  89.                 lives = lives - 1
  90.                 lives$ = STR$(lives)
  91.                 score$ = STR$(sc)
  92.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  93.                 IF lives < 0 THEN
  94.                     LOCATE 20, 40: PRINT "G A M E    O V E R"
  95.                     END
  96.                 END IF
  97.                 FOR ex = 1 TO 35
  98.                     CIRCLE (x(t) + ch, y(t) + ch), ex, _RGB32(0, 0, 0)
  99.                 NEXT ex
  100.             END IF
  101.         NEXT ch
  102.         'Check to see if you eat food.
  103.         FOR ch2 = -14 TO 14
  104.             IF POINT(x(t) + ch2, y(t) + ch2) = _RGB32(255, 255, 128) THEN
  105.                 sc = sc + 10
  106.                 IF sc / 1000 = INT(sc / 1000) THEN lives = lives + 50
  107.                 score$ = STR$(sc)
  108.                 lives$ = STR$(lives)
  109.                 _TITLE "Score: " + score$ + "     Health: " + lives$
  110.                 FOR snd = 300 TO 400 STEP 25
  111.                     SOUND snd, .25
  112.                 NEXT snd
  113.                 FOR ex = .25 TO 8 STEP .25
  114.                     CIRCLE (x(t) + ch2, y(t) + ch2), ex, _RGB32(0, 0, 0)
  115.                 NEXT ex
  116.             END IF
  117.         NEXT ch2
  118.         'Enemies
  119.         FOR rrr = 1 TO 10
  120.             FOR sz = .25 TO 5 STEP .25
  121.                 CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(0, 0, 0)
  122.             NEXT sz
  123.             again:
  124.             IF x2(rrr) > x(t) THEN x2(rrr) = x2(rrr) - et
  125.             IF x2(rrr) < x(t) THEN x2(rrr) = x2(rrr) + et
  126.             IF y2(rrr) > y(t) THEN y2(rrr) = y2(rrr) - et
  127.             IF y2(rrr) < y(t) THEN y2(rrr) = y2(rrr) + et
  128.             FOR ch = -7 TO 7
  129.                 IF POINT(x2(rrr) + ch, y2(rrr) + ch) = _RGB32(255, 0, 0) THEN GOTO again:
  130.             NEXT ch
  131.             FOR sz = .25 TO 5 STEP .25
  132.                 CIRCLE (x2(rrr), y2(rrr)), sz, _RGB32(255, 0, 128)
  133.             NEXT sz
  134.             'Check to see if you hit an enemy,
  135.             FOR ch = -3 TO 3
  136.                 IF POINT(x(t) + ch, y(t) + ch) = _RGB32(255, 0, 128) THEN
  137.                     lives = lives - .01
  138.                     lives$ = STR$(lives)
  139.                     score$ = STR$(sc)
  140.                     _TITLE "Score: " + score$ + "     Health: " + lives$
  141.                     IF lives < 0 THEN
  142.                         LOCATE 20, 40: PRINT "G A M E    O V E R"
  143.                         END
  144.                     END IF
  145.                     FOR ex = 1 TO 35
  146.                         CIRCLE (x(t) + ch, y(t) + ch), ex, _RGB32(0, 0, 0)
  147.                     NEXT ex
  148.                 END IF
  149.             NEXT ch
  150.         NEXT rrr
  151.     LOOP
  152.