• 2019第四次课程设计实验报告


    2019第四次课程设计实验报告:

    一:实验项目名称:

    双人反弹球

    二:实验项目功能描述:

    玩家通过使用英文字母和数字控制球的移动,利用挡板进行计分决定输赢

    三:项目模块结构介绍:

    本实验项目分为5个模块:

    1.显示画面,绘制绿圆和黄色挡板
    设计挡板和小球碰撞以及小球反弹后小圆的坐标
    2.设计小球的大小,速度以及挡板的位置,宽度,高度
    3.
    4.控制小球的移动
    5.游戏结束以及后续处理
    1.设计小球的大小,速度以及挡板的位置,宽度,高度:
    // 全局变量int ball_x,ball_y; // 小球的坐标int ball_vx,ball_vy; // 小球的速度int radius; // 小球的半径int bar1_left,bar1_right,bar1_top,bar1_bottom; // 挡板1的上下左右位置坐标int bar2_left,bar2_right,bar2_top,bar2_bottom; // 挡板2的上下左右位置坐标int bar_height,bar_width; // 挡板的高度、宽度
    2.显示画面,绘制绿圆和黄色挡板:

    void clean()  // 消除画面
    {
        setcolor(BLACK);
        setfillcolor(BLACK);
        fillcircle(ball_x, ball_y, radius);     
        fillcircle(ball_x, ball_y, radius); 
        bar(bar1_left,bar1_top,bar1_right,bar1_bottom); 
        bar(bar2_left,bar2_top,bar2_right,bar2_bottom); 
    }   
    void show()  // 显示画面
    {
        setcolor(GREEN);
        setfillcolor(GREEN);
        fillcircle(ball_x, ball_y, radius); // 绘制绿圆 
        
        setcolor(YELLOW);
        setfillcolor(YELLOW);
        bar(bar1_left,bar1_top,bar1_right,bar1_bottom); // 绘制黄色挡板
        bar(bar2_left,bar2_top,bar2_right,bar2_bottom); 
    
        FlushBatchDraw();
        // 延时
        Sleep(3);   
    }
    

    3.设计挡板和小球碰撞以及小球反弹后小圆的坐标:

    void updateWithoutInput()  // 与用户输入无关的更新{
        // 挡板和小圆碰撞,小圆反弹
        if (ball_x+radius>=bar2_left && ball_y+radius>=bar2_top && ball_y+radius<=bar2_bottom)
            ball_vx = -ball_vx; 
        else if (ball_x-radius<=bar1_right && ball_y+radius>=bar1_top && ball_y+radius<=bar1_bottom)
            ball_vx = -ball_vx; 
        
        // 更新小圆坐标
        ball_x = ball_x + ball_vx;
        ball_y = ball_y + ball_vy;
            
        if ((ball_x<=radius)||(ball_x>=Width-radius))
            ball_vx = -ball_vx;
        if ((ball_y<=radius)||(ball_y>=High-radius))
            ball_vy = -ball_vy; 
    }
    

    4.控制小球的移动:

    void updateWithInput()  // 与用户输入有关的更新{   
            int step = 1;
            if (GetAsyncKeyState(0x57) & 0x8000 )  // w
                bar1_top-=step;
            if ((GetAsyncKeyState(0x53) & 0x8000)) //s
                bar1_top+=step;
            if ((GetAsyncKeyState(VK_UP) & 0x8000))     // 上方向键
                bar2_top-=step;
            if ((GetAsyncKeyState(VK_DOWN) & 0x8000))  // 下方向键
                bar2_top+=step; 
    
            bar1_bottom = bar1_top + bar_height;    
            bar2_bottom = bar2_top + bar_height;
    }
    

    5.游戏结束以及后续处理:

    void gameover()
    {
        EndBatchDraw();
        closegraph();
    }
    int main()
    {
        startup();  // 数据初始化    
        while (1)  //  游戏循环执行
        {
            clean();  // 把之前绘制的内容取消
            updateWithoutInput();  // 与用户输入无关的更新
            updateWithInput();     // 与用户输入有关的更新
            show();  // 显示新画面
        }
        gameover();     // 游戏结束、后续处理
        return 0;
    }
    

    四:实现界面显示:

    五:代码托管链接:
    {https://gitee.com/ali_ma/a_warehouse_of_hemp}
    六:实验总结:
    提出问题:如何将游戏中的小球,挡板用更加有意思的图片代替
    解决方法:通过修改小球以及挡板的设计生成代码可以实现
    实验感想:本次实验是相当有意思的,很有童年感。就是代码比较复杂,但是没有关系,通过不断的学习以及练习,相信自己会越来越优秀。

  • 相关阅读:
    新一篇: 正则表达式使用详解
    C#預處理指令
    [转]SQL Server 2005 Beta 2 TransactSQL 增强功能
    SQL Server 2005之PIVOT/UNPIVOT行列转换
    爬虫入门到放弃系列01:什么是爬虫
    我的程序员之路01:自学Java篇
    Java入门者:如何写出美观的Java代码?
    JedisCluster使用pipeline操作Redis Cluster最详细从0到1实现过程
    IDEA超神之路:安装、运行HelloWorld以及激活到2099年的第一场雪
    软考系统架构师、信息系统项目管理师、系统分析师、系统规划与管理师和网络规划师资料大汇总
  • 原文地址:https://www.cnblogs.com/txtnb/p/11030121.html
Copyright © 2020-2023  润新知