2019年春第二次课程设计实验报告

这是一份2019年春第二次课程设计实验报告,涵盖实验项目名称、功能描述、模块结构介绍、实现界面展示等内容,还给出了代码托管链接https://gitee.com/lcl777/Parlcl/blob/master/xzk.cpp ,并进行了实验总结。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2019年春第二次课程设计实验报告

一.实验项目名称

反弹球

二.实验项目功能描述

反弹球是一个简单的小游戏,目的是训练人的反应能力。只有通过把所有砖块消除完,才能顺利的完成任务。
游戏要求 :
1.实现球速的随机性
2.实现球碰撞到边缘或者砖块自动反弹
3.实现游戏可暂停性
4.游戏结束后才能重新开始

三.项目模块结构介绍

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

// 全局变量
int high,width; // 游戏画面大小
int ball_x,ball_y; // 小球的坐标
int ball_vx,ball_vy; // 小球的速度
int position_x,position_y; // 挡板中心坐标
int ridus;  // 挡板半径大小
int left,right; // 挡板左右位置
int ball_number;  // 反弹小球的次数
int block_x,block_y; // 方块的位置
int score; // 消掉方块的个数

void gotoxy(int x,int y) //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void startup()  // 数据初始化
{
    high = 13;
    width = 17;
    ball_x = 0;
    ball_y = width/2;
    ball_vx = 1;
    ball_vy = 1;
    ridus = 6;
    position_x = high;
    position_y = width/2;
    left = position_y - ridus;
    right = position_y + ridus;
    ball_number = 0;
    block_x = 0;
    block_y = width/2+1; 
    score = 0;
}

void show()  // 显示画面
{
    gotoxy(0,0);    // 光标移动到原点位置,以下重画清屏 
    int i,j;
    for (i=0;i<=high+1;i++)
    {
        for (j=0;j<=width;j++)
        {
            if ((i== ball_x) && (j== ball_y))
                printf("0");  //   输出小球
            else if (j==width)
                printf("|");  //   输出右边框
            else if (i==high+1)
                printf("-");  //   输出下边框
            else if ( (i==high) && (j>left) && (j<right) )  
                printf("*");  //   输出挡板
            else if ((i== block_x) && (j== block_y))
                printf("B");  //   输出方块
            else
                printf(" ");  //   输出空格
        }
        printf("\n");
    }
    printf("反弹小球数:%d\n",ball_number);   
    printf("消掉的方块数:%d\n",score);    
}   

void updateWithoutInput()  // 与用户输入无关的更新
{
    if (ball_x==high-1)
    {
        if ( (ball_y>=left) && (ball_y<=right) )   // 被挡板挡住
        {
            ball_number++;
            printf("\a"); // 响铃
            //ball_y = ball_y + rand()%4-2;
        }
        else    // 没有被挡板挡住
        {
            printf("游戏失败\n");
            system("pause");
            exit(0);
        }       
    }

    if ((ball_x==block_x) && (ball_y==block_y))  // 小球击中方块
    {
        score++;                 // 分数加1
        block_y = rand()%width;  // 产生新的方块
    }

    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;
    
    if ((ball_x==0)||(ball_x==high-1))
        ball_vx = -ball_vx;
    if ((ball_y==0)||(ball_y==width-1))
        ball_vy = -ball_vy;     
    
    Sleep(80);
}

void updateWithInput()  // 与用户输入有关的更新
{   
    char input;
    if(kbhit())  // 判断是否有输入
    {
        input = getch();  // 根据用户的不同输入来移动,不必输入回车
        if (input == 'a')   
        {
            position_y--;  // 位置左移
            left = position_y - ridus;
            right = position_y + ridus;
        }
        if (input == 'd')
        {
            position_y++;  // 位置右移
            left = position_y - ridus;
            right = position_y + ridus;
        }
    }
}

int main()
{
    startup();  // 数据初始化    
    while (1)  //  游戏循环执行
    {
        show();  // 显示画面
        updateWithoutInput();  // 与用户输入无关的更新
        updateWithInput();     // 与用户输入有关的更新
    }
    return 0;
}

1581690-20190531175340376-1899311120.png

四.实现界面展示

1581690-20190531175350715-1692922616.png

五.代码托管链接

https://gitee.com/lcl777/Parlcl/blob/master/xzk.cpp

六.实验总结

很多东西是没有学的,看不懂是什么意思,百度上也看了,不过还是看不明白,是理解不了吧,代码是按书上来打的,有很多细节问题没有注意,导致后面一直出问题,检查了几遍才发现。很多知识点不知道,就希望老师到时候可以讲一下吧。

转载于:https://www.cnblogs.com/lcl777/p/10956704.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值