C语言实现打砖块游戏

 更新时间:2022年05月11日 12:35:06   作者:Object_in_java  
这篇文章主要为大家详细介绍了C语言实现打砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现打砖块游戏的具体代码,供大家参考,具体内容如下

代码:

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

int score;
int ball_row, ball_col;
int ball_vv, ball_vh;
int area_height, area_width;
int baffle_col, baffle_row, baffle_size;
int brick_col, brick_row;
bool isLose;

void gotoxy(int x, int y) {
    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 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
    area_height = 20;
    area_width = 40;
    ball_row = area_height / 2;
    ball_col = area_width / 2;
    ball_vv = 1;
    ball_vh = 1;

    baffle_col = area_width / 2;
    baffle_row = area_height - 2;
    baffle_size = 8;
    
    brick_row = 1;
    brick_col = rand() % area_width;

    score = 0;
    isLose = false;
}

void show()
{
    gotoxy(0, 0);
    int i, j;
    //system("cls");
    for (i = 0; i <= area_height; i++)
    {
        for (j = 0; j <= area_width; j++)
        {
            if (i == ball_row && j == ball_col)
            {
                printf("O");
            }
            else if (i == 0 || i == area_height)
                printf("-");
            else if (j == 0 || j == area_width)
                printf("|");
            else if (i == baffle_row && (j >= baffle_col && j <= baffle_col + baffle_size))
                printf("=");
            else if (i == brick_row && j == brick_col)
                printf("#");
            else printf(" ");
        }
        printf("\n");
    }
    printf("score:%d\n", score);
}

void updateWithhoutInput() {
    static int count = 0;

    if(count % 5 == 0){
        ball_col += ball_vh;
        ball_row += ball_vv;
    }
    
    if (ball_col == 0 + 1 || ball_col == area_width - 1)//vh changed
        ball_vh *= -1;
    if (ball_row == 0 + 1 /*|| ball_row == area_height - 1*/)//vv changed
        ball_vv *= -1;
    if (ball_row == baffle_row - 1 && (ball_col >= baffle_col && ball_col < baffle_col + baffle_size))//在baffle上一行就判断碰撞。 
        ball_vv *= -1;
    // 砖块的左,右,下左,下,下右可以检测到碰撞 
    if ((ball_row == brick_row + 1 && (ball_col >= brick_col - 1 && ball_col <= brick_col + 1))||(ball_row == brick_row && (ball_col == brick_col - 1 || ball_col==brick_col+1))) {
        ball_vv *= -1;
        brick_row = -1;
        score += 10;
        brick_row = 1;
        brick_col = rand() % area_width;
    }

    if (ball_row >= area_height)
        isLose = true;
        
    count++;
}

void updateWithInput() {


    char input;
    if (kbhit()) {
        input = getch();
        switch (input)
        {
        case 'a': if (baffle_col > 0 + 1)baffle_col--; break;
        case 'w': if (baffle_row > 0 + 1)baffle_row--; break;
        case 'd': if (baffle_col < area_width - baffle_size - 1)baffle_col++; break;
        case 's': if (baffle_row < area_height - 1)baffle_row++; break;

        default:
            break;
        }
    }
}

int Lost(){
    if(ball_row > area_height)
        return 1;
    return 0;
}

int IsFinish() {//游戏是否结束 
    if (score == 100) {
        system("cls");
        printf("congretulations!!!");
        score = 0;
        _sleep(500);//先暂停在现实符合人性化 
        system("pause");
        return 1;
    }
    else if (Lost() == 1) {
        system("cls");
        printf("you have lost!!!");
        score = 0;
        _sleep(500);
        system("pause");
        return 1;
    }
    return 0;
}


int main()
{
    HideCursor();
    startup();
    while (1)
    {
        show();
        updateWithInput();
        updateWithhoutInput();
        if(IsFinish() == 1){
            startup();
            continue;
        }
    }
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C语言改造通讯录操作详解

    C语言改造通讯录操作详解

    这篇文章主要介绍了C语言文件操作改造通讯录方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-01-01
  • 深入理解C++内联函数

    深入理解C++内联函数

    这篇文章主要为大家介绍了C++内联函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • C语言do关键字的具体使用

    C语言do关键字的具体使用

    本篇文章主要介绍了C语言do关键字的具体使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • C++ STL 序列式容器与配接器的简单使用

    C++ STL 序列式容器与配接器的简单使用

    本文主要介绍了C++ STL 序列式容器与配接器的简单使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06
  • C++实现LeetCode(127.词语阶梯)

    C++实现LeetCode(127.词语阶梯)

    这篇文章主要介绍了C++实现LeetCode(127.词语阶梯),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++虚函数表与类的内存分布深入分析理解

    C++虚函数表与类的内存分布深入分析理解

    对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实现的。简称为V-Table。本文就将详细讲讲虚函数表的原理与使用,需要的可以参考一下
    2022-08-08
  • 最新C/C++中的new和delete的实现过程小结

    最新C/C++中的new和delete的实现过程小结

    这篇文章主要介绍了C/C++中的new和delete的实现过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 解析C++引用

    解析C++引用

    引用是C++引入的新语言特性,是C++常用的一个重要内容之一。在工作中发现,许多人使用它仅仅是想当然,在某些微妙的场合,很容易出错,究其原由,大多因为没有搞清本源。在本篇中将对引用进行详细讨论,希望对大家更好地理解和使用引用起到抛砖引玉的作用
    2021-06-06
  • c语言颜色代码详解

    c语言颜色代码详解

    在本篇文章里小编给大家整理的是关于c语言颜色代码的知识点内容,需要的朋友们可以参考下。
    2020-02-02
  • C++ 模版双向链表的实现详解

    C++ 模版双向链表的实现详解

    本篇文章是对C++中的模版双向链表进行了详细的分析介绍,需要的朋友参考下
    2013-05-05

最新评论