C#Winform全原创贪吃蛇无bug极致简易小白必学逻辑完整代码
作者写给小白的话:
百度其实有很多贪吃蛇代码,但要么代码过于复杂根本看不懂,要么bug一堆为乞丐版贪吃蛇,要么解释不清楚为残缺代码,还有的是用数据结构弄的贪吃蛇,小白根本都不知道发生了什么,非常不适合学习,顶多复制看看就没了。
本程序特点:
基本把已知的bug解决完,简单易懂都是用基础做的,没有加数据结构。没有加任何花里胡哨的东西,代码明了简介,非常适合小白进行学习!
1. 程序功能介绍:
就是个贪吃蛇的游戏,没啥说的!
2. 代码功能解释:
snake_show()//初始化蛇的生成界面
move_Tick()//计时器自动移动
KeyPress()//接受按键控制方向
egg_show()//蛋的生成
eat()//吃蛋的过程
cheak()//检测函数
tail_show()//控制蛇尾的生成方向
snake_bug()//蛇吃到自己结束游戏
wall_bug()//撞墙结束游戏
egg_bug()//蛋刷到蛇身的bug
禁止按下反方向键移动bug
3. 全部完整代码:
-
创建winform项目,命名为MRTCS ------(末日贪吃蛇)
ps:主要之前一开始弄的是tcs(贪吃蛇),然后又弄了cjtcs(超级贪吃蛇),之后弄了zztcs(最终贪吃蛇)
-
直接从工具箱拖2个timer计时器,改名为move和cheak,move间隔为300,cheak间隔为1,窗口大小设为1000,1000
-
然后复制一下代码
-
最后把代码里面的事件放进相应的控件中(load,move,cheak,KeyPress)
-
运行ok!
以下代码:
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 MRTCS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int x = 0; int y = 0; int count = 0;
char fxp = 's';
char oldfxp = 'w';
bool egg_exist = false;
Label[,] map = new Label[100, 100];//地图的二维数组
Label[] snake = new Label[1000];//蛇的数组
Label egg = new Label();//蛋的标签
Random egg_position = new Random();
private void Form1_Load(object sender, EventArgs e)//load事件
{
snake_show();
}
private void snake_show()//初始化蛇的生成界面
{
Label snake_template = new Label();
snake_template.Size = new Size(100, 100);
snake_template.BackColor = Color.Black;
snake_template.Location = new Point(0, 0);
snake[count] = snake_template;
Controls.Add(snake[count]);
}
private void move_Tick(object sender, EventArgs e)//计时器自动移动
{
if (count > 0)
{
for (int i = count; i > 0; i--)
{
snake[i].Location = snake[i - 1].Location;
}
}
switch (fxp)
{
case 'a': snake[0].Left -= 100; break;
case 'd': snake[0].Left += 100; break;
case 'w': snake[0].Top -= 100; break;
case 's': snake[0].Top += 100; break;
}
eat();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)//接受按键控制方向
{
{
switch (e.KeyChar)
{
case 'a': if (oldfxp != 'd' || count == 0) fxp = 'a'; break;
case 'd': if (oldfxp != 'a' || count == 0) fxp = 'd'; break;
case 'w': if (oldfxp != 's' || count == 0) fxp = 'w'; break;
case 's': if (oldfxp != 'w' || count == 0) fxp = 's'; break;
}
oldfxp = fxp;
}
}
private void egg_show()//蛋的生成
{
int x = egg_position.Next(0, 9);
int y = egg_position.Next(0, 9);
egg.Location = new Point(x * 100, y * 100);
egg.Size = new Size(100, 100);
egg.BackColor = Color.Red;
Controls.Add(egg);
}
private void eat()//吃蛋的过程
{
if (snake[0].Location == egg.Location)
{
++count;
Label snake_template = new Label();
snake_template.Size = new Size(100, 100);
snake_template.BackColor = Color.Black;
snake_template.Location = tail_show(); /*new Point(snake[count - 1].Left - 100, snake[count - 1].Top);*/
snake[count] = snake_template;
Controls.Add(snake[count]);
egg_exist = false;
}
}
private void cheak_Tick(object sender, EventArgs e)//检测
{
if (!egg_exist)//刷蛋
{
egg_show();
egg_exist = true;
}
egg_bug();
snake_bug();
wall_bug();
}
private void egg_bug()//蛋刷到蛇身
{
for (int i = 0; i < count + 1; i++)
{
if (snake[i].Location == egg.Location && i != 0)
{
egg_exist = false;
}
}
}
private void snake_bug()//蛇吃到自己
{
for (int i = 0; i < count + 1; i++)
{
if (snake[i].Location == snake[0].Location && i != 0)
{
move.Enabled = false;
cheak.Enabled = false;
MessageBox.Show("GameOver!");
}
}
}
private void wall_bug()//撞墙
{
if (snake[0].Left < 0 || snake[0].Left > 900 || snake[0].Top < 0 || snake[0].Top > 900)
{
move.Enabled = false;
cheak.Enabled = false;
MessageBox.Show("GameOver!");
}
}
private Point tail_show()//蛇尾的生成
{
if (count == 0|| count == 1)
{
if (snake[0].Top == egg.Top && snake[0].Left - 100 == egg.Left)
return new Point(snake[0].Left + 100, snake[0].Top);
if (snake[0].Top == egg.Top && snake[0].Left + 100 == egg.Left)
return new Point(snake[0].Left - 100, snake[0].Top);
if (snake[0].Left == egg.Left && snake[0].Top - 100 == egg.Top)
return new Point(snake[0].Left, snake[0].Top + 100);
if (snake[0].Left == egg.Left && snake[0].Top + 100 == egg.Top)
return new Point(snake[0].Left, snake[0].Top - 100);
else
return new Point(-200,-200);
}
else
{
if (snake[count-1].Top == egg.Top && snake[count-2].Left - 100 == egg.Left)
return new Point(snake[count-1].Left + 100, snake[count-1].Top);
if (snake[count-1].Top == egg.Top && snake[count - 2].Left + 100 == egg.Left)
return new Point(snake[count-1].Left - 100, snake[count-1].Top);
if (snake[count-1].Left == egg.Left && snake[count - 2].Top - 100 == egg.Top)
return new Point(snake[count-1].Left, snake[count-1].Top + 100);
if (snake[count-1].Left == egg.Left && snake[count - 2].Top + 100 == egg.Top)
return new Point(snake[count-1].Left, snake[count-1].Top - 100);
else
return new Point(-200, -200);
}
}
}
}
以上为全部代码!
感谢阅读!
有具体不懂的可以私聊问我!乐意教学!