如何移动蛇游戏的最后一个图片框

how to move last picturebox for snake game


我想用 C# 制作自己的贪吃蛇游戏。我已经开始了几次(比如 2 或 3 次),但是之间的间隔太大了,所以看起来更容易编写新代码然后尝试理解现有代码(我知道,因此存在评论,但有时我进入编码并完全忘记评论)。
所以现在我想做最后的尝试。我对几乎所有事情都有一些想法,但我不知道如何弄清楚我的蛇的最后一个图片框是什么!一般来说蛇body由很多个PictureBox组成,每次它吃食物的时候最后都会加一个PictureBox。

为了让蛇穿过屏幕,我想把最后一个 PictureBox(在蛇的末端 body)移动到蛇的前面,这样它就变成了蛇头。 body 将由代码生成,因此要跟踪我可以使用列表的所有框。但我不知道如何找出最后一个盒子,如果我找到了,我如何才能只将这个盒子移动到蛇的开头。

我会很感激你能给的每一个提示,
感谢您花时间阅读本文! :)

I have no clue how I can find out which box is the last one and if I found out, how I could move only this box to the beginning of the snake.

任何集合(List、Queue、Stack 等)或数组都可用于跟踪对象的顺序。您的情况听起来最好使用先进先出的集合。队列就是按照这个原则工作的。我将使用字符串来说明它是如何工作的:

Queue<string> queue = new Queue<string>();

queue.Enqueue("one"); //add to queue
//queue is now ["one"]
queue.Enqueue("two"); //queue is now ["one", "two"]
queue.Enqueue("three"); //queue is now ["one", "two", "three"]

string firstString = queue.Dequeue(); //sets string to "one" and removes it from queue
//queue is now ["two", "three"]
queue.Enqueue("four"); //queue is now ["two", "three", "four"]
queue.Enqueue(queue.Dequeue()); //moves first element to the back of the queue
//queue is now ["three", "four", "two"]

Queue 支持任何数据类型,您可以声明一个 PictureBox Queue 来跟踪您的蛇。