c# 小游戏代码

我有更好的答案
推荐于2017-11-28 10:43:19 最佳答案
贪吃蛇
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;
}
}
}

下一百层

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();
}
lift alift = new lift();//梯子实例化
people man = new people();//人物实例化
bool i;//开关
int j =1;//人物移动方向
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (i)
{
alift.drawlift(g);//画梯子
man.drawpeople(g);//画人物
}
if (man.mandead())//人物死亡为真
{
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
if (DialogResult.Yes ==
MessageBox.Show("Game Over", "重新开始游戏?", MessageBoxButtons.YesNo))
{ //重新开始游戏
button1.Enabled = true;
man.Footpoint = new Point(alift.downmost.X + 50, alift.downmost.Y - 20);
}

else
Application.Exit();//退出游戏
}
}
private void button1_Click(object sender, EventArgs e)
{
i = true;//打开开关
pictureBox1.Refresh();//刷新
timer1.Interval = 2;
timer1.Enabled = true;
timer3.Interval = 1;
timer3.Enabled = true;
man.Footpoint = new Point(alift.downmost.X + 50, alift.downmost.Y -20);
//初始化任务的坐标
button1.Enabled = false;//Button1被锁
}
private void timer1_Tick(object sender, EventArgs e)
{
alift.liftrise();//伸梯子
if (alift.downmost.Y <= 260)
alift.addstep();//加梯子
pictureBox1.Refresh();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//用A控制向左,S控制向右
if (e.KeyCode == Keys.A)
{
timer2.Interval = 1;
timer2.Enabled = true;
j = 2;
}
if (e.KeyCode == Keys.S)
{
timer2.Interval = 1;
timer2.Enabled = true;
j = 3;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
//KeyUp事件控制人物左右移动的停止
if (e.KeyCode == Keys.A)
{
timer2.Enabled = false ;
j = 1;
}
if (e.KeyCode == Keys.S)
{
timer2.Enabled = false ;
j = 1;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (j == 2)
man.moveleft();//人物向左移动
else if (j == 3)
man.moveright();//人物向右移动
}
private void timer3_Tick(object sender, EventArgs e)
{
man.movedown();//人物下落
if (alift.manland(man))
{
timer3.Enabled = false;
timer4.Interval = 2;
timer4.Enabled = true;
}
}
private void timer4_Tick(object sender, EventArgs e)
{
man.moverise();//人物随梯子上升
if (alift.manout(man))
{
timer3.Enabled = true;
timer4.Enabled = false;
}
}
}
public class step//台阶类是梯子的一个单位
{
private Point safep;//台阶的坐标
public Point Safep
{
get
{
return safep;
}
set
{
safep = value;
}
}
public void drawstep(Graphics g)//画台阶[实心长方形]
{
SolidBrush b = new SolidBrush(Color.DarkSlateBlue);
Rectangle rect = new Rectangle(safep.X, safep.Y, 100, 10);
g.FillRectangle(b, rect);
}
public int getpointx(int i)//台阶的随机X坐标
{
Random rdm = new Random(System.DateTime.Now.Millisecond+i);
return rdm.Next(0,240);
}
public Point risepoint()//新伸起来的台阶坐标
{
Random rdm = new Random(System.DateTime.Now.Millisecond*123456 );
int x= rdm.Next(0, 240);
Point p = new Point(x, 340);
return p;
}
}
public class lift//梯子类
{
public ArrayList alist = new ArrayList(5);//先进先出表
public Point downmost;//最下面台阶的坐标
public lift()//构造函数
{
step astep;
int x=1,y=10;
for (int i = 0; i < 5; i++)
{
astep = new step();
x = astep.getpointx(x);
astep = new step();
astep.Safep =new Point(x, y);
alist.Add(astep);
y = y + 80;
if (i == 4)
downmost = astep.Safep;
}
}
public void drawlift(Graphics g)//画梯子
{
for (int i=0;i<5;i++)
{
step astep=(step) alist[i];
astep.drawstep (g);
}
}
public void liftrise()//梯子上升
{
//表中的每个Y坐标加1
//并把新的台阶存在表的尾部
for (int i = 0; i < 5; i++)
{
step astep = (step)alist[i];
Point p = new Point(astep.Safep.X, astep.Safep.Y - 1);
astep.Safep = p;
alist.Add(astep);
if (i == 4)
downmost = astep.Safep;
}
for (int i = 0; i < 5; i++)//删除表的前5个数据
{
alist.Remove(alist[i]);
}
}
public void addstep()//伸起来的一节梯子
{
step astep=new step ();
astep.Safep=astep.risepoint();
alist.Add(astep);
alist.Remove(alist[0]);
downmost = astep.Safep; //始终保证最下面的一节为downmost
}
public bool manland(people man)//人物登陆事件
{
step s;
for (int a = 0; a < 5; a++)
{
s = (step)alist[a];
if (Math.Abs( s.Safep.Y -(man.Footpoint.Y+10))<2&&
s.Safep.X <= man.Footpoint.X+10 &&
man.Footpoint.X <= s.Safep.X + 100)
return true;
}
return false;
}
public bool manout(people man)//人物冲出事件
{
step s;

for (int a = 0; a < 5; a++)
{
s = (step)alist[a];
if (Math.Abs(s.Safep.Y - (man.Footpoint.Y + 10)) < 3)
{
if (s.Safep.X-10 > man.Footpoint.X ||
man.Footpoint.X > s.Safep.X + 100)
return true;
}
}
return false;
}
}
public class people//人物类
{
private Point footpoint;//人物的坐标
public Point Footpoint
{
get
{
return footpoint;
}
set
{
footpoint = value;
}
}
public void drawpeople(Graphics g)//画个实心圆代表人物
{
SolidBrush b = new SolidBrush(Color.IndianRed);
Rectangle rect = new Rectangle(footpoint.X, footpoint.Y, 10, 10);
g.FillEllipse(b, rect);
}
public void moveleft()//人物向左移动
{
Point p = new Point(footpoint.X - 2, footpoint.Y);
footpoint = p;
}
public void moveright()//人物向右移动
{
Point p = new Point(footpoint.X + 2, footpoint.Y);
footpoint = p;
}
public void moverise()//人物随梯子上升
{
Point p = new Point(footpoint.X, footpoint.Y-1);
footpoint = p;
}
public void movedown()//人物下落
{
Point p = new Point(footpoint.X, footpoint.Y + 1);
footpoint = p;
}
public bool mandead()//人物死亡
{
if ( footpoint.Y<0 ||footpoint.Y>340)
return true ;
return false ;
}
}
}

发两个玩玩吧。 都测试过可行的

其他回答

啥时候能像你这样写出这样的小程序
王振超happy | 发布于2018-02-16
评论
收起 1条折叠回答

为您推荐:

×

类别

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

说明

0/200

提交
取消