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;
}

相关推荐

  1. C语言实现射击游戏

    2024-03-22 17:16:04       20 阅读
  2. C语言实现飞行游戏

    2024-03-22 17:16:04       23 阅读
  3. C语言实现贪吃蛇游戏

    2024-03-22 17:16:04       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-22 17:16:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-22 17:16:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-22 17:16:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-22 17:16:04       20 阅读

热门阅读

  1. Leetcode 448. 找到所有数组中消失的数字

    2024-03-22 17:16:04       16 阅读
  2. 代码随想录 二叉树—找树左下角的值

    2024-03-22 17:16:04       14 阅读
  3. 拒绝拖延。

    2024-03-22 17:16:04       12 阅读
  4. C语言学习笔记day12

    2024-03-22 17:16:04       15 阅读
  5. 机器人物理交互控制的作用

    2024-03-22 17:16:04       19 阅读
  6. 哪些行为会导致Instagram 封号?如何避免封号?

    2024-03-22 17:16:04       18 阅读
  7. 常用的IDE推荐

    2024-03-22 17:16:04       19 阅读
  8. c++算法学习笔记 (15) 质数

    2024-03-22 17:16:04       14 阅读