飞机躲避障碍物的源代码
时间: 2023-12-10 20:03:48 浏览: 196
好的,这里提供一个简单的飞机躲避障碍物游戏的源代码示例。这个游戏中,玩家需要控制飞机躲避随机生成的障碍物,当飞机碰到障碍物时游戏结束。代码注释中会解释每个函数和变量的用途。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <conio.h>
#include <windows.h>
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
#define OBSTACLE_COUNT 10
typedef struct {
int x, y;
} Point;
typedef struct {
Point position;
int width, height;
} Obstacle;
typedef struct {
Point position;
int lives;
} Player;
void gotoxy(int x, int y) {
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int rand_range(int min, int max) {
return min + rand() % (max - min + 1);
}
void draw_char(char ch, int x, int y) {
gotoxy(x, y);
putchar(ch);
}
void draw_rect(int x, int y, int w, int h) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
draw_char('#', x + j, y + i);
}
}
}
void draw_player(Player player) {
draw_char('^', player.position.x, player.position.y);
}
void draw_obstacle(Obstacle obstacle) {
draw_rect(obstacle.position.x, obstacle.position.y, obstacle.width, obstacle.height);
}
void update_player(Player *player, int dx, int dy) {
player->position.x += dx;
player->position.y += dy;
}
void update_obstacle(Obstacle *obstacle, int dx, int dy) {
obstacle->position.x += dx;
obstacle->position.y += dy;
}
bool check_collision(Player player, Obstacle obstacle) {
if (player.position.x >= obstacle.position.x &&
player.position.x <= obstacle.position.x + obstacle.width &&
player.position.y >= obstacle.position.y &&
player.position.y <= obstacle.position.y + obstacle.height) {
return true;
}
return false;
}
int main() {
srand(time(NULL));
Player player = { { SCREEN_WIDTH / 2, SCREEN_HEIGHT - 1 }, 3 };
Obstacle obstacles[OBSTACLE_COUNT];
for (int i = 0; i < OBSTACLE_COUNT; i++) {
obstacles[i].position.x = rand_range(0, SCREEN_WIDTH - 10);
obstacles[i].position.y = rand_range(0, SCREEN_HEIGHT - 10);
obstacles[i].width = rand_range(5, 10);
obstacles[i].height = rand_range(5, 10);
}
int score = 0;
bool game_over = false;
while (!game_over) {
// 读取输入
if (_kbhit()) {
char ch = _getch();
if (ch == 'a') {
update_player(&player, -1, 0);
} else if (ch == 'd') {
update_player(&player, 1, 0);
}
}
// 更新障碍物位置
for (int i = 0; i < OBSTACLE_COUNT; i++) {
update_obstacle(&obstacles[i], 0, 1);
if (obstacles[i].position.y >= SCREEN_HEIGHT) {
obstacles[i].position.y = 0;
obstacles[i].position.x = rand_range(0, SCREEN_WIDTH - obstacles[i].width);
}
// 检测碰撞
if (check_collision(player, obstacles[i])) {
player.lives--;
if (player.lives == 0) {
game_over = true;
}
score = 0;
break;
}
}
// 更新分数
score++;
// 绘制游戏界面
system("cls");
for (int i = 0; i < OBSTACLE_COUNT; i++) {
draw_obstacle(obstacles[i]);
}
draw_player(player);
printf("Score: %d\nLives: %d\n", score, player.lives);
Sleep(50);
}
// 游戏结束
system("cls");
printf("Game over!\nFinal score: %d\n", score);
return 0;
}
```
这个代码示例中,我们使用了 Windows 平台下的 conio.h 头文件和 Sleep 函数来控制游戏帧率和输入读取。如果你使用的是其他平台,可能需要使用不同的方法来实现同样的功能。同时,这个代码示例中的游戏逻辑和界面都比较简单,你可以根据自己的需求进行修改和扩展。
阅读全文