码迷,mamicode.com
首页 > 编程语言 > 详细

C语言——反弹球游戏(第三阶段

时间:2019-01-18 19:49:55      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:存储   运行   遇到   失败   and   got   oid   canvas   int   

遇到了一些问题,有些地方不能运行

 

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

#define High 15         //游戏画面尺寸
#define Width 20

//全局变量
int ball_x,ball_y;      //小球的坐标
int ball_vx,ball_vy;    //小球的速度
int canvas[High][Width] = {0};  //二维数组存储游戏画布中的对应元素,0为空格,1为小球
int position_x,position_y;
int ridus;
int left,right;

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()          //数据的初始化
{
    ball_x = position_x;
    ball_y = position_y;
    ball_vx = -1;
    ball_vy = 1;
    canvas[ball_x][ball_y] = 1;

    ridus = 5;
    position_x = High-1;
    position_y = Width/2;
    left = position_y-ridus;
    right = position_y+ridus;

    int k,i;

    for(k = left; k <= right; k++)
        canvas[position_x][k] = 2;

    for(k = 0; k < Width; k++)
        for(i = 0; i < High/4; i++)
            canvas[i][k] = 3;
}

void show()         //显示画面
{
    gotoxy(0,0);
    int i,j;

    for(i = 0; i < High; i++)
    {
        for(j = 0; j < Width; j++)
        {
            if(canvas[i][j] == 0)
                printf(" ");
            else if(canvas[i][j] == 1)
                printf("0");
            else if(canvas[i][j] == 2)
                printf("*");
            else if(canvas[i][j] == 3)
                printf("#");
        }
        printf("|\n");
    }

    for(j = 0; j < Width; j++)
        printf("-");
    printf("\n");
}

void updateWithoutInput()       //与用户输入无关的更新
{
    if(ball_x == High-2)
    {
        if((ball_y >= left) && (ball_y <= right))
        {

        }
        else{
            printf("游戏失败\n");
            system("pause");
            exit(0);
        }
    }

    static int speed = 0;

    if(speed < 7)
        speed++;
    if(speed == 7)
    {
        speed = 0;

    canvas[ball_x][ball_y] = 0;

    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;

    canvas[ball_x][ball_y] = 1;

    if(canvas[ball_x-1][ball_y] == 3)
    {
        ball_vx = -ball_vx;
        canvas[ball_x-1][ball_y] = 0;
        printf("\a");
    }

    }
}

void updateWithInput()      //与用户输入有关的更新
{
    char input;

    if(kbhit())
    {
        input = getch();

        if(input == a && left > 0)
        {
            canvas[position_x][right] = 0;
            position_y--;
            left = position_y-ridus;
            right = position_y+ridus;
            canvas[position_x][left] = 2;
        }

        if(input == d && right < Width-1)
        {
            canvas[position_x][left] = 0;
            position_y++;
            left = position_y-ridus;
            right = position_y+ridus;
            canvas[position_x][right] = 2;
        }
    }
}

int main()
{
    startup();
    while(1)
    {
        show();
        updateWithoutInput();
        updateWithInput();
    }

    return 0;
}

 

C语言——反弹球游戏(第三阶段

标签:存储   运行   遇到   失败   and   got   oid   canvas   int   

原文地址:https://www.cnblogs.com/JAYPARK/p/10288976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!