C#实验四停车场游戏的实现

停车场游戏的实现:

在这里插入图片描述

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、实现再来一次功能;

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值