本文介绍了我完全按照YouTube视频描述写了一个Snake游戏,但我仍然无法让我的蛇移动。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发誓我跟着这个家伙的教程写了这封信;我的代码中没有错误。当窗口打开时,指示玩家按空格键以开始游戏,使用箭头键移动蛇,并获得最高分。介绍标签消失,窗口中出现一块食物,但没有一个箭头键会移动蛇。

有2个班级; Snake and Food,以及Windows Form。表格中有3个事件处理程序; Draw,KeyDown和Load。

在阅读代码之前,您需要了解一些关于我的事情。编程中我最大的障碍是我的愿景;我的右眼有20/400,光线和光线都是我左手的动作。为了在电脑上做任何事情,我需要放大至少300-450%。如果您发现的错误很简单,请不要感到惊讶;想象一下放大300倍的YouTube视频;它非常模糊!我主要是在听我能说到的东西以及我能看到的东西。我能够破译大部分文本,但是我错过了一些小而重要的东西的机会非常高。

这是表格的代码:

I swear I followed this guy's tutorial to the letter; I have no errors in my code. When the window is opened, the player is instructed to press the spacebar in order to start the game, use the arrow keys to move the snake, and get the highest score. The intro label disappears and a piece of food appears in the window, but none of the arrow keys will move the snake.
There are 2 classes; Snake and Food, as well as the Windows Form. There are 3 event handlers in the form; Draw, KeyDown, and Load.
There is something you need to know about me before you read the code. My biggest obstacle in programming is my vision; I have 20/400 in my right eye, and light & hand motion in my left. In order to do anything on the computer, I need magnification of at least 300-450%. Please do not be surprised if the error you find is a simple one; imagine watching a YouTube video at 300+ magnification; it is very blurry! I am mostly going on what I can hear and what little I can see. I was able to decipher most of the text, but the chance that I missed something small yet important is very high.
Here is the code for the form:

	public partial class FRM_Snake : Form
	{
		Random foodPiece = new Random();
		Graphics g;
		Snake snake = new Snake();
		Food food;
		bool moveLeft = false;
		bool moveUp = false;
		bool moveRight = false;
		bool moveDown = false;
		int Score = 0;
		public FRM_Snake()
		{
			InitializeComponent();
			food = new Food(foodPiece);
		}
		private void FRM_Snake_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Space)
			{
				timer1.Enabled = true;
				LBL_Begin.Text = "";
				moveUp = false;
				moveDown = true;
				moveLeft = false;
				moveRight = false;
			}
			if (e.KeyData == Keys.Down && moveUp == false)
			{
				moveDown = true;
				moveUp = false;
				moveLeft = false;
				moveRight = false;
				snake.MoveDown();
			}
			if (e.KeyData == Keys.Up && moveDown == false)
			{
				moveDown = false;
				moveUp = true;
				moveLeft = false;
				moveRight = false;
				snake.MoveUp();
			}
			if (e.KeyData == Keys.Left && moveRight == false)
			{
				moveDown = false;
				moveUp = false;
				moveLeft = true;
				moveRight = false;
				snake.MoveLeft();
			}
			if (e.KeyData == Keys.Right && moveLeft == false)
			{
				moveDown = false;
				moveUp = false;
				moveLeft = false;
				moveRight = true;
				snake.MoveRight();
			}
		}
		private void FRM_Snake_Paint(object sender, PaintEventArgs e)
		{
			g = e.Graphics;
			food.drawFood(g);
			snake.DrawSnake(g);
		}
		private void FRM_Snake_Load(object sender, EventArgs e)
		{
			if (moveDown)
			{
				snake.MoveDown();
			}
			if (moveUp)
			{
				snake.MoveUp();
			}
			if (moveLeft)
			{
				snake.MoveLeft();
			}
			if (moveRight)
			{
				snake.MoveRight();
			}	this.Invalidate();
		}
		private void timer1_Tick(object sender, EventArgs e)
		{
			this.TSL_Score.Text = Convert.ToString(Score);
			if (moveDown)
				snake.MoveDown();
			if (moveUp)
				snake.MoveUp();
			if (moveLeft)
				snake.MoveLeft();
			if (moveRight)
				snake.MoveRight();
			//Collision Detection
			for (int i = 0; i < snake.MySnake.Length; i++)
			{
				if (snake.MySnake[i].IntersectsWith(food.foodPiece))
				{
					Score += 20;
					snake.growSnake();
					food.FoodLocation(foodPiece);
				}
			}
		}
		public void Collisoin()
		{
			for (int i = 1; i < snake.MySnake.Length; i++)
			{
				if (snake.MySnake[0].IntersectsWith(snake.MySnake[i]))
				{
					MessageBox.Show("Ouch! Quit biting yourself!\n Your total score is " + Score);
					Restart();
				}
			}
			//Use if you want game to be over when snake hits the edge of the screen
			if (snake.MySnake[0].X < 0 || snake.MySnake[0].X > 480)
			{
				MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
				Restart();
			}
			if (snake.MySnake[0].Y < 0 || snake.MySnake[0].Y > 480)
			{
				MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
				Restart();
			}
		}
		public void Restart()
		{
			Score = 0;
			this.TSL_Score.Text = "0";
			timer1.Enabled = false;
			this.LBL_Begin.Text = "Don't space out!\nPress Spacebar to begin";
			snake = new Snake();
		}
	}
