停车场游戏的实现:

1.实验目的

掌握面向对象程序设计方法和Windows窗体应用程序设计方法。

2.实验内容

设计并实现一款停车场内车辆排位的游戏。停车场中有六个车位,每个车位的颜色都不一样。停车场中有五辆汽车,每辆汽车的颜色都与某一车位颜色相同,但五辆汽车颜色各不相同。玩家可以操纵汽车从当前车位沿设定的路径移动到空闲的车位。当每一辆车与所停车位颜色一致时,游戏成功。

3.实验要求

程序应具有较好的鲁棒性。
第一,要求使用面向对象程序设计方法进行设计。
第二,游戏操作应简单易行,用户体验良好。
第三,玩家移动车辆时,车辆不能移动到车位以外的位置。
第四,当玩家移动车辆不符合游戏规则时,应提示不能这样移动汽车。
第五,当所有汽车与所在车位颜色都一致时,应提示游戏成功,并能结束游戏。

  1. 创建Game类代码如下:
 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 = 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, Color.Blue);cars[1] = new Car(spaces[2].center, 2, Color.Red);cars[2] = new Car(spaces[3].center, 3, Color.Yellow);cars[3] = new Car(spaces[4].center, 4, Color.Green);cars[4] = new Car(spaces[0].center, 0, Color.Pink);freeSpaceNum = 5;}public void PaintParkingLot(Graphics g){foreach(Road r in roads){r.Paint(g);}foreach (Space s in spaces){s.Paint(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;}}
  1. 创建Car类代码如下:
 class Car{public Point center;public int spaceNum;public Color color;public Car(Point center, int spaceNum, Color color){this.color = color;this.center = center;this.spaceNum = spaceNum;}public void Move(Point center, int spaceNum){this.center = center;this.spaceNum = spaceNum;}}
  1. 创建Space类代码如下:
class Space{public Point center;public const int radius = 50;public Color color;public Space(Point center, Color color){this.center = center;this.color = color;}public void Paint(Graphics g){SolidBrush brush = new SolidBrush(Color.Gray);g.FillRectangle(brush, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);Pen pen = new Pen(color, 10);g.DrawRectangle(pen, center.X - radius, center.Y - radius, 2 * radius, 2 * radius);}}
  1. 创建Road类代码如下:
 class Road{public int spaceStartNum;public int spaceEndNum;Point start;Point end;static Color color = Color.DarkOrchid;public Road(int startNum, int endNum, Point p1, Point p2){spaceStartNum = startNum;spaceEndNum = endNum;start = p1;end = p2;}public void Paint(Graphics g){Pen pen = new Pen(color, 10);g.DrawLine(pen, start, end);}}
  1. 主窗体代码:
 public Form1(){InitializeComponent();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 - pBox.Width / 2, game.cars[i].center.Y - pBox.Height / 2);pBox.Visible = true;}}

在窗体中拉五个pictureBox控件代码分别为:

private void pictureBox0_Click(object sender, EventArgs e){PictureBox pBox = (PictureBox)sender;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("恭喜你!游戏通关了");}}else{MessageBox.Show("不能移动");}}
private void pictureBox1_Click(object sender, EventArgs e){PictureBox pBox = (PictureBox)sender;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("恭喜你!游戏通关了");}}else{MessageBox.Show("不能移动");}}
 private void pictureBox2_Click(object sender, EventArgs e){PictureBox pBox = (PictureBox)sender;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("恭喜你!游戏通关了");}}else{MessageBox.Show("不能移动");}}
 private void pictureBox3_Click(object sender, EventArgs e){PictureBox pBox = (PictureBox)sender;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("恭喜你!游戏通关了");}}else{MessageBox.Show("不能移动");}}
 private void pictureBox4_Click(object sender, EventArgs e){PictureBox pBox = (PictureBox)sender;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("恭喜你!游戏通关了");}}else{MessageBox.Show("不能移动");}}

pictureBox的图片可以自己换,方法如下:

button控件图片:


button控件(再玩一次)的代码如下:

private void button2_Click(object sender, EventArgs e){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;}}

添加事件Form1_Paint如下:
代码如下:

