使用C语言编写一个飞机游戏,要求:1.有复杂的飞机图案2.随着积分的增加加快敌机的下落速度3.防止玩家操控飞机飞出边界4.增加按ESC键后游戏暂停的功能
时间: 2024-03-11 15:49:03 浏览: 37
好的,以下是一个简单版本的飞机游戏的C语言代码,满足你的要求:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 80
#define HEIGHT 25
void clearScreen();
void drawPlane(int, int);
void drawEnemy(int, int);
void drawScore(int);
int checkCollision(int, int, int, int, int, int, int, int);
void gameOver(int);
int main() {
int planeX = WIDTH/2, planeY = HEIGHT-2;
int enemyX, enemyY;
int score = 0;
int enemySpeed = 1;
int isPaused = 0;
srand((unsigned int)time(NULL));
while(1) {
clearScreen();
drawPlane(planeX, planeY);
drawScore(score);
if(!isPaused) {
enemyY += enemySpeed;
}
if(enemyY >= HEIGHT-1) {
enemyX = rand() % (WIDTH-2) + 1;
enemyY = 1;
score++;
if(score % 5 == 0) {
enemySpeed++;
}
}
drawEnemy(enemyX, enemyY);
if(checkCollision(planeX, planeY, 3, 1, enemyX, enemyY, 1, 1)) {
gameOver(score);
break;
}
if(kbhit()) {
char c = getch();
if(c == 27) {
isPaused = !isPaused;
} else if(c == 'a' && planeX > 1) {
planeX--;
} else if(c == 'd' && planeX < WIDTH-4) {
planeX++;
} else if(c == 'q') {
break;
}
}
Sleep(25);
}
return 0;
}
void clearScreen() {
system("cls");
}
void drawPlane(int x, int y) {
printf("\033[%d;%dH", y, x);
printf(" /\\ ");
printf("\033[%d;%dH", y+1, x);
printf(" / \\ ");
printf("\033[%d;%dH", y+2, x);
printf("/____\\");
}
void drawEnemy(int x, int y) {
printf("\033[%d;%dH", y, x);
printf(" /\\ ");
printf("\033[%d;%dH", y+1, x);
printf(" / \\ ");
printf("\033[%d;%dH", y+2, x);
printf("/____\\");
}
void drawScore(int score) {
printf("\033[1;1H");
printf("Score: %d", score);
}
int checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
if(x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2) {
return 1;
} else {
return 0;
}
}
void gameOver(int score) {
clearScreen();
printf("\033[%d;%dH", HEIGHT/2, WIDTH/2-4);
printf("Game Over!");
printf("\033[%d;%dH", HEIGHT/2+1, WIDTH/2-4);
printf("Score: %d", score);
while(1) {
if(kbhit()) {
char c = getch();
if(c == 'q') {
break;
}
}
Sleep(25);
}
}
```
这个游戏的实现很简单,使用了常用的控制台输出函数和按键检测函数,其中:
- `clearScreen()` 函数用于清空屏幕
- `drawPlane()` 函数用于绘制玩家的飞机
- `drawEnemy()` 函数用于绘制敌机
- `drawScore()` 函数用于显示得分
- `checkCollision()` 函数用于检测玩家飞机与敌机的碰撞
- `gameOver()` 函数用于显示游戏结束信息并等待玩家按下 `q` 键退出游戏
在 `main()` 函数中,使用了一个无限循环来持续更新游戏状态,并使用 `Sleep()` 函数控制帧率。玩家通过按下 `a` 和 `d` 键操控飞机左右移动,按下 `ESC` 键暂停游戏,按下 `q` 键退出游戏。
游戏中的敌机会随着得分的增加加快下落速度,玩家飞机不能飞出边界,否则会自动弹回边界。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)