求C#自动玩贪吃蛇的源代码

有的朋友请发到:king_jjj2008@163.com... 有的朋友请发到:king_jjj2008@163.com 展开
 我来答
sq_zhuyi
2009-06-22 · TA获得超过680个赞
知道小有建树答主
回答量:1127
采纳率:0%
帮助的人:1004万
展开全部
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 WindowsApplication1
{
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("GAME OVER", "是否重新开始?", MessageBoxButtons.YesNo))
//messagebox消息
{
//点击确定后重新开始游戏
button1.Enabled = true;
a_snake = new snake(5);//初始化蛇
afood = new food();//初始化食物
i = false;//开关为假
g.Clear(pictureBox1.BackColor);//清理picturebox
}
else
Application.Exit();//关闭程序
}
}
private void button1_Click(object sender, EventArgs e)
{
i = true; //开关为真
afood.F_point = afood.getpoint();//产生一个食物的随机坐标
pictureBox1.Refresh();//刷新picturebox
timer1.Enabled = true;//开启timer控件
timer1.Interval = 100; //时间间隔为0.1秒
button1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Refresh();//刷新picturebox
a_snake.extendsnake();//蛇伸长一节
if (a_snake.headpoint == afood.F_point)
afood.F_point = afood.getpoint();//蛇头坐标与食物相同
//就只伸长
else
a_snake.contractsnake();//没吃到食物就缩短一节
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && a_snake.Way != 2)
{
a_snake.Way = 0;
}
if (e.KeyCode == Keys.D && a_snake.Way != 3)
{
a_snake.Way = 1;
}
if (e.KeyCode == Keys.S && a_snake.Way != 0)
{
a_snake.Way = 2;
}
if (e.KeyCode == Keys.A && a_snake.Way != 1)
{
a_snake.Way = 3;
}
//设置KeyDown事件,用按件控制方向
}
}
public class segment//[一节蛇]类
{
private int number;//私有成员
public int Number//[一节蛇]的编号
{
get
{
return number;
}
set
{
number = value;
}
}
private Point orign;
public Point Orign//[一节蛇]的坐标
{
get
{
return orign;
}
set
{
orign = value;
}
}
public void drawpart(Graphics g)//画[一节蛇]
{
Pen p = new Pen(Color.Red);
g.DrawEllipse(p, orign.X, orign.Y, 10, 10);
//画红色圆圈代表一节蛇
}
}
public class snake//蛇类
{
ArrayList alist;//定义一个先进先出的数据表
public Point headpoint;//蛇头坐标
private int way = 1;//初始方向为右
public int Way
{
get
{
return way;
}
set
{
way = value;
}
}
public snake(int count)//蛇的构造函数
{
segment apart;
Point apoint = new Point(30, 30);//初始位置
alist = new ArrayList(count);//初始化数据表
for (int i = 1; i <= count; i++)
{
apoint.X = apoint.X + 10;
apart = new segment();
apart.Number = i;
apart.Orign = apoint;
alist.Add(apart);//将没节蛇存在表里面
if (i == count)//将最后的一节蛇定为蛇头
headpoint = apoint;
}
}
public void drawsnake(Graphics g)//画蛇
{
for (int i = 0; i < alist.Count; i++)
{
segment seg = (segment)alist[i];
seg.drawpart(g);
}
}
public void extendsnake()//伸长一节蛇
{
segment seg = new segment();
seg.Number = alist.Count + 1;
Point p;
if (way == 0)
p = new Point(headpoint.X, headpoint.Y - 10);
else if (way == 2)
p = new Point(headpoint.X, headpoint.Y + 10);
else if (way == 3)
p = new Point(headpoint.X - 10, headpoint.Y);
else
p = new Point(headpoint.X + 10, headpoint.Y);
seg.Orign = p;
alist.Add(seg);//将新的一节蛇添加到表尾
headpoint = seg.Orign;//重新设蛇头
}
public void contractsnake()//蛇缩短一节
{
alist.Remove(alist[0]);//删除表的第一个元素
}
public bool deadsnake()//射死亡事件
{
if (headpoint.X < 0 || headpoint.Y < 0 || headpoint.X > 350 || headpoint.Y > 270)
//判断是否撞墙了
return true;
for (int i = 0; i < alist.Count - 1; i++)
{
segment seg = (segment)alist[i];
if (seg.Orign == headpoint)//判断是否咬到自己
return true;
}
return false;
}
}
public class food//食物类
{
private Point f_point;
public Point F_point//食物的坐标
{
get
{
return f_point;
}
set
{
f_point = value;
}
}
public void drawfood(Graphics g)//画食物
{
SolidBrush b = new SolidBrush(Color.Blue);
Rectangle rtg = new Rectangle(f_point.X, f_point.Y, 10, 10);
g.FillRectangle(b, rtg);
//实心的蓝色方块
}
public Point getpoint()//获得食物坐标[随机数point]
{
int i = 10;
Random rdm = new Random(System.DateTime.Now.Millisecond + i);
i = rdm.Next(0, 27);
int j = rdm.Next(0, 27);
Point newp = new Point(i * 10, j * 10);
return newp;
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
云易虹
2009-07-06 · 超过11用户采纳过TA的回答
知道答主
回答量:116
采纳率:0%
帮助的人:39.6万
展开全部
我的邮箱是lc520sj@163.com 我发给你
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式