美文网首页
1. 模块化的飞机游戏

1. 模块化的飞机游戏

作者: 卡尔书院 | 来源:发表于2020-11-05 12:57 被阅读0次

bug

有时子弹击中后没有效果

代码

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

//全局变量
int height, width;//游戏窗口尺寸
int score;//得分
int miss;//没打中的
int positionX, positionY;//飞机位置
int bulletX, bulletY;//子弹的位置
int enemyX, enemyY;//敌机位置

/**
*   隐藏光标
*/
void hideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };//第二个值为o表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

/**
*   初始化游戏数据
*/
void init() {
    hideCursor();//隐藏光标
    height = 16;//游戏窗口尺寸
    width = 60;
    positionX = width / 2;//飞机位置
    positionY = height * 2 / 3;
    bulletY = -1;//子弹Y坐标
    enemyY = height + 1;//敌机Y坐标, 敌机Y坐标在height时处于被摧毁状态
}

/**
*   光标移动至(x, y)
*/
void cursorGoto(int x, int y) {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
/**
*   显示游戏内容
*/
void show() {
    cursorGoto(0, 0);
    for (size_t i = 0; i < height; i++) {
        for (size_t j = 0; j < width; j++) {
            if (i == positionY && j == positionX) {
                printf("*");
            } else if (i == bulletY && j == bulletX) {
                printf("^");
            } else if (i == enemyY && j == enemyX) {
                printf("$");
            } else {
                printf(" ");
            }
        }
        printf("|\n");
    }
    for (size_t i = 0; i < width; i++) {
        putchar('-');
    }
    printf("|\n     得分:%d     错过:%d", score, miss);
}

/**
*   与输入有关的更新
*/
void updateWithInput() {
    char input;
    if (_kbhit()) {
        input = _getch();
        switch (input) {
        case 'a':positionX = positionX - 1 < 0? width-1: positionX-1;
            break;
        case 'd':positionX = positionX + 1 > width - 1 ? 0 : positionX + 1;
            break;
        case 'w':positionY = positionY - 1 < 0 ? 0 : positionY - 1;
            break;
        case 's':positionY = positionY + 1 > height-1 ? height - 1 : positionY + 1;
            break;
        case ' ':bulletX = positionX;
            bulletY = positionY - 1;
            break;
        default:
            break;
        }
    }
}

/**
*   与输入无关的更新
*/
void updateWithoutInput() {
    static int speed = 0;
    if (bulletY > -1 && bulletY <= positionY) {
        bulletY--;
    }
    if (enemyY < height) {
        if (speed > 5) {
            enemyY++;
            if (enemyY == height - 1) {
                miss++;
            }
            speed = 0;
        } else {
            speed++;
        }
    } else {
        enemyY = 0;
        enemyX = rand() % width;
    }
    if (enemyX == bulletX && enemyY == bulletY) {
        enemyY = height + 1;
        bulletY = height + 3;
        score++;
    }
}

int main() {
    init();//初始化游戏数据
    while (1) {
        show();//显示
        updateWithoutInput();//与输入无关的更新
        updateWithInput();//与输入有关的更新
    }
    return 0;
}

相关文章

  • 1. 模块化的飞机游戏

    bug 有时子弹击中后没有效果 代码

  • Task 6

    飞机大战游戏设计 1. 代码的整体框架 首先设置游戏界面的大小、标题、背景图片、飞机图片(正常、爆炸)、子弹...

  • Java+swing版飞机大战

    Java小游戏实训——飞机大战 一、实验目的 使用Java+swing实现飞机大战小游戏 二、实验要求 1.掌握J...

  • 飞机游戏

    这是一款集合经典飞机、六边形、方块与圈圈消消乐的合集游戏,飞机游戏让您重温经典体会无穷乐趣,六边形、方块与圈圈消消...

  • 飞机游戏

    这是一款经典的飞行射击小游戏、无限版的飞机空战让你体会无穷的乐趣。 【游戏特色】 ※操作简单,容易上手 ※火爆弹幕...

  • es6总结

    1.模块化 /*关于模块化的了解 非模块化有以下缺点: 1.命名冲突 2.文件依赖 前端流行的模块化:AMD re...

  • 面试总结

    北京某游戏公司 1.介绍一下了解的一个项目。 2.组件话和模块化的区别? 组件化是基于重用,减少耦合;模块化是根据...

  • 飞机游戏 - 全民飞机单机游戏大全

    “第一次打开游戏请允许“使用数据”,否则游戏无法正常运行。” 2017最新最好玩的怀旧风格暴风战机!!游戏具有完美...

  • 1.模块化

    模块化发展: 1.命名空间 库名.类别名.方法名: var NameSpace = {}; NameSpace.t...

  • 1.模块化

    一、概述 模块就是在程序集中定义一个实现abpmodule的类。从而可以在任意是ABP框架项目中实现重用性(引用该...

网友评论

      本文标题:1. 模块化的飞机游戏

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