Draw.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Curriculum_design
{
interface Draw
{
//画蛇
void DrawSnake();
}
}
Food.cs
using System.Drawing;
using System.Threading.Tasks;
namespace Curriculum_design
{
public class Food
{
//食物坐标
private Point foodPoint;
//食物颜色
private Color foodColor = Color.Green;
//食物大小
private Size size = new Size(10, 10);
public Point FoodPoint { get => foodPoint; set => foodPoint = value; }
public Color FoodColor { get => foodColor; set => foodColor = value; }
public Size Size { get => size; set => size = value; }
}
}
Snake.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using tanchishe1;
namespace Curriculum_design
{
class Snake : Draw
{
//蛇身
private Label _body;
//蛇的颜色
private Color _color = Color.SkyBlue;
//移动方向默认向西
private Way way = Way.WEST;
//移动速度
private int speed = 100;
//蛇
private ArrayList snake;
//判断食物是否在蛇身体里
private bool hasFood = false;
//颜色属性
public Color Color { get => _color; set => _color = value; }
//方向属性
public Way SnakeWay
{
set
{
this.way = value;
}
get
{
return way;
}
}
//速度属性
public int Speed { get => speed; set => speed = value; }
public void DrawSnake()
{
//设置身体
snake = new ArrayList();
for (int i = 0; i < 5; i++)
{
_body = new Label();
_body.BackColor = _color;
_body.Size = new Size(10, 10);
_body.BorderStyle = BorderStyle.FixedSingle;
_body.Location = new Point(200 + i * 10, 150);
snake.Add(_body);
}
}
//返回蛇体
public ArrayList GetSnake()
{
return snake;
}
/// <summary>
/// 蛇的移动
/// </summary>
/// <param name="control"></param>
public void Move(Control control)
{
if (!this.hasFood)
{
//Controls.Remove 可以实现一个一个的删除控件
control.Controls.Remove(control.GetChildAtPoint(((Label)snake[snake.Count - 1]).Location));
//删掉末尾的身体
snake.RemoveAt(snake.Count - 1);
}
Label temp = new Label();
CopyBody(temp, (Label)snake[0]);
switch (this.way)
{
//运动方向的判断
case Way.WEST:
{
temp.Left -= 10;
snake.Insert(0, temp);
break;
}
case Way.EAST:
{
temp.Left += 10;
snake.Insert(0, temp);
break;
}
case Way.NORTH:
{
temp.Top -= 10;
snake.Insert(0, temp);
break;
}
case Way.SOUTH:
{
temp.Top += 10;
snake.Insert(0, temp);
break;
}
}
//添加身体
control.Controls.Add((Label)snake[0]);
if (this.hasFood)
{
this.hasFood = false;
}
}
/// <summary>
/// 蛇吃东西
/// </summary>
/// <param name="food">食物</param>
/// <returns></returns>
public bool Eat(Point food)
{
if (((Label)snake[0]).Left == food.X && ((Label)snake[0]).Top == food.Y)
{
//吃到东西
this.hasFood = true;
return true;
}
return false;
}
//copy蛇身
void CopyBody(Label x, Label y)
{
x.Location = y.Location;
x.BackColor = y.BackColor;
x.Size = y.Size;
x.BorderStyle = y.BorderStyle;
}
}
}
initialForm.cs设计
initialForm.cs
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 tanchishe1
{
public partial class initialForm : Form
{
public initialForm()
{
InitializeComponent();
}
//打开游戏窗口
private void StartGame_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
mainForm.Show();
}
//关闭游戏窗口
private void CloseGame_Click(object sender, EventArgs e)
{
MainForm.ActiveForm.Close();
}
}
}
mainForm.cs设计
mainForm.cs
using Curriculum_design;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace tanchishe1
{
public enum Way
{
EAST,
SOUTH,
WEST,
NORTH
}
public partial class MainForm : Form
{
Snake snake = new Snake();
Food foods = new Food();
private Thread game;
private Boolean flag = false;
//创建委托
private delegate void DrawDele();
private DrawDele drawDelegate;
public MainForm()
{
this.KeyPreview = true;
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
//画蛇
snake.DrawSnake();
game = new Thread(new ThreadStart(StartGame));
game.Start();
this.button2.Enabled = false;
this.Focus();
}
private void StartGame()
{
drawDelegate = new DrawDele(PutFood);
this.Invoke(drawDelegate, null);
while (true)
{
//为当前线程指定时间
Thread.Sleep(snake.Speed);
if (this.IsGameOver())
{
MessageBox.Show("GAME OVER");
//获取当前线程的执行状态
if (game.IsAlive)
{
//终止线程
game.Abort();
}
}
if (snake.Eat(foods.FoodPoint))
{
//消除原本食物
drawDelegate = new DrawDele(KillFood);
this.Invoke(drawDelegate, null);
//重新放置食物
drawDelegate = new DrawDele(PutFood);
this.Invoke(drawDelegate, null);
}
drawDelegate = new DrawDele(MoveSnake);
this.Invoke(drawDelegate, null);
}
}
//键盘控制移动方向
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.A)
this.snake.SnakeWay = (this.snake.SnakeWay == Way.EAST) ? Way.EAST : Way.WEST;
else if (e.KeyData == Keys.D)
this.snake.SnakeWay = (this.snake.SnakeWay == Way.WEST) ? Way.WEST : Way.EAST;
else if (e.KeyData == Keys.W)
this.snake.SnakeWay = (this.snake.SnakeWay == Way.SOUTH) ? Way.SOUTH : Way.NORTH;
else if (e.KeyData == Keys.S)
this.snake.SnakeWay = (this.snake.SnakeWay == Way.NORTH) ? Way.NORTH : Way.SOUTH;
}
/// <summary>
/// 放置食物
/// </summary>
private void PutFood()
{
Random rand = new Random();
//返回一个小于指定数的随机数
int x = rand.Next(510);
int y = rand.Next(230);
//食物设置
Label food = new Label();
food.Size = foods.Size;
food.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
food.BackColor = foods.FoodColor;
if (x < 10)
{
x += 10;
}
if (y < 10)
{
y += 10;
}
food.Location = new Point(x % 10 == 0 ? x : x + (10 - x % 10), y % 10 == 0 ? y : y + (10 - y % 10));
foods.FoodPoint = new Point(food.Left, food.Top);
panel1.Controls.Add(food);
}
/// <summary>
/// 清除食物
/// </summary>
private void KillFood()
{
ClearFood(foods.FoodPoint);
}
private void ClearFood(Point food)
{
panel1.Controls.Remove(panel1.GetChildAtPoint(food));
}
/// <summary>
/// 判断游戏是否结束
/// </summary>
/// <returns></returns>
private bool IsGameOver()
{
ArrayList temp = snake.GetSnake();
Label head = (Label)temp[0];
foreach (Label lbl in temp.GetRange(1, temp.Count - 1))
{
//判断是否碰撞身体
if (lbl.Left == head.Left && lbl.Top == head.Top)
{
return true;
}
}
//判断是否碰撞墙壁
if (((Label)snake.GetSnake()[0]).Left == 0 || ((Label)snake.GetSnake()[0]).Left == 520 ||
((Label)snake.GetSnake()[0]).Top == 0 || ((Label)this.snake.GetSnake()[0]).Top == 240)
{
return true;
}
return false;
}
//蛇移动
private void MoveSnake()
{
snake.Move(panel1);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("通过按下 上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到" +
"一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙," +
"不能咬到自己的身体,更不能咬自己的尾巴,达到了1000分,就能过关。");
}
private delegate void SetTextCallback(string text);
//在给textBox1.text赋值的地方调用以下方法即可
//设置颜色
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
snake.Color = Color.Red;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
snake.Color = Color.Green;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
snake.Color = Color.Blue;
}
//暂停游戏
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
flag = true;
game.Suspend();
}
//继续游戏
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
if (flag == true)
{
game.Resume();
}
else
{
MessageBox.Show("游戏未暂停!!!");
}
}
//简单模式
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
snake.Speed = 500;
}
//普通模式
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
snake.Speed = 100;
}
//困难模式
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
snake.Speed = 50;
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}