美文网首页
网易c++------课时27--函数封装的飞机游戏

网易c++------课时27--函数封装的飞机游戏

作者: heiqimingren | 来源:发表于2020-10-05 21:40 被阅读0次

https://study.163.com/course/courseLearn.htm?courseId=1004489035#/learn/video?lessonId=1049178460&courseId=1004489035

#include<stdio.h>
#include<stdlib.h>    //清屏命令在这里。
#include <windows.h> //延时10毫秒-sleep,gotoxy函数
#include <iostream>
#include <conio.h>       //getch()----不用按回车,就可以输入字符。    
using namespace std;


//函数外定义全局变量
int high, width;  //定义屏幕尺寸
int ball_x, ball_y;    //小球的坐标。
int ball_vx, ball_vy;  //小球的运动速度
int position_x, position_y;   //挡板的中心坐标
int radius;           //挡板的半径大小
int left1, right1;      //挡板的左右边界
int ball_number;         //记录小球反弹的次数

              

void gotoxy(int x, int y) // 光标移动到intx,inty位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void HideCursor() //
{
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };  //第二个值为0,表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}



void startup()     //数据初始化
{
    high = 18;
    width = 25;

    ball_x = 1;
    ball_y = width/2;     //设置小球的初始坐标,x和y

    ball_vx = 1;
    ball_vy = 1;       //设置小球的运动速度

    position_x = high;
    position_y = width / 2;
    radius = 5;
    left1 = position_y - radius;
    right1 = position_y + radius;
    ball_number = 0;

    HideCursor();       //隐藏光标 
}
void show()     //显示画面
{
    gotoxy(0, 0);       //光标移动到原点位置,以下重置清屏
    
    int i, j;
    //system("cls");   //清除屏幕
    //cout << position_x << "  " << position_y << endl;
    for (i = 0; i < high;i++)    //2个for循环,将飞机,子弹,敌机都现在在屏幕上,通过不停的循环。巧妙的思路。
    {
        for (j = 0; j < width;j++)  
        {
            if ((i==ball_x)&&(j==ball_y))   //当i,j达到目的小球坐标x,y的时候,就输出*
            {
                printf("*");     //输出飞机*
            }
            else if (j==width-1 )   //输出右边界
            {
                printf("|");  
            }
            else if (i == high-1)   //输出下边界
            {
                printf("-");
            }
            else if ((i==high-2)&&(j>=left1)&&(j<=right1))   //输出活动的挡板
            {
                printf("*");
            }

            else
            {
                printf(" ");    //输出空格
            }
            
        }
        printf("\n");  //这个位置不能乱动!否则错位。
    }   
    printf("反弹小球次数:%d\n", ball_number);
}
void UpdateWithoutInput()  //与用户输入无关的更新,例如子弹自动往上飞,敌机自动往下落
{
    if (ball_x==high-2)
    {
        if ((ball_y >= left1) && (ball_y<=right1))
        {
            ball_number++;   //记录一次反弹次数
            ball_vy = -ball_vy;   //反弹一下,方向改变。
        }
        else
        {
            printf("Game Over!");
            exit(0);     //结束程序
        }
    }
    
    
    //根据速度更新小球的位置
    ball_x = ball_x + ball_vx;
    ball_y = ball_y + ball_vy;

    //碰到边界之后,就改变速度的方向,实现反弹效果
    if ((ball_x == 0) || (ball_x==high-2))  //如果小球碰到上边界或者下边界,就转变方向。
    {
        ball_vx = -ball_vx;
    }
    if ((ball_y == 0) || (ball_y==width-1))  //如果小球碰到左边界或者右边界,就转变方向。
    {
        ball_vy = -ball_vy;
    }
    Sleep(50);



}
void UpdateWithInput()  //与用户输入有关的更新。
{
    char input;
    if (kbhit())   //当按键时候执行。
    {
        input = getch();
        if (input=='a')  //左移挡板
        {
            position_y--;
            left1 = position_y - radius;
            right1 = position_y + radius;
        }
        if (input == 'd')  //左移挡板
        {
            position_y++;
            left1 = position_y - radius;
            right1 = position_y + radius;
        }
        

    }
}
int main()
{
    startup();   //数据初始化
    while (1)
    {
        show();      //显示画面
        UpdateWithoutInput();     //与用户输入无关的更新
        UpdateWithInput();        //与用户输入有关的更新。

    }
    return 0;
}


image.png

相关文章

网友评论

      本文标题:网易c++------课时27--函数封装的飞机游戏

      本文链接:https://www.haomeiwen.com/subject/vvqzuktx.html