1.该项目无需添加控件,复制代码即可运行;
由于上班无聊想着玩一玩小游戏,无意中看到一个跑酷型页面游戏想着能不能用C#写一个,于是有了这个:
namespace 恐龙快跑
{
public partial class DragonRun : Form
{
public DragonRun()
{
InitializeComponent();
}
/// <summary>
/// 障碍物形态枚举
/// </summary>
enum zhangaiwu
{
gaode,
aide,
gaohekuande,
aihekuande
}
/// <summary>
/// 变量定义
/// </summary>
zhangaiwu zhangai;
int dimian;
bool move_left;
bool move_right;
bool move_jump_up;
bool move_jump_down;
int jump_shizhong = 0;
int busu = 0;
PictureBox box_zhangaiwu1 = new PictureBox();
PictureBox box_zhangaiwu2 = new PictureBox();
Random t = new Random();
Button kaishi = new Button();
PictureBox dragon = new PictureBox();
System.Windows.Forms.Timer time1 = new System.Windows.Forms.Timer();
System.Windows.Forms.Timer time2 = new System.Windows.Forms.Timer();
///可调节参数
int dimiangaodu = 100;//地面高度;
int zhangaiwushudu = 5;//障碍物移动速度;
/// <summary>
/// 页面初始化控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
private void Chushihua()
{
///定时器1
time1.Interval = 10;
time1.Tick += new EventHandler(time1_Tick);
///定时器2
time2.Interval = 10;
time2.Tick += new EventHandler(time2_Tick);
///开始按钮
kaishi.Location = new Point((ClientSize.Width-kaishi.Width)/2,(ClientSize.Height-kaishi.Height)/2);
kaishi.Width = 100;
kaishi.Height = 50;
kaishi.Text = "开始";
kaishi.Name = "kaishi";
kaishi.Click += new EventHandler(kaishi_Click);
this.Controls.Add(kaishi);
///地面位置
dimian = ClientSize.Height - dimiangaodu;
}
/// <summary>
/// 窗体启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
Chushihua();
time1.Start();
}
/// <summary>
/// 实现跳跃的方法
/// </summary>
private void jump_Click()
{
if (jump_shizhong == 0)
{
move_jump_up = true;
}
}
/// <summary>
/// 跳跃子方法,上升过程
/// </summary>
void jump_up()
{
if (move_jump_up == true && jump_shizhong < 35 && move_jump_down == false)
{
if (jump_shizhong < 15)
{
dragon.Top -= 4;
}
else if (jump_shizhong < 25)
{
dragon.Top -= 2;
}
else if (jump_shizhong < 35)
{
dragon.Top -= 1;
}
jump_shizhong += 1;
}
else if (jump_shizhong == 35 && move_jump_up == true)
{
move_jump_up = false;
move_jump_down = true;
}
}
/// <summary>
/// 跳跃子方法,下降过程
/// </summary>
void jump_down()
{
if (move_jump_up == false && jump_shizhong > 0 && move_jump_down == true)
{
if (jump_shizhong > 25)
{
dragon.Top += 1;
}
else if (jump_shizhong > 15)
{
dragon.Top += 2;
}
else if (jump_shizhong > 0)
{
dragon.Top += 4;
}
jump_shizhong -= 1;
}
else if (jump_shizhong == 0 && move_jump_down == true)
{
move_jump_up = false;
move_jump_down = false;
}
}
/// <summary>
/// 实现移动的方法
/// </summary>
private void move()
{
if (move_left == true)
dragon.Left -= 2;
if (move_right == true)
dragon.Left += 2;
}
/// <summary>
/// 键盘控制,包含跳跃和移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void jump(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
jump_Click();
}
if (e.KeyCode == Keys.A)
{
move_left = true;
}
if (e.KeyCode == Keys.D)
{
move_right = true;
}
}
/// <summary>
/// 键盘回弹时取消移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void move(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
move_left = false;
}
if (e.KeyCode == Keys.D)
{
move_right = false;
}
}
/// <summary>
/// 定时器1的执行,循环执行类似时钟信号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void time1_Tick(object sender, EventArgs e)
{
time1.Stop();
move();
jump_up();
jump_down();
time1.Start();
}
/// <summary>
/// 定时器2的执行,循环执行类似时钟信号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void time2_Tick(object sender, EventArgs e)
{
time2.Stop();
zhangaiwuyidong();
chupengpanduan();
time2.Start();
}
/// <summary>
/// 生成障碍物
/// </summary>
void zhangaiwushengcheng()
{
zhangai = (zhangaiwu)t.Next(0, 4);
PictureBox di = new PictureBox();
di.Left = 900;
di.BackColor = Color.Black;
//di.Image =Image.FromFile(@"");
//di.ImageLocation = "https://img-blog.csdnimg.cn/4568da41976847849e29fd4ed9034d66.jpg";
switch (zhangai)
{
case zhangaiwu.aide:
di.Height = 50;
di.Width = 40;
break;
case zhangaiwu.gaode:
di.Height = 60;
di.Width = 40;
break;
case zhangaiwu.aihekuande:
di.Height = 50;
di.Width = 60;
break;
case zhangaiwu.gaohekuande:
di.Height = 60;
di.Width = 60;
break;
}
di.Top = dimian + 50 - di.Height;
if (busu == 1) box_zhangaiwu1 = di;
if (busu == 2) box_zhangaiwu2 = di;
this.Controls.Add(di);
time2.Start();
}
/// <summary>
/// 位移完成后清理障碍物
/// </summary>
void zhangaiwuqingli()
{
if (busu == 2)
{
this.Controls.Remove(box_zhangaiwu1);
}
else if (busu == 1)
{
this.Controls.Remove(box_zhangaiwu2);
}
}
/// <summary>
/// 障碍物移动的方法
/// </summary>
void zhangaiwuyidong()
{
if (busu == 1)
{
if (box_zhangaiwu1.Left > -50)
{
box_zhangaiwu1.Left -= zhangaiwushudu;
}
else
{
busu = 2;
zhangaiwushengcheng();
zhangaiwuqingli();
}
}
if (busu == 2)
{
if (box_zhangaiwu2.Left > -50)
{
box_zhangaiwu2.Left -= zhangaiwushudu;
}
else
{
busu = 1;
zhangaiwushengcheng();
zhangaiwuqingli();
}
}
}
/// <summary>
/// 开始按钮执行的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void kaishi_Click(object sender, EventArgs e)
{
var di = new PictureBox();
di.Width = 1000;
di.Height = 30;
di.BackColor = Color.DarkGoldenrod;
di.Top = dimian + 50;
di.Left = 0;
this.Controls.Add(di);
dragon.Top = dimian;
dragon.Left = 100;
dragon.Width = 30;
dragon.Height = 50;
dragon.BackColor = Color.Red;
//dragon.Image =Image.FromFile(@"");
//dragon.ImageLocation = "https://img-blog.csdnimg.cn/88c0bb277f8b43718a8c59ecb4a8de36.jpg";
dragon.BackgroundImageLayout= ImageLayout.Center;
this.Controls.Add(dragon);
this.Controls.Remove(kaishi);
busu = 1;
zhangaiwushengcheng();
}
/// <summary>
/// 人物触碰到障碍物的判断方法
/// </summary>
void chupengpanduan()
{
if (
(dragon.Bottom > box_zhangaiwu1.Top && busu == 1 &&
(
(dragon.Right > box_zhangaiwu1.Left && dragon.Right < box_zhangaiwu1.Right) ||
(dragon.Left > box_zhangaiwu1.Left && dragon.Left < box_zhangaiwu1.Right)
)
)
||
(dragon.Bottom > box_zhangaiwu2.Top && busu == 2 &&
(
(dragon.Right > box_zhangaiwu2.Left && dragon.Right < box_zhangaiwu2.Right) ||
(dragon.Left > box_zhangaiwu2.Left && dragon.Left < box_zhangaiwu2.Right)
)
)
)
{
shiwang();
}
}
/// <summary>
/// 判断人物触碰到障碍物后执行的方法
/// </summary>
void shiwang()
{
Thread.Sleep(100);
}
}
}
里面基本都有注释,本人英文扣脚,命名都是中文拼音;
另外附上form1窗体的构建代码:
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// DragonRun
//
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(782, 403);
this.KeyPreview = true;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(800, 450);
this.MinimumSize = new System.Drawing.Size(500, 350);
this.Name = "DragonRun";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "恐龙快跑";
this.Load += new System.EventHandler(this.Form1_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.jump);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.move);
this.ResumeLayout(false);
}
#endregion
2.如何运行
1.新建一个C#的winfrom程序,打开From1.cs下的Form1.Designer.cs,将上面的窗体构建代码复制进去,主DragonRun下面那一段;
2.右键窗体查看代码进入代码页,将主程序代码粘贴进去就可以了;
3.需注意代码中使用了Thread类,报错的话需要手动添加using System.Threading;到using引用里;
4.点击运行就可以了,A和D是左右移动,空格是跳跃;