C#贪吃蛇
来源:互联网 发布:win10更新windows.old 编辑:程序博客网 时间:2024/05/04 09:54
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Snakes
{
public partial class Form1 : Form
{
public int initialX = 60; //设置X坐标
public int initialY = 60; //设置Y坐标
public Button [] Snake = new Button [500]; //按钮类型的数组
public int initialNum = 3; //初始化有多少个蛇
public int snakeNum = 0; //一共有多少个蛇
public int direction = 2; //1:上 2:右 3:下 4 左
public Button Food = new Button(); //食物
public Random R = new Random(); //随机数
public int Score = 0; //分数
public int live = 100; //血上限
public int x; //设置食物的X坐标
public int y; //设置食物Y坐标
public Button [] barrier = new Button [100];//设置障碍物
public int tollGate = 1; //关卡
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) //加载事件
{
for (int i = 0; i < initialNum; i++)
{
Button B = new Button(); //新建一个Botton
B.Size = new Size(20, 20);
B.BackColor = Color.Violet;
B.Enabled = false;
B.Text = i.ToString();
B.ForeColor = Color.White;
B.Location = new Point(initialX - 20 * i, initialY); //初始化坐标
this.Controls.Add(B); //添加按钮到面板
Snake[i] = B;
snakeNum++;
}
timer1.Enabled = true;
timer1.Interval = 600;
Food.Size = new Size(20, 20);
Food.BackColor = Color.Black;
Food.Text = "F";
Food.Enabled = false;
this.Controls.Add(Food);
Food.Location = new Point(R.Next(30) * 20, R.Next(25) * 20);
label1.Text = "分数" + Score;
label2.Text = "血上限" + live;
tollGate_Method(1);
}
public void tollGate_Method(int parameter) //关卡方法
{
for (int i = 0; i < parameter * 10; i++) //初始化障碍物
{
x = R.Next(30) * 20;
y = R.Next(25) * 20;
Button Barriers = new Button();//新建一个Botton
Barriers.Size = new Size(20, 20);
Barriers.BackColor = Color.Red;
Barriers.Enabled = false;
Barriers.Location = new Point(x, y);
Barriers.Text = i.ToString();
this.Controls.Add(Barriers);
barrier[i] = Barriers;
}
}
private void timer1_Tick(object sender, EventArgs e) //定时器事件
{
for (int i = snakeNum - 1; i > 0; i--) //i = 2
{
Snake[i].Location = Snake[i - 1].Location;
}
switch(direction){
case 1:
Snake[0].Location = new Point(Snake[0].Location.X, Snake[0].Location.Y - 20);
break;
case 2:
Snake[0].Location = new Point(Snake[0].Location.X + 20, Snake[0].Location.Y);
break;
case 3:
Snake[0].Location = new Point(Snake[0].Location.X, Snake[0].Location.Y + 20);
break;
case 4:
Snake[0].Location = new Point(Snake[0].Location.X - 20, Snake[0].Location.Y);
break;
}
if (Snake[0].Location == Food.Location) //如果坐标一样,则再随机生成一个食物
{
x = R.Next(30)*20;
y = R.Next(25)*20;
Food.Location = new Point(x, y);
Button B2 = new Button();
B2.Size = new Size(20, 20);
B2.BackColor = Color.Violet;
B2.Enabled = false;
B2.Text = snakeNum.ToString();
B2.ForeColor = Color.White; //颜色为white
B2.Location = new Point(x, y);
this.Controls.Add(B2);
Snake[snakeNum] = B2;
B2.Location = Snake[snakeNum - 1].Location;
snakeNum++;
Score+=100;
label1.Text = "分数" + Score;
label3.Text = "X" + x;
label4.Text = "Y" +y;
}
for (int i = 0; i < tollGate * 10; i++)
{
if (Snake[0].Location == barrier[i].Location)
{
timer1.Enabled = false;
this.BackColor = Color.DarkMagenta;
MessageBox.Show("死亡");
}
}
if (Food.Location.Y == 0) //如果食物不见了,则再生成一个食物
{
x = R.Next(30) * 20;
y = R.Next(25) * 20;
Food.Location = new Point(x, y);
Button B3 = new Button();
B3.Size = new Size(20, 20);
B3.BackColor = Color.Violet;
B3.Enabled = false;
B3.Text = snakeNum.ToString();
B3.ForeColor = Color.White; //颜色为white
B3.Location = new Point(x, y);
this.Controls.Add(B3);
Snake[snakeNum] = B3;
B3.Location = Snake[snakeNum - 1].Location;
snakeNum++;
Score+= 100;
label1.Text = "分数"+ Score;
label3.Text = "X"+x;
label4.Text = "Y"+y;
}
if (Snake[0].Location.Y< 0)
{
Snake[0].Location = new Point(Snake[0].Location.X, 500);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.Y > 500)
{
Snake[0].Location = new Point(Snake[0].Location.X, -15);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.X < -5)
{
Snake[0].Location = new Point(600, Snake[0].Location.Y);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.X > 600)
{
Snake[0].Location = new Point(-15, Snake[0].Location.Y);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (live == 0)
{
MessageBox.Show("游戏结束!");
Snake[0].Location = new Point(300,250);
timer1.Enabled = false;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
direction = 1;
break;
case Keys.Left:
direction = 4;
break;
case Keys.Right:
direction = 2;
break;
case Keys.Down:
direction = 3;
break;
}
}
private void 暂停游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void 继续游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 600;
}
private void 关ToolStripMenuItem1_Click(object sender, EventArgs e)
{
timer1.Interval = 300;
}
private void 关ToolStripMenuItem2_Click(object sender, EventArgs e)
{
timer1.Interval = 100;
}
private void 关ToolStripMenuItem3_Click(object sender, EventArgs e)
{
timer1.Interval = 80;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
}
private void 障碍物ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(1);
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(3);
}
private void 技术支持信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(2);
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(4);
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(5);
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
MessageBox.Show("确定要退出游戏吗?");
this.Close();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Snakes
{
public partial class Form1 : Form
{
public int initialX = 60; //设置X坐标
public int initialY = 60; //设置Y坐标
public Button [] Snake = new Button [500]; //按钮类型的数组
public int initialNum = 3; //初始化有多少个蛇
public int snakeNum = 0; //一共有多少个蛇
public int direction = 2; //1:上 2:右 3:下 4 左
public Button Food = new Button(); //食物
public Random R = new Random(); //随机数
public int Score = 0; //分数
public int live = 100; //血上限
public int x; //设置食物的X坐标
public int y; //设置食物Y坐标
public Button [] barrier = new Button [100];//设置障碍物
public int tollGate = 1; //关卡
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) //加载事件
{
for (int i = 0; i < initialNum; i++)
{
Button B = new Button(); //新建一个Botton
B.Size = new Size(20, 20);
B.BackColor = Color.Violet;
B.Enabled = false;
B.Text = i.ToString();
B.ForeColor = Color.White;
B.Location = new Point(initialX - 20 * i, initialY); //初始化坐标
this.Controls.Add(B); //添加按钮到面板
Snake[i] = B;
snakeNum++;
}
timer1.Enabled = true;
timer1.Interval = 600;
Food.Size = new Size(20, 20);
Food.BackColor = Color.Black;
Food.Text = "F";
Food.Enabled = false;
this.Controls.Add(Food);
Food.Location = new Point(R.Next(30) * 20, R.Next(25) * 20);
label1.Text = "分数" + Score;
label2.Text = "血上限" + live;
tollGate_Method(1);
}
public void tollGate_Method(int parameter) //关卡方法
{
for (int i = 0; i < parameter * 10; i++) //初始化障碍物
{
x = R.Next(30) * 20;
y = R.Next(25) * 20;
Button Barriers = new Button();//新建一个Botton
Barriers.Size = new Size(20, 20);
Barriers.BackColor = Color.Red;
Barriers.Enabled = false;
Barriers.Location = new Point(x, y);
Barriers.Text = i.ToString();
this.Controls.Add(Barriers);
barrier[i] = Barriers;
}
}
private void timer1_Tick(object sender, EventArgs e) //定时器事件
{
for (int i = snakeNum - 1; i > 0; i--) //i = 2
{
Snake[i].Location = Snake[i - 1].Location;
}
switch(direction){
case 1:
Snake[0].Location = new Point(Snake[0].Location.X, Snake[0].Location.Y - 20);
break;
case 2:
Snake[0].Location = new Point(Snake[0].Location.X + 20, Snake[0].Location.Y);
break;
case 3:
Snake[0].Location = new Point(Snake[0].Location.X, Snake[0].Location.Y + 20);
break;
case 4:
Snake[0].Location = new Point(Snake[0].Location.X - 20, Snake[0].Location.Y);
break;
}
if (Snake[0].Location == Food.Location) //如果坐标一样,则再随机生成一个食物
{
x = R.Next(30)*20;
y = R.Next(25)*20;
Food.Location = new Point(x, y);
Button B2 = new Button();
B2.Size = new Size(20, 20);
B2.BackColor = Color.Violet;
B2.Enabled = false;
B2.Text = snakeNum.ToString();
B2.ForeColor = Color.White; //颜色为white
B2.Location = new Point(x, y);
this.Controls.Add(B2);
Snake[snakeNum] = B2;
B2.Location = Snake[snakeNum - 1].Location;
snakeNum++;
Score+=100;
label1.Text = "分数" + Score;
label3.Text = "X" + x;
label4.Text = "Y" +y;
}
for (int i = 0; i < tollGate * 10; i++)
{
if (Snake[0].Location == barrier[i].Location)
{
timer1.Enabled = false;
this.BackColor = Color.DarkMagenta;
MessageBox.Show("死亡");
}
}
if (Food.Location.Y == 0) //如果食物不见了,则再生成一个食物
{
x = R.Next(30) * 20;
y = R.Next(25) * 20;
Food.Location = new Point(x, y);
Button B3 = new Button();
B3.Size = new Size(20, 20);
B3.BackColor = Color.Violet;
B3.Enabled = false;
B3.Text = snakeNum.ToString();
B3.ForeColor = Color.White; //颜色为white
B3.Location = new Point(x, y);
this.Controls.Add(B3);
Snake[snakeNum] = B3;
B3.Location = Snake[snakeNum - 1].Location;
snakeNum++;
Score+= 100;
label1.Text = "分数"+ Score;
label3.Text = "X"+x;
label4.Text = "Y"+y;
}
if (Snake[0].Location.Y< 0)
{
Snake[0].Location = new Point(Snake[0].Location.X, 500);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.Y > 500)
{
Snake[0].Location = new Point(Snake[0].Location.X, -15);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.X < -5)
{
Snake[0].Location = new Point(600, Snake[0].Location.Y);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (Snake[0].Location.X > 600)
{
Snake[0].Location = new Point(-15, Snake[0].Location.Y);
live = live - 10;
label2.Text = "血上限" + live;
MessageBox.Show("撞墙了!");
}
else if (live == 0)
{
MessageBox.Show("游戏结束!");
Snake[0].Location = new Point(300,250);
timer1.Enabled = false;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.Up:
direction = 1;
break;
case Keys.Left:
direction = 4;
break;
case Keys.Right:
direction = 2;
break;
case Keys.Down:
direction = 3;
break;
}
}
private void 暂停游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void 继续游戏ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 关ToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Interval = 600;
}
private void 关ToolStripMenuItem1_Click(object sender, EventArgs e)
{
timer1.Interval = 300;
}
private void 关ToolStripMenuItem2_Click(object sender, EventArgs e)
{
timer1.Interval = 100;
}
private void 关ToolStripMenuItem3_Click(object sender, EventArgs e)
{
timer1.Interval = 80;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
timer1.Enabled = false;
}
private void 障碍物ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(1);
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(3);
}
private void 技术支持信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(2);
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(4);
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
for (int i = 0; i < tollGate * 10; i++)
{
this.Controls.Remove(barrier[i]);
}
tollGate_Method(5);
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
MessageBox.Show("确定要退出游戏吗?");
this.Close();
}
}
}
- c# 贪吃蛇
- C#贪吃蛇
- C#贪吃蛇
- C#贪吃蛇
- C#贪吃蛇教程
- C#贪吃蛇一
- C#贪吃蛇二
- C#贪吃蛇
- c#贪吃蛇源码
- C#贪吃蛇
- C#中的贪吃蛇
- 贪吃蛇C#实现
- C#贪吃蛇
- C#窗体贪吃蛇
- C#实现贪吃蛇
- c#写的贪吃蛇
- 贪吃蛇的C#代码
- C# 学习--贪吃蛇demo
- java JTable 中设置没列的宽度
- 二叉树的三种遍历的非递归实现
- 开始自己的opencv之旅
- 输入三个数的和及平均值
- Gentoo Linux下查看工作组网络时提示:Nautilus cannot handle "smb" locations.
- C#贪吃蛇
- 计算圆柱体的体积
- STL vector用法介绍
- 在5*5数组中输出最小值
- 单文档程序中动态切换多个窗体
- 求一个数的平方
- 两个数的最大值和最小值*
- 自定义JTabbedPane 标签风格
- MFC 浏览器