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
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
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
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
<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