Click here to Skip to main content
15,798,592 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys tried creating my first game today SNAKE

I have followed a tutorial on mooict which was really good only problem is that the game starts i see food but i cannot see the snack and Visual Studio is showing me no errors and help would be great as i was doing this to help my coding at college :D

Settings Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake_1
{
    public enum Directions
        {
            Left,
            Right,
            Up,
            Down,
        };
    
    class Settings
    {
        public static int Width { get; set; }
        public static int Height { get; set; }
        public static int Speed { get; set; }
        public static int Score { get; set; }
        public static int Points { get; set; }
        public static bool GameOver { get; set; }
        public static Directions direction { get; set; }

        public Settings()
        {
            Width = 16;
            Height = 16;
            Speed = 20;
            Score = 0;
            Points = 100;
            GameOver = false;
            direction = Directions.Down;
        }
    }  
}


Circle Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections;
using System.Windows.Forms;


namespace Snake_1
{
    class Circle
    {
        public int X { get; set; }
        public int Y { get; set; }

        public Circle()
        {
            X = 0;
            Y = 0;
        }
    }
}


Input code
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Windows.Forms;

namespace Snake_1
{
    class Input
    {
        private static Hashtable Keytable = new Hashtable();

        public static bool KeyPress(Keys key)
        {
            if(Keytable[key] == null)
            {
                return false;
            }

            return (bool)Keytable[key];
        }

        public static void changeState(Keys key, bool state)
        {
            Keytable[key] = state;
        }
    }
}



Form Code

C#
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake_1
{
    public partial class Form1 : Form
    {
        private List<Circle> Snake = new List<Circle>();
        private Circle food = new Circle();
        public Form1()
        {
            InitializeComponent();

            new Settings();

            gameTimer.Interval = 1000 / Settings.Speed;
            gameTimer.Tick += updateScreen;
            gameTimer.Start();

            startGame();
        }

        private void updateScreen(object sender, EventArgs e)
        {



            if (Settings.GameOver == true)
            {

                if (Input.KeyPress(Keys.Enter))
                {
                    startGame();
                }

            }
            else
            {
                if (Input.KeyPress(Keys.Right) && Settings.direction != Directions.Left)
                {
                    Settings.direction = Directions.Right;
                }
                else if (Input.KeyPress(Keys.Left) && Settings.direction != Directions.Right)
                {
                    Settings.direction = Directions.Left;
                }
                else if (Input.KeyPress(Keys.Up) && Settings.direction != Directions.Down)
                {
                    Settings.direction = Directions.Up;
                }
                else if (Input.KeyPress(Keys.Down) && Settings.direction != Directions.Up)
                {
                    Settings.direction = Directions.Down;
                }

                movePlayer();

            }

            pbCanvas.Invalidate();

        }

        private void movePlayer()
        {
            for (int i = Snake.Count - 1; i >= 0; i--)
            {
                if (i == 0)
                {
                    switch (Settings.direction)
                    {
                        case Directions.Right:
                            Snake[i].X++;
                            break;
                        case Directions.Left:
                            Snake[i].X--;
                            break;
                        case Directions.Up:
                            Snake[i].Y--;
                            break;
                        case Directions.Down:
                            Snake[i].Y++;
                            break;
                    }

                    int maxXpos = pbCanvas.Size.Width / Settings.Width;
                    int maxYpos = pbCanvas.Size.Height / Settings.Height;

                    if (
                        Snake[i].X < 0 || Snake[i].Y < 0 ||
                        Snake[i].X > maxXpos || Snake[i].Y > maxYpos
                        )
                    {
                        die();
                    }

                    for (int j = 1; j < Snake.Count; j++)
                    {
                        if (Snake[i].X == Snake[j].X && Snake[j].Y == Snake[j].Y)
                        {
                            die();
                        }
                    }

                    if (Snake[0].X == food.X && Snake[0].Y == food.Y)
                    {
                        eat();
                    }

                }

                else
                {
                    Snake[i].X = Snake[i - 1].X;
                    Snake[i].Y = Snake[i - 1].Y;
                }
            }
        }

        private void Keyisdown(object sender, KeyEventArgs e)
        {
            Input.changeState(e.KeyCode, true);
        }

        private void Keyisup(object sender, KeyEventArgs e)
        {
            Input.changeState(e.KeyCode, false);
        }

        private void updateGraphics(object sender, PaintEventArgs e)
        {
            Graphics canvas = e.Graphics;

            if (Settings.GameOver == false)
            {
                Brush snakeColour;

                for (int i = 0; i < Snake.Count; i++)
                {
                    if (i == 0)
                    {
                        snakeColour = Brushes.Black;
                    }
                    else
                    {
                        snakeColour = Brushes.Green;
                    }

                    canvas.FillEllipse(Brushes.Red,
                                         new Rectangle(
                                             food.X * Settings.Width,
                                             food.Y * Settings.Height,
                                             Settings.Width, Settings.Height
                                             ));
                }
            }
            else
            {
                string gameOver = " Game Over \n" + "Final Score is" + Settings.Score + "\n Press enter to Restart \n";
                label3.Text = gameOver;
                label3.Visible = true;
            }
        }

        private void startGame()
        {
            label3.Visible = false;
            new Settings();
            Snake.Clear();
            Circle head = new Circle { X = 10, Y = 5 };
            Snake.Add(head);

            label2.Text = Settings.Score.ToString();

            generateFood();
        }

        private void generateFood()
        {
            int maxXpos = pbCanvas.Size.Width / Settings.Width;
            int maxYpos = pbCanvas.Size.Height / Settings.Height;
            Random rnd = new Random();
            food = new Circle { X = rnd.Next(0, maxXpos), Y = rnd.Next(0, maxYpos) };
        }

        private void eat()
        {
            Circle body = new Circle
            {
                X = Snake[Snake.Count - 1].X,
                Y = Snake[Snake.Count - 1].Y
            };

            Snake.Add(body);
            Settings.Score += Settings.Points;
            label2.Text = Settings.Score.ToString();
            generateFood();
        }

        private void die()
        {
            Settings.GameOver = true;
        }
    }
}


