小游戏之小鸡快跑游戏设计
《嵌入式系统导论》课程设计
名称姓名院系班级学号时间
小游戏之小鸡快跑
电气工程系 电子二班
2014年12月19日
目录
设计目的------------------------------------------------3 设计器件------------------------------------------------3 设计步骤------------------------------------------------3 仿真步骤与结果------------------------------------------3
1. 小鸡行走------------------------------------------6
2. 小鸡停止下坠---------------------------------------6
设计程序(部分)-----------------------------------------------7
1. 计时器程序-------------------------------------------------7
2. 操作程序---------------------------------------------------8
3. 行走路径程序-----------------------------------------------9
4. 主程序-----------------------------------------------------12
总结-----------------------------------------------------------15
设计目的:
通过过此次实验,让大家更熟visual studio 软件的使用。同时通过小组讨论的方式,增强大家的沟通、合作能力。学会在遇到问题时与他人协作的能力。
设计器件:
计算机一台 visual studio软件
设计步骤:
1. 确立题目,选择方向 2. 进行编写程序
3. 打开visual studio软件,加载程序 4. 点击运行按钮,进行软件的仿真 5. 根据仿真结果进行调试
仿真步骤与结果:
步骤一
步骤二
步骤三
步骤四
步骤五
小鸡行走:
小鸡停止下坠:
实验程序(部分)
计时器程序
namespace ChickRun {
partial class Form1 {
///
private System.ComponentModel.IContainer components = null ;
///
/// protected override void Dispose(bool disposing) {
if (disposing && (components != null )) {
components.Dispose(); }
base .Dispose(disposing); }
#region Windows
private void InitializeComponent() {
this .components = new System.ComponentModel.Container (); this .pictureBox1 = new System.Windows.Forms.PictureBox (); this .timer1 = new System.Windows.Forms.Timer (this .components);
((System.ComponentModel.ISupportInitialize )(this .pictureBox1)).BeginInit(); this .SuspendLayout(); //
// pictureBox1 //
this .pictureBox1.Dock = System.Windows.Forms.DockStyle .Fill; this .pictureBox1.Location = new System.Drawing.Point (0, 0); this .pictureBox1.Name = "pictureBox1" ;
this .pictureBox1.Size = new System.Drawing.Size (530, 346); this .pictureBox1.TabIndex = 0;
this .pictureBox1.TabStop = false ; this .pictureBox1.MouseClick this .pictureBox1.Paint // // timer1 //
this .timer1.Tick += new System.EventHandler (this .timer1_Tick); // // Form1 //
this .AutoScaleDimensions = new System.Drawing.SizeF (6F, 12F); this .AutoScaleMode = System.Windows.Forms.AutoScaleMode .Font; this .ClientSize = new System.Drawing.Size (530, 346); this .Controls.Add(this .pictureBox1); this .Name = "Form1" ; this .Text = "Form1" ;
this .Load += new System.EventHandler (this .Form1_Load);
this .KeyUp += new System.Windows.Forms.KeyEventHandler (this .Form1_KeyUp); this .KeyDown
+=
new
System.Windows.Forms. KeyEventHandler (this .Form1_KeyDown);
((System.ComponentModel.ISupportInitialize )(this .pictureBox1)).EndInit(); this .ResumeLayout(false );
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Timer timer1; } }
+= +=
new new
System.Windows.Forms. MouseEventHandler (this .pictureBox1_MouseClick); System.Windows.Forms. PaintEventHandler (this .pictureBox1_Paint);
操作程序:
using System;
using System.Collections.Generic; using System.Text; using System.Drawing;
namespace ChickRun
public class Chick {
public float g = 150.0f; public float speedX;
public float speedY = 100.0f; public float dt = 0.1f; public bool IsRuning = false ; public bool IsJumping = false ; public bool IsSpeeding = false ; public bool IsFalling = false ; public Bitmap bmp; public Chick() {
using (Bitmap bmpt = (Bitmap ) Bitmap .FromFile("chick.png" )) {
bmp = bmpt.Clone() as Bitmap ; } }
public void Update() {
if (!IsRuning) return ; if (IsSpeeding)
this .point.X += (int )((speedX *2) * dt+0.9); else
this .point.X += (int )(speedX * dt+0.9); if (IsJumping || IsFalling) {
this .point.Y += (int )(-speedY * dt+0.9); speedY -= (int )(g * dt); } }
public Point point; } }
行走路径程序:
using System;
using System.Collections.Generic; using System.Text; using System.Drawing;
namespace ChickRun {
public class Road {
static Random rand = new Random (); public int ItemLen;
public List landbmp = new List (); public Road(int itemnum, int itemlen) {
item00.imageIndex = 0; item00.type = 1; _roads.Add(item00);
{
{
ang = -20; index = 2; }
{
ang = 0; index = 0; } {
ang = 20; index = 1; }
item.type = 0; item.type = 1; item.imageIndex = index; _roads.Add(item);
if (itemlen
throw new Exception (); this .ItemLen = itemlen;
RoadItem item00 = new RoadItem (0,this .ItemLen); for (int i = 0; i
}
RoadItem item11 = new RoadItem (0, this .ItemLen);
item11.imageIndex = 0;
item11.type = 1;
_roads.Add(item11);
for (int i = 0; i
{
RoadItem item = _roads[i];
if (i == 0)
item.start = new Point (0, 100);
else
item.start = _roads[i - 1].end;
item.end = new Point (item.start.X + item.length, item.start.Y (int )(Math .Tan(item.angle * Math .PI / 180) * item.length+0.9));
}
using (Image bmp1 = Image .FromFile("land0.png" ))
{
landbmp.Add(bmp1.Clone() as Image );
}
using (Image bmp2 = Image .FromFile("land20.png" ))
{
landbmp.Add(bmp2.Clone() as Image );
}
using (Image bmp3 = Image .FromFile("land-20.png" ))
{
landbmp.Add(bmp3.Clone() as Image );
}
}
//public int GetY(int x)
//{
// int i = x / ItemLen;
// int y = 0;
// if (i + 1
// {
// RoadItem item = RoadItems[i];
// float k = (x - item.start.X) / (float)(item.end.X - item.start.X); // y = item.start.Y + (int)k * (item.end.Y - item.start.Y);
// }
// return y;
//}
RoadItems _roads = new RoadItems ();
public RoadItems RoadItems
+
{
get
{
return _roads;
}
}
}
public class RoadItems : List
{
}
public class RoadItem
{
public int angle;
public int length;
public int type;//0为空,1为陆地
public RoadItem(int angle, int length)
{
this .angle = angle;
this .length = length;
}
public Point start;
public Point end;
public int GetY(int x)
{
RoadItem item = this ; // RoadItems[i];
float k = (x - item.start.X) / (float )(item.end.X - item.start.X);
int y = item.start.Y + (int )(k * (item.end.Y - item.start.Y)+0.9);
return y;
}
public int imageIndex = 0;
}
}
主程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ChickRun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Scene sc;
private void Form1_Load(object sender, EventArgs e)
{
sc = new Scene (pictureBox1 .Size);
sc.StateChange += new StateChange (sc_StateChange);
sc.Refresh();
}
void sc_StateChange(int v,float p)
{
if (v == 1)
this .Text=" 跳起" ;
else if (v == 2)
{
jumpNum = 0;
this .Text = " 落到地面" ;
}
else if (v == 3)
this .Text = " 正在下落" ;
else if (v == 4)
this .Text = " 行走" ;
else if (v == 5)
this .Text = " 下坠" ;
else if (v == 6)
this .Text = " 停止下坠game over";
else if (v == 7)
this .Text = " 停止, 到达终点" ;
this .Text+="["+(int )(p*100)+"%]";
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (sc == null )
return ;
e.Graphics.DrawImage(sc.bitmap, new Point (0, 0));
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
timer1.Enabled = !timer1.Enabled;
if (timer1.Enabled)
sc.chick.IsRuning = true ;
}
private void timer1_Tick(object sender, EventArgs e)
{
sc.Refresh();
pictureBox1.Refresh();
}
int jumpNum = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys .Space)
{
if (sc.currentRoadsIndex + 1 == sc.road.RoadItems.Count)
return ;
jumpNum++;
if (jumpNum > 2)
return ;
sc.chick.speedY = 150;
sc.chick.IsJumping = true ;
}
else if (e.KeyData == Keys .S)
{
if (sc.chick.IsRuning)
sc.chick.IsSpeeding = true ;
}
}
//+5+1+a+s+p+x
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys .S)
{
if (sc.chick.IsRuning)
sc.chick.IsSpeeding = false ;
}
}
}
}
总结:
众所周知,ARM 微处理器功能特别的强大,与 MC51 单片机相比,它更适合做中高端数字系统的微处理器,ARM 已经成了现代主流微处理器内核, 目前,绝大多数码产品,如,手机,MP3,MP4,数码相机,他们的系统都嵌 入了 ARM系列内核, ARM 微处理器更新换代很快, 现在的诺基亚 N 系列智 能手机如 N78,N79 已经采用了 ARM11 了,使得运行于塞班操作系统 S60 第 三版的应用程序流畅自如,基于 ARM的强大功能, 我们没有理由不学 ARM,而我们现在学的仅仅是 ARM7 或 LPC2000 系列的一点皮毛,有大把知识需 要自学才能掌握, 兴趣是最好的老师,只要你爱 ARM,ARM 也会爱你的,通过这次实验的设计,我们对 ARM 有了以下的了解:
1,初步了解了 LPC2138 系列的硬件结构;
2,了解了 LPC2138 引脚的大致功能;
3,掌握 GPIO 口;
4,相关寄存器的名称和使用功能;
5,熟悉基于 C 语言的 ARM 初始化程序和功能模块编程;
6,掌握 PWM 的初始化及歌曲简谱节拍和音乐输出实验程序的编写。
但是在完成这次实验的时候,由于对ADS 软件不熟悉和对C 语言掌握不够牢固,我们遇到了很多的困难,有时一个小问题都会让我们抓狂郁闷很久。但正是这种经历,让我们更好地掌握了自学能力,也学会了运用团队的力量,在今后的学习中,我们会扬长避短,争取做得更好