Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Discussion I wrote this snake game with my 10 year old today

Discussion in 'Scripting' started by Max-om, Oct 5, 2023 at 12:05 AM.

Thread Status:
Not open for further replies.
  1. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    And I think this is the level most gamedevs should start at :D

    Code (CSharp):
    1. using System.Diagnostics;
    2. using System.Numerics;
    3.  
    4. var stopwatch = new Stopwatch();
    5. stopwatch.Start();
    6. var time = stopwatch.Elapsed;
    7.  
    8. int width = 100;
    9. int heigh = 25;
    10. Console.SetWindowSize(width, heigh);
    11.  
    12. (int X, int Y) food = (0, 0);
    13.  
    14. var snake = new List<(int X, int Y)>{ (0, 0)  };
    15. PlaceFood();
    16. var highres = new Vector2(width / 2.0f, heigh / 2.0f);
    17. var direction = new Vector2(0, 0);
    18. var directionMap = new Dictionary<ConsoleKey, Vector2>
    19. {
    20.     { ConsoleKey.A, new Vector2(-1, 0) },
    21.     { ConsoleKey.D, new Vector2(1, 0) },
    22.     { ConsoleKey.W, new Vector2(0, -1) },
    23.     { ConsoleKey.S, new Vector2(0, 1) },
    24.     { ConsoleKey.LeftArrow, new Vector2(-1, 0) },
    25.     { ConsoleKey.RightArrow, new Vector2(1, 0) },
    26.     { ConsoleKey.UpArrow, new Vector2(0, -1) },
    27.     { ConsoleKey.DownArrow, new Vector2(0, 1) },
    28. };
    29.  
    30.  
    31. while (true)
    32. {
    33.  
    34.     if (Console.KeyAvailable)
    35.     {
    36.         var key = Console.ReadKey(true);
    37.         if (directionMap.ContainsKey(key.Key))
    38.         {
    39.             var newDirection = directionMap[key.Key];
    40.             var delta = direction + newDirection;
    41.             if (delta != Vector2.Zero) direction = newDirection;
    42.         }
    43.     }
    44.  
    45.     var newTime = stopwatch.Elapsed;
    46.     var deltaTime = newTime - time;
    47.  
    48.     time = newTime;
    49.  
    50.     highres += direction * (float)deltaTime.TotalSeconds * 10;
    51.  
    52.     if (snake[0].X == (int)highres.X && snake[0].Y == (int)highres.Y) continue;
    53.  
    54.     var newPos = ((int)highres.X, (int)highres.Y);
    55.  
    56.     var death = snake.Any(pos => newPos == pos);
    57.     if (death)
    58.     {
    59.         Console.Clear();
    60.         Console.SetCursorPosition(1, 1);
    61.         Console.WriteLine($"DEATH! Score: {snake.Count}");
    62.         Console.ReadLine();
    63.         break;
    64.     }
    65.    
    66.     WriteAt(' ', snake[^1]);
    67.     for (int i = snake.Count - 1; i > 0; i--)
    68.         snake[i] = snake[i - 1];
    69.  
    70.     snake[0] = newPos;
    71.     WriteAt('█', snake[0]);
    72.  
    73.     if (food.X == snake[0].X && food.Y == snake[0].Y)
    74.     {
    75.         PlaceFood();
    76.         snake.Add((0, 0));
    77.     }
    78. }
    79.  
    80. void WriteAt(char c, (int X, int Y) position)
    81. {
    82.     Console.SetCursorPosition(position.X, position.Y);
    83.     Console.Write(c);
    84. }
    85.  
    86. void PlaceFood()
    87. {
    88.     do
    89.     {
    90.         var padding = 3;
    91.         food = (Random.Shared.Next(padding, width - padding), Random.Shared.Next(padding, heigh - padding));
    92.     } while (snake.Any(pos => pos == food));
    93.  
    94.     WriteAt('F', food);
    95. }
     
    Lurking-Ninja and CodeRonnie like this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    This has what to do with Unity though?
     
    Bunny83 and Kurt-Dekker like this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    421
    I just wrote this with my cat today

    Code (CSharp):
    1. ; hello-DOS.asm - single-segment, 16-bit "hello world" program
    2. ;
    3. ; assemble with "nasm -f bin -o hi.com hello-DOS.asm"
    4.  
    5.     org  0x100        ; .com files always start 256 bytes into the segment
    6.  
    7.     ; int 21h is going to want...
    8.  
    9.     mov  dx, msg      ; the address of or message in dx
    10.     mov  ah, 9        ; ah=9 - "print string" sub-function
    11.     int  0x21         ; call dos services
    12.  
    13.     mov  ah, 0x4c     ; "terminate program" sub-function
    14.     int  0x21         ; call dos services
    15.  
    16.     msg  db 'Hello, World!', 0x0d, 0x0a, '$'   ; $-terminated message
     
    StarManta and Nad_B like this.
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,943
    Closed, off topic.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.