Programming a Snake Game in C# - Full Guide

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 365

  • @OttoBotCode
    @OttoBotCode  Год назад +12

    *Source code*
    If you don't want to write the code yourself, it's available here for 3$ (and free for Patreon supporters):
    www.patreon.com/OttoBotCode/shop/78661
    Your support would be a massive help for me and my channel! 😊

    • @andjelaa23
      @andjelaa23 Год назад

      private readonly Image[,] in mainwindow.xaml is not working on my project. It says that the type or namespace Image could not be found

    • @hamnaic
      @hamnaic Год назад

      I have a snake game project, but my language is C++. I am just a first-semester student, and I don't know much about C++. 😢 Can you create a video, similar to how you do in C#, or guide me on how to convert this code into C++? I have only 10 days left for project submission, and my project is still pending.

    • @jamesross3939
      @jamesross3939 Год назад +2

      @@hamnaic good luck ... people are not here to do your homework.

    • @josephedison6366
      @josephedison6366 5 месяцев назад

      ​@@jamesross3939Real

  • @BigWill076
    @BigWill076 10 месяцев назад +6

    I've been staring at this without doing it for so long, but today's the day! Thanks for making it - teachers like you make starting the journey for people who want to do this sort of thing so much better.

  • @xucxichuc325
    @xucxichuc325 2 года назад +42

    I'm not done watching the tutorial yet! But I'm really appreciated it, that you've invested so much time and effort into making these Tutorial. Thank you so much for that! You're a real hero

    • @OttoBotCode
      @OttoBotCode  2 года назад +5

      Thank you for the kind comment! ☺

  • @robertlysebo
    @robertlysebo Год назад +18

    I followed this guide as an intro to WPF during my studies.
    Tempo was great, explainations were great. Overall a pretty fun video!

  • @vladoss4643
    @vladoss4643 Год назад +4

    Man!! Thank you a lot!
    My first fream was write a game using C# and today I made it!!)
    I spend 4,5 hours watching you, but I did it!!
    Thanks a lot.
    I have then looked your videolist. And have seen, that you made my second dream. Write the chess!!!
    AAAAAA!! I am so happy right now! I will thank a world for your existance!)
    Good luck in your life and etc.))
    Thanks a lot again

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      Thank you so much! Glad you enjoy the videos 😊

  • @RonRoostan
    @RonRoostan Год назад +11

    Hi Otto, I would like to say this tutorial is absolutely the best ever. Thanks and well done!
    I have modified my version to include a pause/resume feature and currently playing on a 32 x 32 grid. The longest snake I have to date is 176 cells. This took several minutes to construct but with the pause feature I was able to stop for a tea break halfway through. :)

    • @OttoBotCode
      @OttoBotCode  Год назад +3

      You've made my day 🤩 Thanks for your comment and well done on the pause/resume feature!

    • @M-G112
      @M-G112 Год назад +1

      Would you be willing to share pause implementation?

    • @RonRoostan
      @RonRoostan Год назад +1

      Hi @@M-G112
      Sorry I couldn't respond sooner. Maybe you have found a way to implement pause/resume in the meantime.
      But my approach was to introduce a new enum to track game mode (distinct from GameState):
      public enum GameMode
      {
      NotStarted,
      Started,
      Paused,
      Resuming,
      Over
      }
      The code looks for the space bar key press to enter 'Paused' mode
      if (gameState.Mode == GameMode.Started && e.Key == Key.Space)
      {
      mediaPlayer.Open(new Uri("Assets/intermission.wav", UriKind.Relative));
      mediaPlayer.Play();
      gameState.Mode = GameMode.Paused;
      return;
      }
      And another press of the space bar will enter 'Resuming' mode briefly before returning to 'Started' where the game play will continue from where it was interrupted.
      if (gameState.Mode == GameMode.Paused && e.Key == Key.Space)
      {
      gameState.Mode = GameMode.Resuming;
      return;
      }
      There are other code changes too but hopefully you get the idea. You can see them all in this github repo:
      github.com/rroostan/Snake/

    • @M-G112
      @M-G112 Год назад

      @@RonRoostan thank you for the reply, I allready implemented the pause but it is nice to see how you went about it.

  • @darefishman
    @darefishman Год назад +6

    Thats a nice Tutorial! One Tip: you can double a row of text by using ctrl+D. This speeds up code like:
    public readonly static Direction Left = new Direction(0, -1);
    public readonly static Direction Right = new Direction(0, 1);
    public readonly static Direction Up = new Direction(1, 0);
    public readonly static Direction Down = new Direction(-1, 0);

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      Thanks for the comment and the tip! 😊

  • @jellycoding
    @jellycoding 2 года назад +10

    Now you code in a much better speed. I only had to pause a few times. The flow is much better when I code along. Great video...thanks.

    • @OttoBotCode
      @OttoBotCode  2 года назад +1

      Super! Thank you for that comment 🤩

  • @lisalobo6389
    @lisalobo6389 Год назад +4

    Just finished the tutorial today. Thank you so much for explaining each step and for making this video. It helps immensely in seeing what I still need to revise in my own understanding of the language and building confidence in seeing the bits and pieces coming together. I am a huge fan!! Thanks a lot and I will be going through the other tuts on your channel as well.

    • @OttoBotCode
      @OttoBotCode  Год назад

      I'm so glad to hear that! Thanks for your comment 😊

  • @VitorBRYT
    @VitorBRYT 9 месяцев назад +5

    My first C# project!
    Thanks @OttoBotCode!

  • @ifif6158
    @ifif6158 Год назад +2

    Dear OttoBotCode,
    Thank you so much for the video. You are explaining very well and the temp is good. I am still newbie in programming, but you are explaining in very understandable and helpful way.
    Before I started with the learning to code in c#, friend of mind recommended me to start to work under some projects following a step by step the professionalist. I am very glad that I found that your video. Thank you again for doing all this hard work.
    I followed all of your steps and I (thought I did the codes the same like you). Of course it is not the same because I have an issue for solving.
    Everything at the game goes at the way you did ( the colors, the snake, the food, the dead body snake, the counter from 3 to 1, the welcome text) except the important thing - the game it is not working prorerly(after the back counting the snake just dying and the game is restarting). It is not giving me any opportunity to move it.
    That was even when I wrote the codes with you. You showed that your game is working (I think may be from the 20min in the video) but mine did not moved at all. I reviewed and compared mine and your code tripple and everything seems the same as your. Of course I am sure I have missed someting😢.
    (By the way the 3 watching of the full video helped me to understand a lot of things).
    I decided to write you an comment, because I didn't find similar issue at the comments I read.
    The other think that I wanted to ask is - what I have to do to send the game to my friend to try it out?
    I tried, but on my computer is opening just on visual studio.
    Is it possible to set a instalation file, because I did not found it at the saved folder?
    I will be very glad if I receive your back comment.
    Thank you again for all your hard work under this project and thank you for sharing and helping people with it. Wish you many luck.

    • @OttoBotCode
      @OttoBotCode  Год назад +2

      Thanks for the kind words! I cannot figure out what the problem is without seeing your code. If you are interested, you can send me your entire project to ottobotcodehelp@gmail.com (either as a github link or a zip). Then I'll take a look and see what I can find. When your game works, I can show you how to send it to a friend 😉

    • @TeamTornado168
      @TeamTornado168 9 месяцев назад

      لدي نفس المشكلة هل توصلتي الي حل؟

  • @justtohide
    @justtohide Год назад +2

    thank youu for this a great application. i made a few changes like, different types of obstacles, foods, health bar, game duration, pause/stop menu and notification bar. + snake can move from outside
    Movement:
    Use keys A, W, S, D for movement.
    Also, consider arrow keys (arrays) and the number pad if available.
    Special Movements:
    X Movement:
    Q: Move above-left.
    E: Move above-right.
    Z: Move below-left.
    C: Move below-right.
    Scoring:
    Animals:
    Bird: 1 point per second.
    Rabbit: 3 points per 3 seconds.
    Frog: 5 points per 5 seconds.
    Total hearts: 3.
    Wood obstacles deduct 1 heart, metal obstacles deduct 2 hearts.
    Game duration: 60 seconds.
    Mystery Box and Chances:
    If total second is less than 30 seconds:
    Either +10 seconds or -5 seconds.
    Else total second is greater than 30:
    1. Increase total obstacles.
    2. Decrease total obstacles.
    3. Additional Hearth
    Notification Bar:
    Located above the game arena.
    Displays important information.
    Pause/Resume:
    Open/close the resume menu by clicking the Space key.

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      I'm glad you enjoyed the tutorial. Well done on all the extra features, it's sounds like a really fun Snake game! 😁

    • @justtohide
      @justtohide Год назад

      @@OttoBotCode yeah it's. your role is undeniable

  • @GandohLakika-c7u
    @GandohLakika-c7u 4 месяца назад

    great video otto!! i havent watched more than 2 min but i can tell you but alot of hard work into this, great work luv from india!!!

  • @deanRSA1
    @deanRSA1 Год назад +1

    Just finished the project. Thanks for the tutorial, this was also a cool introduction to WPF.

    • @OttoBotCode
      @OttoBotCode  Год назад

      Awesome! Thanks for your comment 😊

  • @deniscooper09
    @deniscooper09 2 года назад +13

    Thank you for listening to me!

    • @OttoBotCode
      @OttoBotCode  2 года назад +5

      And thank you for the suggestion 😀

  • @stealthragnarr2644
    @stealthragnarr2644 Год назад

    Thanks a Lot for this, I needed one project to complete personally in C# and that was my first one, thank you.

    • @OttoBotCode
      @OttoBotCode  Год назад

      I'm glad you like it. Thanks for your comment!

  • @katharxia7118
    @katharxia7118 Год назад +3

    really good tutorial! helped me with my college project!!

  • @anthonysteinerv
    @anthonysteinerv 2 года назад +1

    Finally this type of videos return

  • @gameaudioshaman
    @gameaudioshaman 10 месяцев назад

    appreciate the work you put into this

  • @mocanuemanuel7553
    @mocanuemanuel7553 2 года назад

    You re the best bro , don t stop here. Tell us all you know, GOAT!

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Thank you so much 🤩 I know it has been a little slow (lot's of things going on these past few months) BUT I am not stopping don't worry. I am working on the next video 😉

  • @simplonetudiant
    @simplonetudiant Год назад

    This video is really great
    Hope you will continue making games with c#

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      Thanks! There's a big project on the way 😉

  • @rzvn111
    @rzvn111 3 месяца назад

    Thx, i learned the logic of how to make a game.

  • @cedricfr7047
    @cedricfr7047 9 месяцев назад

    Awesome, greetings from France ! Thank you !

  • @cariyaputta
    @cariyaputta 6 месяцев назад

    Thanks for this and the chess game tutorial. Added to my library.

  • @georgeseese
    @georgeseese Месяц назад

    Thanks; accurate code works.

  • @SahanRajapaksha-pr2ms
    @SahanRajapaksha-pr2ms Год назад

    awesome stuff bro !!

  • @andrus1002
    @andrus1002 5 месяцев назад

    Great tutorial, thank you so much!

  • @Suzy01973
    @Suzy01973 Месяц назад

    Thank you very much for tutorial Sir !

  • @cuke59
    @cuke59 Год назад

    Best video of making snake games

  • @Emcprogamer
    @Emcprogamer 2 года назад +3

    HELP!
    At 19:48 it when you did the HeadPosition, TailPosition and also the SnakePositions I get the ERROR Code CS0050 (Inconsistent accessibility: return type 'type' is less accessible than method 'method'). And I did not know what do do so I put it to private for then but at 1:14:40 when you used the HeadPosition again I got the ERROR Code CS0122 ('member' is inaccessible due to its protection level) because it was private. I really need help because I am completely stuck.
    Thank you!

    • @OttoBotCode
      @OttoBotCode  2 года назад +2

      The properties have to public like you said. You are probably getting errors because your Position class is "internal" instead of "public" 😉

    • @Emcprogamer
      @Emcprogamer 2 года назад

      @@OttoBotCode Yep, That was the solution! Thank you very much, This deserves to subscribe

  • @jpdo4067
    @jpdo4067 5 месяцев назад

    Bedankt

  • @setayeshmaroof7815
    @setayeshmaroof7815 25 дней назад

    Thank you veryyyy much
    It wad amazing💚💚💚

  • @Ayymoss
    @Ayymoss Год назад +1

    I think the glossing over nullables is probably a little bad for newer developers. Why did you disable it? Nullables are super useful, those warnings should be handled correctly - even if you don't anticipate nullref.

  • @user-gc8if5ts3l
    @user-gc8if5ts3l 2 года назад +1

    Hi OttoBotCode, thanks a lot. it is very good tutorial. i followed through with good understanding. 2 suggestions, 1) can you see some sound to it, when eat food, turn and die? as a next level tutorial. 2) consider teaching use how to make it an web app to put in online thanks anyway

    • @OttoBotCode
      @OttoBotCode  2 года назад +1

      Thanks for the suggestions 😊
      Playing sounds is very easy.
      Just add an mp3 file to the assets folder.
      You have to set "Build Action" to "Content" and "Copy to Output Directory" to "Copy always".
      You can play the sound with the MediaPlayer class.
      MediaPlayer player = new MediaPlayer();
      player.Open(new Uri("Assets/your_sound.mp3", UriKind.Relative));
      player.Play();

    • @Izzy-fr1zu
      @Izzy-fr1zu Год назад

      @@OttoBotCode awesome thanks! I was looking for that! Can you please explain why we use "content" in that case and not "Resource"?

  • @Nate-nc4he
    @Nate-nc4he Год назад +2

    Hey, I really love your content so far very high quality and informative. I unfortunately have been running into a problem for quite sometime now and can't seem to figure it out. At 44:34 is where I first start to have problems. Everything works up until then but, unlike you, when I run the program the empty grid image that is in the assets folder does not show up. I decided to continue till about 53:00 and the same issue still persits, none of the ImageSource images that are shown when you run it, show up for me. The images are indeed marked as "resource" and i have checked over most of the code. Any Ideas? Thanks

    • @OttoBotCode
      @OttoBotCode  Год назад

      Thanks for the kind words! Perhaps you are loading your assets from the wrong path (in Images.cs). I am of course just guessing here. If you want me to take a look, then send your project to me at ottobotcodehelp@gmail.com. I'll see if I can find the issue! ☺

    • @Nate-nc4he
      @Nate-nc4he Год назад

      @@OttoBotCode Nvm fixed the issue, turns out i spelt "assets" wrong lmao

  • @Vunk
    @Vunk 4 месяца назад

    Everything was good until 44:00 minutes, the grids won't show. I still followed thru until the snake and food being draw, and nothing was there.
    I did it exactly like you, maybe something broke in these 2 years.

    • @OttoBotCode
      @OttoBotCode  4 месяца назад

      No it's working just fine for other people. The most common mistake is not marking your assets as resources - then they won't show up. In the assets folder, select all the images, right click and choose properties, then set "Build Action" to "Resource" (I show this around 2:40 in the video). Let me know if that solves the issue! 😉

    • @alessandroesofago1837
      @alessandroesofago1837 4 месяца назад

      ​​@@OttoBotCodei'm having the same problem, all the assets are marked as resources.

    • @OttoBotCode
      @OttoBotCode  4 месяца назад

      @@alessandroesofago1837 The other problem is see quite often is people spell "Assets" wrong. Make sure that the folder is indeed called "Assets" and that the path in Images.cs is also correct. If that doesn't work, you can send your project to ottobotcodehelp@gmail.com and I'll find the problem for you 😊

  • @omegahaifoleet
    @omegahaifoleet Год назад

    Amazing tutorial!

  • @solvedplus858
    @solvedplus858 Год назад

    this is very good
    many thanks for your great effort

  • @stephenkeesling4665
    @stephenkeesling4665 9 месяцев назад +3

    Idk why but I can’t get the snake to move but the code is the same

  • @hallo7459
    @hallo7459 2 месяца назад

    Hi Otto, hast du das direkt aus dem Kopf programmiert oder dir vorher schon ein Konzept überlegt?
    Wie würdest du das Niveau des Codes einschätzen? Ist das schon Niveau aus der Berufswelt?

  • @rikajans
    @rikajans 11 месяцев назад

    Im having issues around 45:00 - 55:00 somewhere inbetween my code doesnt do the same as yours anymore since when i load the game nothing gets loaded anymore, i got the grid to work before but when i add the snake and the food and give the grid values it just opens the program with nothing, like no program opens at all

    • @rikajans
      @rikajans 11 месяцев назад +1

      Never mind I got it to work but ill keep this here incase anyone has a similar issue so you don't have to respond as often, i solved my issue going back to the gamestate code and finding an issue i wrote there. In my case i had put: for (int c = 0; c

  • @captainufo4587
    @captainufo4587 2 года назад +2

    Great tutorial! My first time monkeying with WPF, I learned a lot.
    I thought of using the same system to display an old console app game of mine, but it's slow as molasses (and before making my own, I tested your game with a field as large as 120x70 and it was pretty fast). Any suggestion? Can I write you in private?

    • @OttoBotCode
      @OttoBotCode  2 года назад +1

      This game is optimized more for simplicity than for speed, but it's good to hear that it works okay for larger grid sizes. How big is the grid in your game? You can contact me at ottobotcodehelp@gmail.com 😊

  • @lexical450
    @lexical450 Год назад

    Hello, i'm at 52:55, when i run the application sometimes the key presses do not work. And when they do, after i change the direction 3 times it stops working completely. Then the game "pauses"/freezes, not sure where i went wrong though.

    • @OttoBotCode
      @OttoBotCode  Год назад

      You can send your entire project to me at ottobotcodehelp@gmail.com. I'll take a look and see if I can spot the problem 😊

  • @mohamdrezamohamadiraftar343
    @mohamdrezamohamadiraftar343 Год назад

    thanks my brother 💙

  • @alejopgonzz
    @alejopgonzz Год назад

    Thanks a lot! you are awesome!

  • @hampus3699
    @hampus3699 2 года назад

    really nice video! thank you

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Thanks 😁 I appreciate it!

  • @Notwise66
    @Notwise66 4 месяца назад

    At 43:22 I Wrote GameGrid and a error appears it says does not exist in the current context

  • @DiimaPopov
    @DiimaPopov 2 года назад

    Great Job! Thank you

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Thanks for your comment 😀

  • @TechRithikesh
    @TechRithikesh Год назад +1

    I will Like and Subscribe in my 10 devices
    Very good video and easy way of coding
    thanks a lot , i hope you will be doing such good coding videos

  • @iceblocksoda8214
    @iceblocksoda8214 2 года назад

    thank you sir, it's very helpful

    • @OttoBotCode
      @OttoBotCode  2 года назад +1

      I'm glad you like it! Thanks for the comment 😉

  • @zellezxelece9016
    @zellezxelece9016 2 года назад +2

    Would one be able to add one or two more snakes to the game, and also add different type of foods to make the game more dynamic?

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Anything is possible! Both ideas sound like a fun additions to the game. I think different types of food would be easy to implement. How would multiple snakes work? Will all snakes be controlled by the user? 😉

    • @johnsoto7112
      @johnsoto7112 2 года назад

      @@OttoBotCode possibly multiplayer

  • @ocramkd
    @ocramkd 7 месяцев назад

    I know the basics of C++ and C#, I've programmed applications like to-do, the hanged man, a textual game using classes, objects, pointers etc... and still I'm having a lot of difficulties understanding this code. Even if I searched it yet I don't understand what is an hashcode for example

  • @paynemax3825
    @paynemax3825 Год назад +1

    Thank you very much! This is awesome! May I ask you how can I save the score with a nickname?

  • @gooffball9758
    @gooffball9758 2 года назад +2

    @OttoBotCode in 50 min mark how did you auto generate the Window_Loaded and Window_KeyDown methods? It gives me an error once I try to make them my self, and I cant see where in the code you wrote them.
    Thanks in advance :)

    • @OttoBotCode
      @OttoBotCode  2 года назад +2

      Let's say you want to create an event handler for the KeyDown event.
      Type in KeyDown="", then the text should show up.
      When it does, simple hit return and the method will be generated for you 😊

    • @gooffball9758
      @gooffball9758 2 года назад +1

      @@OttoBotCode Sweet thanks for the fast help, that fixed it :)

    • @Izzy-fr1zu
      @Izzy-fr1zu Год назад +1

      @@OttoBotCode Thanks, I was looking for that!

  • @francoislepron2301
    @francoislepron2301 6 месяцев назад

    Great job. This would have been awesome if you did it using Avalonia UI which is very close to WPF and is cross-platform. This is just an idea.

  • @its_eyjay6080
    @its_eyjay6080 2 года назад +2

    Hello can i get the whole copy of this game?

  • @esatzis4664
    @esatzis4664 Год назад

    Hello Otto,
    I would like to thank you from the bottom of my heart about this guide but i would like to ask you if you can help me with a problem i got. Right now i am at 49:51 of the video and when i run the program it says Exceptions Unhandled in a small box, and behind a new screen pops up with the title Symbol Loading Skipped. Can you please help ?

    • @OttoBotCode
      @OttoBotCode  Год назад

      You're very welcome 😊
      I can try to help, but I need to see your code to figure out what's going on. Can you send your entire project to ottobotcodehelp@gmail.com? It can be as a GitHub link or a zip.

  • @andriikozyriev3714
    @andriikozyriev3714 2 года назад

    Hello. Thank you for lesson. I started to learn c# 3moth ago. I did what you did until 1:00:00, but wheh I start project i see only square and inscription SCORE 0 from above. Maybe I don't tied up grid with main document or what can it be. In class Direction and Position i see ??? like :operator ==(Position? left, Position? ...) public override bool Equals(object? obj), maybe problem tied up with this?

    • @OttoBotCode
      @OttoBotCode  2 года назад

      The ? after the types indicates that they are "nullable". You can turn off the "nullable" feature (I show how to do it in the beginning of this video). Try turning it off and then generate Equals, GetHashCode, etc. again. That will get rid of those strange question marks.
      I'm not sure if this will fix your problem, if it doesn't just let me know 😁

    • @andriikozyriev3714
      @andriikozyriev3714 2 года назад

      @@OttoBotCode I just did't do assets like resourse. I think, programing is not mine, I even can't repeat that have already been done(

  • @coreyhunter25
    @coreyhunter25 Год назад

    At this part in the code under the gamestate class i am getting an error saying inconsistent accessibility for headposition, tailposition, and snakeposition. I have went over everything even rewatched your video up til this point to make sure everything was correct and it is and I can not figure out what is going on. Any ideas?
    public Position HeadPosition()
    {
    return snakePositions.First.Value;
    }
    public Position TailPosition()
    {
    return snakePositions.Last.Value;
    }
    public IEnumerable SnakePositions()
    {
    return snakePositions;

  • @unknowngamer5842
    @unknowngamer5842 2 года назад

    It is going really good but 44:30 when I am starting the program the grid is not appearing.

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Double-check that your assets are marked as resources. Let me know if it solves your problem

    • @iskrenos_gaming3545
      @iskrenos_gaming3545 2 года назад

      @@OttoBotCode I have the same problem i did chek and there is still no grid

    • @OttoBotCode
      @OttoBotCode  2 года назад

      @@iskrenos_gaming3545 You can send your project to me at ottobotcodehelp@gmail.com. I'll take a look and find the problem 😊

    • @ls167
      @ls167 Год назад

      @@OttoBotCode Thank you, very good tutorial. Did you by any chance find the problem? I have got the same issue.

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      @@ls167 I believe we did, but I don't remember what the issue was. First double check that your assets are marked as resources. If that doesn't fix it, you are also welcome to send me your project!

  • @alexanderivanov1672
    @alexanderivanov1672 2 года назад

    Hi I really like your tutorial, but I ran into a problem. I am on the 49:00 minute and if gives me error " No overload for "Window_Loaded" matches delegate "RoutedEventHandler" ". I rewrote the 46:00 to 49:00 minute code several times, but the same thing happened. I tried with debug but nothing happened. Can you help?

    • @OttoBotCode
      @OttoBotCode  2 года назад

      If you send your entire project (as a zip or github link) to ottobotcodehelp@gmail.com, I'll try to find the problem for you 😉

    • @alexanderivanov1672
      @alexanderivanov1672 2 года назад

      @@OttoBotCode I sent it

    • @OttoBotCode
      @OttoBotCode  2 года назад

      ​@@alexanderivanov1672 I've found the problem. It's explained in my email reply ☺

    • @alexanderivanov1672
      @alexanderivanov1672 2 года назад

      @@OttoBotCode Thank you very much! Appreciate it❤

    • @TestTest-zx4mk
      @TestTest-zx4mk Год назад

      @@alexanderivanov1672 You probably just forgot to remove the Window_Loaded Event in xaml

  • @SarcasticCupcake223
    @SarcasticCupcake223 2 года назад +1

    So far I am really enjoying this tutorial! However I have run into a problem that probably has a super easy fix but I just can't figure it out. At roughly 43:00 you input the code "GameGrid.Row = rows" and the same for column. My problem is it keeps saying that "GameGrid" doesn't exist. it is possible I missed where you created that, but I can't find where you did that.

    • @OttoBotCode
      @OttoBotCode  2 года назад +2

      GameGrid is the name we give the Uniform Grid in MainWindow.xaml. Make sure that your UniformGrid has the attribute x:Name="GameGrid" (I write it around 39:45). Hope this helps and good luck with the rest of the tutorial 😊

    • @SarcasticCupcake223
      @SarcasticCupcake223 2 года назад

      @@OttoBotCode Thank you for replying! It turns out I actually did name the Uniform Grid that, but after closing it and opening it, it seems visual studio has registered it properly now, so its all working now. Thanks again!

    • @OttoBotCode
      @OttoBotCode  2 года назад +1

      @@SarcasticCupcake223 You're welcome. "Bugs" like that seem to be a little too common in Visual Studio 😅

    • @SarcasticCupcake223
      @SarcasticCupcake223 2 года назад +1

      @@OttoBotCode Hey again, so I've run into another problem. for some reason, when you write the switch statement at 50:27 my one has a problem with the (e.Key) it keeps saying:
      " 'RoutedEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'RoutedEventArgs' could be found (are you missing a using directive or an assembly reference?)"

    • @OttoBotCode
      @OttoBotCode  2 года назад

      @@SarcasticCupcake223 I probably need a bit more information to find the problem. If you send your code to ottobotcodehelp@gmail.com I'll take a look!

  • @agataa6067
    @agataa6067 Год назад

    hey how could one make an 'easy gamemode' and once the snake hits the edge of the board it reapears on the other side?

  • @minininja5771
    @minininja5771 2 года назад

    hey on min 44:30 i cant seem to get the grid to show when i start the program.

    • @OttoBotCode
      @OttoBotCode  2 года назад

      If you are not getting any errors, my guess is that your assets are marked as "Content" instead of "Resource". Check out the "Importing Assets" part of this video 😊 If that doesn't help then let me know.

  • @Whatisbrotalkingabout
    @Whatisbrotalkingabout 8 месяцев назад

    At 53:21 when you change the text for "ScoreText," my program says "The name 'ScoreText' does not exist in the current context," do you know why this could be?

    • @Whatisbrotalkingabout
      @Whatisbrotalkingabout 8 месяцев назад

      Okay, just to be clear, the code still does work, it's just that Visual Studio says there's an error and underlines the code in red.

    • @OttoBotCode
      @OttoBotCode  8 месяцев назад

      @@Whatisbrotalkingabout If the program runs and works without issues, then perhaps the IDE is buggy. Have you tried restarting Visual Studio?

    • @Whatisbrotalkingabout
      @Whatisbrotalkingabout 8 месяцев назад

      @OttoBotCode I restarted Visual Studio, but it still shows an error. I guess I'll just ignore it, because the code works fine. Thanks for the video!

  • @coreyhunter25
    @coreyhunter25 Год назад

    Scratch my last comment I figured it out. Thank you for this tutorial it is helping me alot!

  • @katharxia7118
    @katharxia7118 Год назад

    HI! I got an error in the switch (e.Key). There is an error on the word key saying 'RoutedEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'RoutedEventArgs' could be found. What might be the problem?
    I'm not sure what I'm doing wrong so a help would be much appreciated!

  • @sujinlee2980
    @sujinlee2980 Год назад +1

    where is the right place to call SnackPositions() ?

  • @lukaslp77
    @lukaslp77 2 года назад

    at 43:14
    line 28 gives me an error and says "The name 'InitializeComponent' Does not exist in the current context"
    same goes for GameGrid in line 34 and 35
    does anyone know why that happens?

    • @OttoBotCode
      @OttoBotCode  2 года назад

      It seems like your xaml file (MainWindow.xaml) and code-behind (MainWindow.xaml.cs) are not properly connected. Send your code to ottobotcodehelp@gmail.com then I'll see if I can spot the problem 😊

    • @OttoBotCode
      @OttoBotCode  2 года назад

      I've figured out the problem and sent you an email response! 😉

    • @lukaslp77
      @lukaslp77 2 года назад +1

      @@OttoBotCode omg! thank you so much! :D

    • @lukaslp77
      @lukaslp77 2 года назад

      @@OttoBotCode oml this is so annoying and I'm sorry for bothering you again, but at 1:00:34 in line 152
      "Property or indexer 'GameState.Dir' cannot be assigned to -- it is read only"
      should I send you my code over email again?

    • @OttoBotCode
      @OttoBotCode  2 года назад

      @@lukaslp77 Your code has this property in GameState:
      public Direction Dir { get; }
      It is read-only (can only be assigned from constructor). You don't want that in this case, so change it to:
      public Direction Dir { get; private set; }

  • @zelowatch30
    @zelowatch30 Год назад

    I like this. Finally a real person. I'm so tired of the stupid AI voices and videos.

    • @OttoBotCode
      @OttoBotCode  Год назад

      Glad you like it! Thanks for your comment 😊

  • @schav9408
    @schav9408 Год назад

    Hello!!! you just gain a new subscriber here!!!
    I just wanted to ask a question on can you write a code so that every 10 food eaten the snake will get faster and faster? thank you!!!

    • @OttoBotCode
      @OttoBotCode  Год назад

      Awesome, thank you 👍
      You can create a variable for the delay in the gameLoop, which you change depending on the current score.
      For a concrete example, check out my Tetris tutorial. I do something very similar in the game loop for that project! 😊

  • @nicholasflavin2961
    @nicholasflavin2961 Год назад

    Hi Otto, Thank you so much for the tutorial. This helped me learn a lot about creating things in C#. I was using this to try and learn how to create windows and games in C#, however, what I want to do will need a lot larger grid then the 15x15 for snake. I was wondering if this method of creating and drawing the grid is scalable to something like H:350xW:1000? When I try to use large numbers with the code of the snake game (i.e H:200xW:400) the game window doesn't open.

    • @OttoBotCode
      @OttoBotCode  Год назад

      Although it is convenient to use a UniformGrid filled with Image Controls, it's not great for performance. For very large grids, it won't work (at least not well). You need to find a different way of drawing the GameState if you want to use a large grid. I don't know what options are available for fast 2D drawing in C#, but I'm sure something exists.

  • @MCAltar68
    @MCAltar68 Год назад

    hello OttoBotCode ! I would like to say that your tutorial is amazing to understand C# syntaxt and principles. Although, I have a question : In the Direction Class, won't the lines where you initialize LEFT, RIGHT, DOWN, UP create a spiral and themselves create a Direction class that will repeat these lines etc? because you used "new Direction(0, -1)" won't this create a new Direction class?
    Thanks.
    PS : I'm french so my question could be badly formulated

    • @OttoBotCode
      @OttoBotCode  Год назад +2

      The new keyword creates an instance of the given class and calls it's constructor.
      "Normal" class variables are instance variables, which means they belong to an instance (or object) of a class.
      Left, Right, Down and Up are static variables. Static variables belong to the class itself, so they will only be initialized once.
      There are simply 4 predefined instances of the Direction class. It is not the case that each Direction instance contains 4 other Direction instances. That would indeed be really bad!
      Thanks for the kind words ☺

  • @cheapproductions6389
    @cheapproductions6389 Год назад

    At 1:00:00 the snake still dies when I make it move in the opposite direction can anyone help me?

    • @OttoBotCode
      @OttoBotCode  Год назад

      If you are interested I can take a look. Just send your entire project to ottobotcodehelp@gmail.com 😊

  • @GravitraxMasters69
    @GravitraxMasters69 Год назад

    ​ @OttoBotCode im having problem with this line "List empty = new List(Convert.ToInt32(EmptyPositions()));​" in the AddFood() method

    • @OttoBotCode
      @OttoBotCode  Год назад

      The line is:
      List empty = new List(EmptyPositions());
      Do you get an error when you use that?

  • @Gusseman69
    @Gusseman69 Год назад

    Hello OttobotCode i have a problem with the snake and the grid. The problem is that the snake and the grid are not showing on the screen but they exist so i can score points and the snake dies but i can not see anything that is happening

    • @OttoBotCode
      @OttoBotCode  Год назад

      It sounds like your assets are not found. Can you double check that they are marked as "Resources"?

    • @Gusseman69
      @Gusseman69 Год назад +1

      Found the problem it was in the images class. It is when you write public readonly static but it does not work for me so i change them around like public static readonly and it worked

    • @Gusseman69
      @Gusseman69 Год назад

      @@OttoBotCode thank you for your help it was still usefull for findning the problem

  • @alext2886
    @alext2886 3 месяца назад

    i have error i cant understand what is going wrong please help
    Severity Code Description Project File Line Suppression State
    Error (active) CS0050 Inconsistent accessibility: return type 'Position' is less accessible than method 'GameState.HeadPosition()' SnakeGame
    Error (active) CS0050 Inconsistent accessibility: return type 'Position' is less accessible than method 'GameState.TailPosition()' SnakeGame
    Error (active) CS0050 Inconsistent accessibility: return type 'IEnumerable' is less accessible than method 'GameState.SnakePositions()' SnakeGame

    • @OttoBotCode
      @OttoBotCode  3 месяца назад

      It sounds like the problem is that your Position class is either internal or private. The methods (HeadPosition, TailPosition, SnakePositions) are public so they cannot return instances of a less visible class (internal, private).
      Your position class should be made public, as I've done in this video. Hope this helps! 😊

  • @optic8268
    @optic8268 Год назад

    Hi there, haven't finished the tutorial yet but it seems really good. One problem I have is that App.xaml is not appearing on the solution explorer sidebar, what is the specific fix for that?

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      Hmmm, did you create a WPF project when you started? Do you have MainWindow.xaml and MainWindow.xaml.cs?

    • @optic8268
      @optic8268 Год назад

      @@OttoBotCode oh right, thanks. i started with a WPF class library, instead of WPF application. thanks for the help!

    • @optic8268
      @optic8268 Год назад

      @@OttoBotCode lol need a bit of help again, at 44:42, when i try to start the program, nothing pops up. It says there is no build errors so im just a bit confused

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      @@optic8268 No problem. It's because your assets are not loading correctly.
      Double check that they are marked as "Resource" (I do it in the "Importing Assets" section).
      And make sure you have spelled "Assets" correctly (both the folder itself and the path in Images.cs)
      😊

    • @aidenstocking8910
      @aidenstocking8910 Год назад

      @@optic8268 Did you manage to fix it? I'm having the same issue and the other suggested fixes aren't working, can't find anything online, very confused.

  • @benyapat.453
    @benyapat.453 Год назад

    Excuse me, i to write your code and everything runs except my snake. It doesn't move anywhere. Can you help me please? I've looked at it several times but can't find any mistakes.

    • @OttoBotCode
      @OttoBotCode  Год назад

      Send your project to ottobotcodehelp@gmail.com. I'll take a look 😉

  • @Sync__s
    @Sync__s Год назад +1

    hey! great video, im having trouble since this is kind of an old version , can u help and create an updated version? thanks!

    • @OttoBotCode
      @OttoBotCode  Год назад

      I believe everything should work even with the newest version of C#. What kind of issues are you having?

  • @zainuddin5591
    @zainuddin5591 11 месяцев назад

    for some reason the grid is not showing up when i run the code what exactly makes the grid and how do i fix it.

    • @OttoBotCode
      @OttoBotCode  11 месяцев назад +1

      The grid is made by displaying the empty squares next to each other. If it doesn't show up, it's most likely because your assets are not loading correctly. Double check that they are marked as resources and that you've typed their paths correctly in Images.cs 😊

    • @zainuddin5591
      @zainuddin5591 11 месяцев назад

      yes i forgot to set the build action to resource
      @@OttoBotCode

  • @PASHKULI
    @PASHKULI Год назад

    Where is the part where the Food does not get placed on the Snake?

  • @craig_austin
    @craig_austin Год назад

    Do you have the final project on Github? I have gone wrong somewhere and want to compare your code to mine. This will help to see where I have gone wrong

  • @catssmacks_
    @catssmacks_ 2 года назад

    Hello, Thank You for the great tutorial but i ran into a problem with the DrawSnakeHead method that you did at 1:14:43 and it looks like this:
    private void DrawSnakeHead()
    {
    Position headPos = gameState.HeadPositon();
    Image image = gridImages[headPos.Row, headPos.Col];
    image.Source = Images.Head;
    int rotation = dirToRotation[gameState.Dir];
    image.RenderTransform = new RotateTransform(rotation);
    }
    but it gives an error on dirToRotation

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Hello! What does the error say?

    • @catssmacks_
      @catssmacks_ 2 года назад

      @@OttoBotCode i cant tell you the exact error rn but i remember it said that it doesn't exist in that context

    • @catssmacks_
      @catssmacks_ 2 года назад

      @@OttoBotCode the exact error is : The name 'dirToRotation' does not exist in the current context

    • @catssmacks_
      @catssmacks_ 2 года назад

      @@OttoBotCode Nevermind I managed to find the problem myself in the head direction method I have typed it completely wrong. Sorry for the bother!

    • @natsumeinnit1758
      @natsumeinnit1758 2 года назад

      @@catssmacks_ im getting same problem help me out

  • @its02ninety29
    @its02ninety29 Год назад

    hey i was following our steps but i run into problem, it tells me error on (Icon="Assets/icon.ico"). when i deleted it. it launched game but there no grid lines on game it only sad SCORE 0 and game box. i recheck everything but still no difference, even it shows (no issues found). everything is correct but still no lines on game. then i added game states but still nothing. can you help me?

    • @OttoBotCode
      @OttoBotCode  Год назад

      Your assets are not being found. Most people who had this issue either didn't mark their images as "Resource" or spelled "Assets" wrong (either the folder itself or the path in Images.cs). Can you check if that is the case for you?

    • @its02ninety29
      @its02ninety29 Год назад

      @@OttoBotCode thenks men I'll check

    • @its02ninety29
      @its02ninety29 Год назад

      @@OttoBotCode found it my Assets folder name was assits and thats why it didnt show

  • @abhinavprabhakar455
    @abhinavprabhakar455 2 года назад

    Do you have any plans for videos MAUI? Looking forward for that

  • @emanbhatti585
    @emanbhatti585 7 месяцев назад

    what did you say in the first 10 min video about the key, the key is cntrl + ...... please tell me

  • @邓翔-v7j
    @邓翔-v7j Год назад

    hey!could i ask about if i met a problem that in the 40 minutes part of the video i try to start my game to check the grid but the grid didn't appear,what's wrong with that ?i am really confused

    • @OttoBotCode
      @OttoBotCode  Год назад

      It's most likely because your assets did not load correctly. Make sure they are marked as "Resource" and that the asset names are spelled correctly in Images.cs.

    • @邓翔-v7j
      @邓翔-v7j Год назад

      @@OttoBotCode thank u for ur help

    • @邓翔-v7j
      @邓翔-v7j Год назад

      @@OttoBotCode Can you show me a way to learn c# development games? I don't know where to start.

  • @azekeilaze9908
    @azekeilaze9908 Год назад

    Could you please make tutorial about how to add a high score on the snake thank you.

  • @iskrenos_gaming3545
    @iskrenos_gaming3545 2 года назад

    it is teling me Error CS1061 'KeyboardEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'KeyboardEventArgs' could be found (are you missing a using directive or an assembly reference?) Can you help thanks

    • @OttoBotCode
      @OttoBotCode  2 года назад

      Do you get this error in the event handler for the key down event? The argument should be"KeyEventArgs" not "KeyboardEventArgs".

  • @Махлун
    @Махлун Год назад +1

    Hey. May you tell which patterns of OOP do you use here, please?

  • @nic00la1
    @nic00la1 Год назад

    Awesome Project! 😎💪💪 Thank You so much!

    • @OttoBotCode
      @OttoBotCode  Год назад

      Glad you like it and thanks for your comment!

  • @GravitraxMasters69
    @GravitraxMasters69 Год назад

    hi at 14:02 it says this error: "LinkedList does not exist"

    • @OttoBotCode
      @OttoBotCode  Год назад

      Make sure that you have:
      using System.Collections.Generic;
      At the top of the file 😉

  • @ceejaycordero
    @ceejaycordero Год назад

    hi i love the tutorial, but i have a problem with the code the food of the snake is not showing can you help me fix it?

    • @OttoBotCode
      @OttoBotCode  Год назад

      Yes if you send your project to me at ottobotcodehelp@gmail.com 😊

    • @ceejaycordero
      @ceejaycordero Год назад

      @@OttoBotCode sent

  • @safakhan6574
    @safakhan6574 Год назад

    Hi there, I’ve just emailed you again regarding my code. When I press any key to start the game the snake doesn’t move and dies on the spot. Do you know what I’ve done wrong?

  • @moletes
    @moletes Год назад

    By the way i found a bug...sometimes food appears on snake body...did you notice ?

    • @OttoBotCode
      @OttoBotCode  Год назад

      That doesn't happen in my game. Maybe you have written something wrong in "AddFood" or "EmptyPositions" in GameState.cs?
      AddFood should only be able to pick a position which contains nothing

    • @moletes
      @moletes Год назад

      @@OttoBotCode yes i found it was missing IF condition GameGrid empty :) !! I will now try to implement new things then i share ! tks a lot

  • @TrollHeroActual
    @TrollHeroActual 9 месяцев назад

    Visual Studio 2022 has deadlined WPF. It's not available in Visual Studio anymore. Is there an alternative way to set up the project in VS 2022?

    • @OttoBotCode
      @OttoBotCode  9 месяцев назад +1

      I've just updated my installation of VS 2022 and WPF is available! Open the "Visual Studio Installer" and "Modify" your installation. Under "Desktop and Mobile" make sure you have ".NET desktop development" installed.

    • @TrollHeroActual
      @TrollHeroActual 9 месяцев назад

      @@OttoBotCode Thank you!!! :)

  • @isacericsson7218
    @isacericsson7218 Год назад

    hi! ive ran into a bit of a problem where the grid doesnt appear when i launch the game

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      Most likely your assets are not loading. Usually people either forget to mark the images as "Resource" or they spell "Assets" wrong (either in Images.cs or the folder itself). Please check if that is the case for you 😊

    • @isacericsson7218
      @isacericsson7218 Год назад

      @@OttoBotCode Thank you for the fast answer! i used ctrl + f "grid" on the comments section and i saw the guy spelled assets wrong and i prob have something stupid like that aswell :D, is it around 30:00 min you are marking the images as resources? Thanks again for replying!

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      @@isacericsson7218 You're welcome! It's in the "Importing Assets" section around 2:00

    • @isacericsson7218
      @isacericsson7218 Год назад

      @@OttoBotCode Otto i would kiss you right on the lips rn if you were here. Thank you so much.

    • @OttoBotCode
      @OttoBotCode  Год назад +1

      @@isacericsson7218 Hahaha that's great I suppose 😆

  • @runestamp89
    @runestamp89 Месяц назад

    funny thing is...i learned all the fundamentals of C# but...i dont get absolutely anything. There are so many comands and elements ive never seen before.

  • @PASHKULI
    @PASHKULI Год назад

    You need a special Tail .png or .svg for the End of the snake… maybe a triangle shape. A bit more work (especially changing direction of that Tail), but would be much more real.