我有个C#编的小游戏代码,谁可以把它实现,是windows应用程序

生如夏花去若雪 2011-11-14 02:45:38
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;
}
}
}
直接复制代码到那个代码好像不行,需要添加什么类我不是很清楚,谁可以帮我做下,谢谢了,做好可以把生成的整个文件发我邮箱172867649@qq.com,在此感谢
...全文
180 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
andy1020tc 2011-11-14
  • 打赏
  • 举报
回复
LZ 右键已发 注意查收

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