C#贪吃蛇代码设计
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 贪吃蛇
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool i;//开关
snake a_snake = new snake(5);//实例化个长度为 5 的蛇
food afood = new food();//实例化一个食物
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (i)//点击了开始 button
{
a_snake.drawsnake(g);//画出蛇
afood.drawfood(g);//画出来食物
}
if (a_snake.deadsnake())//如果蛇死亡事件为真
{
timer1.Enabled = false;//timer 控件停止
if (DialogResult.Yes ==
MessageBox.Show(" 游 戏 结 束 ! ", " 是 否 重 新 开 始 ?",
MessageBoxButtons.YesNo))
//messagebox 消息
{
//点击确定后重新开始游戏
button1.Enabled = true;
a_snake = new snake(5);//初始化蛇
afood = new food();//初始化食物
i = false;//开关为假
g.Clear(pictureBox1.BackColor);//清理 picturebox
}
else