Создал змеиную игру, все это выглядит великолепно, но не могу увидеть змею
Эй ребята попробовали создать мою первую игру сегодня змея
Я следовал учебнику по mooict, который был действительно хорош, только проблема в том, что игра начинается, я вижу еду, но не вижу закуски, и Visual Studio не показывает мне никаких ошибок, и помощь была бы отличной, так как я делал это, чтобы помочь моему кодированию в колледже :D
Код Настроек
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; } } }
Код Круга
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; } } }
Входной код
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; } } }
код формы
<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; } } }
Что я уже пробовал:
Я новичок в кодировании, поэтому я только что смог проверить учебник, и все кажется прекрасным, у меня были проблемы с стрельбой, но все кажется нормальным, я не знаю, обновляет ли код графику или я сделал что-то не так.
Спасибо за любую помощь :D