What I have tried:

Im new to coding so i have just been able to check over the tutorial and all seems fine had a go at trouble shooting myself but all appears okay i dont know if the code is updating graphics or i did something wrong.

Thank you for any help :D
Posted
Updated 30-Apr-18 10:16am

If you got the code from a tutorial on a different site, then the best thing to do is to go back to that site and ask there: just dumping your code here and effectively saying "sort this out for me" is rather rude, particularly so when no-one here is in any way related to that code or the tutorial itself.

And if the tutorial doesn't work, then they need to know that too...
 
Share this answer
 
Comments
Member 13799439 29-Apr-18 10:32am    
Sorry if you feel that way, i just assumed it was a programming forum and people would like to be helpful particularly because its for educational purposes. the website does not have a forum, its not sorting it out, it would be help a possible future coder progress, yes i didn't say that in the message but didn't expect people to get annoyed enough to send a rude message, it probably does work for them but i have coded it wrong.
OriginalGriff 29-Apr-18 10:40am    
:laugh: That wasn't rude! If that offended you, then you will need to develop a lot thicker skin to survive long on the internet...

Seriously: look at what you did. Dumped a large pile of "student grade" code on a "random" site and expected them to sort it all out for you. That isn't good - it isn't showing that you are trying at all, just that you can google a bit.
To be honest, if the tutorial site doesn't have any comment facility, and doesn't seem to respond to requests in a reasonable time (and bear in mind weekends, different time zones and so forth) then the chances are it's a bit crap and you might be better off looking for a better - or at least better supported - one.
Dave Kreskowiak 30-Apr-18 15:59pm    
If that's the code that the tutorial walked you through, there's a ton of stuff wrong with it, not the least of which is it's not following proper Windows Forms painting methodology. It can be fixed, with a complete rewrite.

Serisouly, if this is the path the tutorial led you down, find another tutorial. This one stinks.
The party that developed the code (you are trying to run in one whack) was developed in "stages"; with each part "tested"; and then "integrated".

Your task is to reverse the process; break out the components; and test ONLY the part that actually "writes" to the screen; and figure out what the problem is.

Falls under "reverse engineering"; and something to learn if you want to "progress".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900