当前位置: 首页 > news >正文

C语言实现射击小游戏

以下是一个简单的C语言射击小游戏的实现示例。这个游戏中,玩家控制一个飞船,敌方飞船会随机出现并向玩家移动。如果玩家的飞船与敌方飞船相撞,玩家就失去一条生命,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define WIDTH 10
#define HEIGHT 5
#define ENEMY_SHIP 'E'
#define PLAYER_SHIP 'S'
#define BULLET '|'
 
char game_field[HEIGHT][WIDTH + 1];
int player_ship_x = WIDTH / 2;
int enemy_ship_x = -1;
int enemy_ship_y = -1;
int bullet_x = -1;
int bullet_y = -1;
int lives = 3;
 
void draw_game_field() {
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (j == player_ship_x && i == bullet_y) {
                printf("%c", BULLET);
            } else if (j == player_ship_x && i == 0) {
                printf("%c", PLAYER_SHIP);
            } else if (j == enemy_ship_x && i == enemy_ship_y) {
                printf("%c", ENEMY_SHIP);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
    printf("Lives: %d\n", lives);
}
 
void move_enemy_ship() {
    if (enemy_ship_x < WIDTH - 1) {
        enemy_ship_x++;
    } else {
        enemy_ship_y++;
        enemy_ship_x = 0;
    }
    if (enemy_ship_y == HEIGHT) {
        enemy_ship_y = 0;
    }
}
 
void move_bullet() {
    if (bullet_x > 0) {
        bullet_x--;
    } else {
        bullet_x = player_ship_x;
        bullet_y = -1;
    }
}
 
void handle_collisions() {
    if (bullet_x == enemy_ship_x && bullet_y == enemy_ship_y) {
        bullet_x = player_ship_x;
        bullet_y = -1;
        enemy_ship_x = -1;
        enemy_ship_y = -1;
        lives--;
    }
}
 
void game_loop() {
    srand(time(0));
    while (lives > 0) {
        draw_game_field();
        move_enemy_ship();
        move_bullet();
        handle_collisions();
        if (enemy_ship_x != -1 && enemy_ship_y != -1) {
            draw_game_field();
            char input = getchar();
            if (input == 'a') {
                if (player_ship_x > 0) {
                    player_ship_x--;
                }
            } else if (input == 'd') {
                if (player_ship_x < WIDTH - 1) {
                    player_ship_x++;
                }
            } else if (input == 'w') {
                bullet_y = player_ship_x;
                bullet_x = player_ship_x;
            }
        }
    }
}
 
int main() {
    game_loop();
    printf("Game Over\n");
    return 0;
}

http://www.lryc.cn/news/322992.html

相关文章:

  • c++11 标准模板(STL)本地化库 - std::islower(std::locale) 检查字符是否被本地环境分类为小写
  • 粘度指数改进剂市场需求增长 为润滑油添加剂细分产品
  • LabVIEW柴油机安保监控系统
  • 实测国内AI大模型问答效果
  • 不得不等待的无奈 -《葡萄成熟时》
  • 【Python】Python中装饰器和魔法方法的区别
  • 【React】创建你的第一个React组件
  • 五分钟搞懂MySQL索引下推
  • 【数据库】SQL如何添加数据
  • ClickHouse01-什么是ClickHouse
  • 使用Docker搭建Nascab
  • Elasticsearch8.x版本Java客户端Elasticsearch Java API 如何并发修改
  • Docker 安装 Skywalking以及UI界面
  • mysql 空间查询 多边形内的点
  • 实际开发中,git版本切换操作
  • 线程池实现“线程复用”的原理
  • [Linux开发工具]——make/Makefile的使用
  • C++中的动态数组vector的基本操作
  • vsc ctrl+. 无效的问题
  • 科大讯飞开放平台-python语音转文字教程
  • 【LeetCode: 433. 最小基因变化 + BFS】
  • Python 安装目录及虚拟环境详解
  • linux sh脚本编写
  • 代码随想录笔记|C++数据结构与算法学习笔记-字符串(二)|28. 实现 strStr()、459.重复的子字符串、KMP算法
  • 【复杂网络建模】——建模工具Matlab入门
  • JVM面试篇
  • openEuler 22.03(华为欧拉)一键安装 Oracle 19C RAC(19.22) 数据库
  • 蓝桥杯刷题记录之数字王国之军训排队
  • Go语言学习Day1:什么是Go?
  • C语言内存函数之 memcmp函数