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 ;
}
}
}
(责任编辑:admin)