Author Topic: Simplest snake  (Read 5724 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
Simplest snake
« on: March 08, 2020, 09:31:43 pm »
So there's this guy, Chris DeLeon, that makes these fast-coding videos to promote his online courses, and I recently watched the video in which he wrote a functional Snake game in 4 minutes and 30 seconds. Of course we're not counting the time he spent practicing it before hitting record on his camera, and I don't judge him if he has a cheat sheet right in front of him. The thing is, this is impressive because it's so simple and I wanted to port it over to QB64. Here's the result.

The video in which he writes this in 4m 30s:

Original code in JavaScript: https://pastebin.com/Z3zhb7cY

Code: QB64 $NOPREFIX: [Select]
  1.  
  2. TYPE position
  3.     x AS SINGLE
  4.     y AS SINGLE
  5.  
  6. DIM canvas AS LONG
  7. canvas = NEWIMAGE(400, 400, 32)
  8.  
  9. SCREEN canvas
  10. TITLE "Simplest snake"
  11.  
  12. DIM px, py, gs, tc, ax, ay, xv, yv, tail, i
  13. DIM trail(1000) AS position
  14. px = 10
  15. py = 10
  16. gs = 20
  17. tc = 20
  18. ax = 15
  19. ay = 15
  20. tail = 1
  21.  
  22. FOR i = 0 TO tail - 1
  23.     trail(i).x = px
  24.     trail(i).y = py
  25.  
  26.     DIM k AS LONG
  27.     k = KEYHIT
  28.     SELECT CASE k
  29.         CASE 18432 'up
  30.             xv = 0
  31.             yv = -1
  32.         CASE 20480 'down
  33.             xv = 0
  34.             yv = 1
  35.         CASE 19200 'left
  36.             xv = -1
  37.             yv = 0
  38.         CASE 19712 'right
  39.             xv = 1
  40.             yv = 0
  41.     END SELECT
  42.  
  43.     px = px + xv
  44.     py = py + yv
  45.  
  46.     IF px < 0 THEN px = tc - 1
  47.     IF px > tc - 1 THEN px = 0
  48.     IF py < 0 THEN py = tc - 1
  49.     IF py > tc - 1 THEN py = 0
  50.  
  51.     'bg
  52.     LINE (0, 0)-(WIDTH, HEIGHT), RGB32(0), BF
  53.  
  54.     'snake
  55.     FOR i = 0 TO tail - 1
  56.         LINE (trail(i).x * gs, trail(i).y * gs)-STEP(gs - 2, gs - 2), RGB32(55, 161, 61), BF
  57.         IF trail(i).x = px AND trail(i).y = py THEN tail = 5
  58.     NEXT
  59.  
  60.     trail(tail).x = px
  61.     trail(tail).y = py
  62.     FOR i = 0 TO tail - 1
  63.         trail(i) = trail(i + 1)
  64.     NEXT
  65.  
  66.     'apple
  67.     IF (ax = px AND ay = py) OR tail < 5 THEN
  68.         tail = tail + 1
  69.         ax = INT(RND * tc)
  70.         ay = INT(RND * tc)
  71.     END IF
  72.  
  73.     LINE (ax * gs, ay * gs)-STEP(gs - 2, gs - 2), RGB32(255, 0, 0), BF
  74.  
  75.     DISPLAY
  76.     LIMIT 10

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simplest snake
« Reply #1 on: March 08, 2020, 10:42:58 pm »
Not too far from where Ron77 started from here: https://www.qb64.org/forum/index.php?topic=1106.0

Marked as best answer by on Today at 05:20:52 pm

FellippeHeitor

  • Guest
Re: Simplest snake
« Reply #2 on: March 08, 2020, 10:44:22 pm »
  • Undo Best Answer
  • Can you spot the difference in approaches?

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: Simplest snake
    « Reply #3 on: March 08, 2020, 10:46:34 pm »
    Like using Type and real graphics?

    Inkey$ instead of keydown

    Neither one checks to see if the new food is in snake body, I think.
    « Last Edit: March 08, 2020, 10:51:46 pm by bplus »

    FellippeHeitor

    • Guest
    Re: Simplest snake
    « Reply #4 on: March 08, 2020, 10:47:45 pm »
    That too.

    Offline bplus

    • Global Moderator
    • Forum Resident
    • Posts: 8053
    • b = b + ...
      • View Profile
    Re: Simplest snake
    « Reply #5 on: March 08, 2020, 10:54:35 pm »
    lines 24-28 odd?
     

    Oh he is doing wrap arounds at boundries.

    I should play the thing instead of read code, also looks like body starts at 5.

    Doesn't look like snake dies if crashes into body, gets shorter back to 5

    Dim k inside a do loop is dumb.

    The snake is imortal it doesn't die going back on itself either.

    Did I get them all?

    Does gs stand for game scale?

    Something is wrong with key response a slight delay by 1 frame.
    « Last Edit: March 08, 2020, 11:31:18 pm by bplus »