收藏 分销(赏)

C#停车位游戏.docx

上传人:xrp****65 文档编号:6543146 上传时间:2024-12-12 格式:DOCX 页数:27 大小:134.17KB 下载积分:10 金币
下载 相关 举报
C#停车位游戏.docx_第1页
第1页 / 共27页
C#停车位游戏.docx_第2页
第2页 / 共27页


点击查看更多>>
资源描述
郑州轻工业学院 实训报告 实训名称: 停车位游戏 指导教师: 姓名: 学号: 班级: 提交日期: 1. 实训目的 通过开发一款停车场游戏程序,熟练掌握C#编程语言、面向对象程序设计方法和可视化编程技术。 2. 实训题目 使用C#编程语言,开发一款停车场游戏。 3. 功能描述 游戏基本功能描述如下: 1) 停车场有 5 种颜色的汽车和 6 个车位。 2) 每一辆汽车对应颜色的车位。 3) 车位之间有的有通道,有的没有。 4) 最初 5 种颜色的汽车未停在对应颜色的车位。 5) 玩家点击汽车,实现将该汽车沿通道移动到空闲的车位上;当该汽车与空间的车位之间没有通道时,则不移动汽车。 6) 玩家可以点击按钮“自来一次”,重新开始游戏。 7) 当所有的汽车都听到对应颜色的车位上时,游戏成功。 8) 游戏有计分功能,分数点击一次减一。 9) 游戏有下一关,地图难度增加。 10)游戏有当前时间显示。 4. 需求分析 根据功能描述可知,停车场游戏的系统结构图如下所示。 5. 设计说明 根据需求分析可知,车位具有位置、大小和颜色等属性,定义Space类用于描述车位的信息。通道具有起点和终点两个属性,定义 Road类用于描述通道的信息以及通道与车位之间的关系。车具有位置 和颜色等属性,定义Car类用于描述车的信息以及车停留在哪个车位, 并实现车的移动功能。 为了使游戏的运行更易于控制,定义Game类用于启动游戏、控 制游戏和结束游戏。 综上所述,在停车场游戏中,有Space(车位)、Road(通道)、 Car(车)、Game(游戏)和Form1(用户接口)五个类。停车场 游戏的逻辑模型如下图所示。 6. 源代码 Form1.cs源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private Game game; public Form1() { InitializeComponent(); score.Text = Convert.ToString(Convert.ToInt32(score.Text)); game = new Game(); for (int i = 0; i < 5; i++) { string name = "pictureBox" + i.ToString(); PictureBox pBox = (PictureBox)this.Controls.Find(name,false)[0]; pBox.Location = new Point(game.cars[i].center.X - pictureBox0.Width / 2, game.cars[i].center.Y - pictureBox0.Height / 2); pBox.Visible = true; } } private void pictureBox0_Click(object sender, EventArgs e) { PictureBox pBox = (PictureBox)sender; if (game.df > 0) { game.df--; score.Text = game.df.ToString(); } else pictureBox0.Enabled = false; if (game.MoveCar(0, game.cars[0].spaceNum, game.freeSpaceNum)) { pBox.Location = new Point(game.cars[0].center.X - pBox.Width / 2, game.cars[0].center.Y - pBox.Height / 2); if (game.Success()) { MessageBox.Show("您的得分为:" + game.df.ToString(), "恭喜!成功了", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } else { MessageBox.Show("不能移动"); } if (game.df <1) { MessageBox.Show("剩余步数为0,你没法再走了"); } } private void pictureBox1_Click(object sender, EventArgs e) { PictureBox pBox = (PictureBox)sender; if (game.df > 0) { game.df--; score.Text = game.df.ToString(); } else pictureBox1.Enabled = false; if (game.MoveCar(1, game.cars[1].spaceNum, game.freeSpaceNum)) { pBox.Location = new Point(game.cars[1].center.X - pBox.Width / 2, game.cars[1].center.Y - pBox.Height / 2); if (game.Success()) { MessageBox.Show("您的得分为:" + game.df.ToString(), "恭喜!成功了", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } else { MessageBox.Show("不能移动"); } if (game.df <= 0) { MessageBox.Show("剩余步数为0,你没法再走了"); } } private void pictureBox2_Click(object sender, EventArgs e) { PictureBox pBox = (PictureBox)sender; if (game.df > 0) { game.df--; score.Text = game.df.ToString(); } else pictureBox2.Enabled = false; if (game.MoveCar(2, game.cars[2].spaceNum, game.freeSpaceNum)) { pBox.Location = new Point(game.cars[2].center.X - pBox.Width / 2, game.cars[2].center.Y - pBox.Height / 2); if (game.Success()) { MessageBox.Show("您的得分为:" + game.df.ToString(), "恭喜!成功了", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } else { MessageBox.Show("不能移动"); } if (game.df <= 0) { MessageBox.Show("剩余步数为0,你没法再走了"); } } private void pictureBox3_Click(object sender, EventArgs e) { PictureBox pBox = (PictureBox)sender; if (game.df > 0) { game.df--; score.Text = game.df.ToString(); } else pictureBox3.Enabled = false; if (game.MoveCar(3, game.cars[3].spaceNum, game.freeSpaceNum)) { pBox.Location = new Point(game.cars[3].center.X - pBox.Width / 2, game.cars[3].center.Y - pBox.Height / 2); if (game.Success()) { MessageBox.Show("您的得分为:" + game.df.ToString(), "恭喜!成功了", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } else { MessageBox.Show("不能移动"); } if (game.df <= 0) { MessageBox.Show("剩余步数为0,你没法再走了"); pictureBox3.Enabled = false; } } private void pictureBox4_Click(object sender, EventArgs e) { PictureBox pBox = (PictureBox)sender; if (game.df > 0) { game.df--; score.Text = game.df.ToString(); } else pictureBox4.Enabled = false; if (game.MoveCar(4, game.cars[4].spaceNum, game.freeSpaceNum)) { pBox.Location = new Point(game.cars[4].center.X - pBox.Width / 2, game.cars[4].center.Y - pBox.Height / 2); if (game.Success()) { MessageBox.Show("您的得分为:" + game.df.ToString(), "恭喜!成功了", MessageBoxButtons.OK, MessageBoxIcon.Information ); } } else { MessageBox.Show("不能移动"); } if (game.df <= 0) { MessageBox.Show("剩余步数为0,你没法再走了"); pictureBox4.Enabled = false; } } private void buttonStart_Click(object sender, EventArgs e) { game.PaintClear(this.CreateGraphics()); score.Text = Convert.ToString(Convert.ToInt32(100)); game = new Game(); for (int i = 0; i < 5; i++) { string name = "pictureBox" + i.ToString(); PictureBox pBox = (PictureBox)this.Controls.Find(name,false)[0]; pBox.Location = new Point(game.cars[i].center.X - pictureBox0.Width / 2, game.cars[i].center.Y - pictureBox0.Height / 2); pBox.Visible = true; pBox.Enabled = true; } } private void buttonStart_Paint(object sender, PaintEventArgs e) { game.PaintParkingLot(this.CreateGraphics()); } private void score_Click(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("1.单击小车可以移动到其所在圆圈连线对应的圆圈内\n2.将小车移动到对应颜色圆圈内即为获胜\n3.得分高玩得好", "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void buttonNext_Click(object sender, EventArgs e) { game.PaintClear(this.CreateGraphics()); game.Next(); for (int i = 0; i < 5; i++) { string name = "pictureBox" + i.ToString(); PictureBox pBox = (PictureBox)this.Controls.Find(name, false)[0]; pBox.Location = new Point(game.cars[i].center.X - pictureBox0.Width / 2, game.cars[i].center.Y - pictureBox0.Height / 2); pBox.Visible = true; pBox.Enabled = true; } game.PaintParkingLot(this.CreateGraphics()); } private void toolTip1_Popup(object sender, PopupEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { this.toolTip1.SetToolTip(this.label1, "您的剩余步数"); this.toolTip1.SetToolTip(this.score , "您的剩余步数"); this.toolTip1.SetToolTip(this.buttonStart, "开始新的游戏"); this.toolTip1.SetToolTip(this.buttonNext, "直接下一关"); this.toolTip1.SetToolTip(this.help, "获取帮助"); this.toolTip1.SetToolTip(this.time, "时间"); } private void timer1_Tick(object sender, EventArgs e) { DateTime dt = DateTime.Now; time.Text = dt.ToString(); } private void buttonNext_Paint(object sender, PaintEventArgs e) { } private void time_TextChanged(object sender, EventArgs e) { } } } Car.cs源码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace WindowsFormsApplication2 { class Car { public Point center; public int spaceNum; public Car(Point center, int spaceNum) { this.center = center; this.spaceNum = spaceNum; } public void Move(Point center, int spaceNum) { this.center = center; this.spaceNum = spaceNum; } } } Game.cs源码: using System; using System.Collections.Generic; using System.Linq; using System.Drawing; using System.Text; namespace WindowsFormsApplication2 { class Game { public Space[] spaces; public Road[] roads; public Car[] cars; public int freeSpaceNum; public int df = 10; public Game() { spaces = new Space[6]; roads = new Road[9]; cars = new Car[5]; spaces[0] = new Space(new Point(200, 400), Color.Blue); spaces[1] = new Space(new Point(200, 200), Color.Red); spaces[2] = new Space(new Point(500, 100), Color.Yellow); spaces[3] = new Space(new Point(800, 200), Color.Green); spaces[4] = new Space(new Point(800, 400), Color.Pink); spaces[5] = new Space(new Point(500, 500), Color.Black); roads[0] = new Road(0, 1, spaces[0].center, spaces[1].center); roads[1] = new Road(1, 2, spaces[1].center, spaces[2].center); roads[2] = new Road(2, 3, spaces[2].center, spaces[3].center); roads[3] = new Road(3, 4, spaces[3].center, spaces[4].center); roads[4] = new Road(4, 5, spaces[4].center, spaces[5].center); roads[5] = new Road(5, 0, spaces[5].center, spaces[0].center); roads[6] = new Road(0, 3, spaces[0].center, spaces[3].center); roads[7] = new Road(1, 4, spaces[1].center, spaces[4].center); roads[8] = new Road(2, 5, spaces[2].center, spaces[5].center); cars[0] = new Car(spaces[1].center, 1); cars[1] = new Car(spaces[2].center, 2); cars[2] = new Car(spaces[3].center, 3); cars[3] = new Car(spaces[4].center, 4); cars[4] = new Car(spaces[0].center, 0); freeSpaceNum = 5; } public void PaintParkingLot(Graphics g) { foreach (Road r in roads) { r.Paint(g); } foreach (Space s in spaces) { s.Paint(g); } } public void PaintClear(Graphics g) { foreach (Road r in roads) { r.Clear(g); } foreach (Space s in spaces) { s.Clear(g); } } public bool Success() { for (int i = 0; i < 5; i++) { if (i != cars[i].spaceNum) return false; } return true; } public bool MoveCar(int carNum, int start, int end) { foreach (Road r in roads) { if (r.spaceStartNum == start && r.spaceEndNum == end) { cars[carNum].Move(spaces[end].center, end); freeSpaceNum = start; return true; } if (r.spaceStartNum == end && r.spaceEndNum == start) { cars[carNum].Move(spaces[end].center, end); freeSpaceNum = start; return true; } } return false; } public void Next() { spaces = new Space[6]; roads = new Road[9]; cars = new Car[5]; spaces[0] = new Space(new Point(200, 350), Color.Blue); spaces[1] = new Space(new Point(200, 100), Color.Red); spaces[2] = new Space(new Point(500, 100), Color.Yellow); spaces[3] = new Space(new Point(800, 100), Color.Green); spaces[4] = new Space(new Point(800, 350), Color.Pink); spaces[5] = new Space(new Point(500, 500), Color.Black); roads[0] = new Road(0, 1, spaces[0].center, spaces[1].center); roads[1] = new Road(1, 2, spaces[1].center, spaces[2].center); roads[2] = new Road(2, 3, spaces[2].center, spaces[3].center); roads[3] = new Road(3, 4, spaces[3].center, spaces[4].center); roads[4] = new Road(4, 5, spaces[4].center, spaces[5].center); roads[5] = new Road(5, 0, spaces[5].center, spaces[0].center); roads[6] = new Road(0, 3, spaces[0].center, spaces[3].center); roads[7
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 环境建筑 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服