private void Form1_Paint(object sender, PaintEventArgs e){game.PaintParkingLot(this.CreateGraphics());//画笔工具,把线路画出来}

4.实验测试计划

一、Space的生成和显示
1、测试程序是否能在游戏开始时,生成5个车位;
2、测试程序是否能在游戏开始时,显示5个车位。

二、Road的生成和显示
1、测试程序是否能在游戏开始时,生成X个通道,且通道首位分别连接两个车位;
2、测试程序是否能在游戏运行中,显示X个通道。

三、Car的生成、显示和移动
1、测试程序是否能在游戏开始时,生成X辆车,且每个车位都在一个车位内;
2、测试程序是否能在游戏开始时,将车沿着通道移动到空闲的车位上;
3、测试程序是否能在游戏开始时,出现车移动到车位以外的情况。
4、测试程序是否能在所有车都位于相同颜色的车位时,结束游戏。

5.实验测试结果

1、每一辆车都能移动到相邻的空车位;
2、如果没有相邻车位,车不能移动;
3、游戏成功显示游戏成功;
4、实现再来一次功能;

C#实验四停车场游戏的实现相关推荐

  1. 微信小程序实验四 —— 扫雷游戏

    实验小提醒,打开微信小程序模板时,一定要看清楚,要选js模板,不要选ts模板,因为ts中对数据类型检查更严格,同样的代码在ts中可能无法运行! 实验内容: 编写如下扫雷游戏,基本要求如下: (1)方块 ...

  2. 数据结构实验四 约瑟夫生死游戏

    实验四 约瑟夫生死游戏 1.实验目的: 利用线性表解决实际问题. 2.实验环境与设备: 已安装Visual Studio 2010(或其以上版本)集成开发环境的计算机. 3.实验原理: (1)利用线性 ...

  3. python实训总结报告书_20172304 实验四python综合实践报告

    20172304 实验四python综合实践报告 姓名:段志轩 学号:20172304 指导教师:王志强 课程:Python程序设计 实验时间:2020年5月13日至2020年6月14日 实验分析 本 ...

  4. python实训报告pygame_20181218 实验四《Python程序设计》实验报告

    20181218 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1812 姓名: 学号:20181218 实验教师:王志强 ...

  5. python程序设计报告-20192416 实验四《Python程序设计》综合实践报告

    20192416 实验四 <Python程序设计> 综合实践报告 课程:<Python程序设计> 班级:1924 姓名:不愿透露姓名的はんたくさん 学号:20192416 实验 ...

  6. python实验过程心得体会_20192416 实验四《Python程序设计》综合实践报告

    20192416 实验四 <Python程序设计> 综合实践报告 课程:<Python程序设计> 班级:1924 姓名:不愿透露姓名的はんたくさん 学号:20192416 实验 ...

  7. 贪吃蛇程序设计报告python_20192116 2019-2020-2 《Python程序设计》实验四报告

    20192116 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1921 姓名: 饶欢 学号:20192116 实验教师: ...

  8. python课设带报告_20193103陈柏维《Python程序设计》实验四报告

    20193103 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1931 姓名: 陈柏维 学号:20193103 实验教师 ...

  9. python编程成果_20192217 2019-2020-2 《Python程序设计》实验四报告

    20192221 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级:1922班 姓名:程子轩 学号:20192217 实验教师: ...

最新文章

  1. 运行shell脚本时怎么知道jdk路径_Linux中如何查询运行文件的全路径的方法
  2. 对于tnsping的连接超时的功能补充
  3. AndroidCamera开发学习笔记01
  4. 在Docker中体验数据库之MySql
  5. 单步调试时遇到cout和cin会自动跳到ostream文件中的解决方案
  6. 电气论文实现:对大规模用户负荷曲线进行聚类
  7. 条件随机场 python_如何直观地理解条件随机场,并通过PyTorch简单地实现
  8. MySQL 的CASE WHEN 语句
  9. DPDK - Symmetric Receive-side Scaling
  10. 20171205_Matlab求方差,均值,均方差,协方差的函数
  11. 历史类:古希腊与亚历山大帝国
  12. php wamp一键环境包,phpwind本地环境一键安装包Wamp 5.0使用说明
  13. 泛泛而谈:白话分布式一致性与共识算法
  14. rounded-{0 | top | right | bottom | left | circle } 边角半径设置 - bootStrap4常用CSS笔记(2019-05-16 09:38)...
  15. solaris9 x86安装oicq过程,sparc也行
  16. 2023NPDP产品经理认证如何考取?
  17. Windows SubSystem for Linux(WSL)设置默认和设置默认登陆用户
  18. 腾讯光子游戏客户端开发公开课以及实习生面试凉经
  19. python-tkinter(7) 实现各种个样的撩妹鼠标拖尾
  20. JavaScript学习:函数

热门文章

  1. 最新AI绘画矢量笔刷2100款大合集,支持AI2022版本
  2. 硬盘数据丢失有哪些找回方法?一分钟学会硬盘恢复
  3. Ubuntu 20.04 LTS 安装坚果云
  4. 专业硕士没有计算机科学,大学计算机专业没有参加竞赛对考研影响大么
  5. 阿里P9,年薪300万,凭什么?
  6. 深圳市腾讯计算机系统有限公司末日沙城,末日沙城腾讯版
  7. 2.4 BLE Mesh各层帧包格式详解
  8. APP测试面试题汇总(基础篇、进阶篇)
  9. 大连工业大学艺术学院计算机考试,2021年大连工业大学艺术与信息工程学院入学考试,入学指南,开学时间及新生转专业...
  10. Can't bind to 'ngModel' since it isn't a known property of 'pl-select'.