</pre>
Here is the Snake class; I don't think the Food class has anything to do with my snake's lack of movement :)
<pre>	class Snake
	{
		private Rectangle[] mySnake;
		private SolidBrush myBrush;
		private int x;
		private int y;
		private int width;
		private int height;
		public Rectangle[] MySnake
		{
			get
			{
				return mySnake;
			}
		}
		//Constructor
		public Snake()
		{
			//Basic drawing components of the snake
			mySnake = new Rectangle[3];
			myBrush = new SolidBrush(Color.White);
			//The position of the snake on the game
			x = 16;
			y = 16;
			//Width and height of the snake
			width = 16;
			height = 16;
			//Give the snake a base so it can grow
			for (int i = 0; i &lt; mySnake.Length; i++)
			{
				mySnake[i] = new Rectangle(x, y, width, height);
				x -= 16;
			}
		}
		public void DrawSnake(Graphics g)
		{
			foreach (Rectangle rec in mySnake)
			{
				g.FillRectangle(myBrush, rec);
			}
		}
		public void DrawSnake()
		{
			for (int i = mySnake.Length - 1; i &gt; 0; i--)
			{
				mySnake[i] = mySnake[i - 1];
			}
		}
		public void MoveDown()
		{
			DrawSnake();
			mySnake[0].Y += 16;
		}
		public void MoveUp()
		{
			DrawSnake();
			mySnake[0].Y -= 16;
		}
		public void MoveLeft()
		{
			DrawSnake();
			mySnake[0].X -= 16;
		}
		public void MoveRight()
		{
			DrawSnake();
			mySnake[0].X += 16;
		}
		public void growSnake()
		{
			List&lt;Rectangle&gt; Rec = mySnake.ToList();
			Rec.Add(new Rectangle(mySnake[mySnake.Length - 1].X, mySnake[mySnake.Length - 1].Y, width, height));
			mySnake = Rec.ToArray();
		}
	}



[]

由于作者不再拥有网站,因此无法找到您眯眼的示例项目时无效:)


http://www.youtube.com/user/TutorialHouseNz#p/a[^]
It doesn't help when the sample project you're squinting at cannot be found because the author no longer has a website, either :)

推荐答案

private void timer1_Tick(object sender, EventArgs e)
{
    this.TSL_Score.Text = Convert.ToString(Score);
    if (moveDown)
        snake.MoveDown();
    if (moveUp)
        snake.MoveUp();
    if (moveLeft)
        snake.MoveLeft();
    if (moveRight)
        snake.MoveRight();

    this.Invalidate();
}





此外,不需要'FRM_Snake_Load'功能,因为只在表单加载时调用一次,这是你重绘蛇的形式无效的地方,但这是无效的。



希望这会有所帮助。



Also the 'FRM_Snake_Load' function is not needed, as this is only called once when the form is loaded and this was where you were invalidating the form to redraw the snake, but this is ineffective.

Hope this helps.


这篇关于我完全按照YouTube视频描述写了一个Snake游戏,但我仍然无法让我的蛇移动。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:40