用C++写一个打飞机小游戏
时间: 2023-12-27 07:17:03 浏览: 44
下面是一个简单的打飞机小游戏的C代码,使用了Windows API实现图形界面。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 60
#define HEIGHT 30
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point pos;
Point speed;
int show;
} Bullet;
typedef struct {
Point pos;
Point speed;
int show;
} Enemy;
HANDLE hConsole;
void setCursorVisible(int visible) {
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = visible;
SetConsoleCursorInfo(hConsole, &cursorInfo);
}
void setCursorPosition(int x, int y) {
COORD pos = {x, y};
SetConsoleCursorPosition(hConsole, pos);
}
void setColor(int color) {
SetConsoleTextAttribute(hConsole, color);
}
void drawChar(char c, int x, int y, int color) {
setCursorPosition(x, y);
setColor(color);
printf("%c", c);
}
void drawString(const char *str, int x, int y, int color) {
setCursorPosition(x, y);
setColor(color);
printf("%s", str);
}
void clearScreen() {
system("cls");
}
int randomInt(int min, int max) {
return rand() % (max - min + 1) + min;
}
int getDistance(Point p1, Point p2) {
int dx = p1.x - p2.x;
int dy = p1.y - p2.y;
return (int)sqrt(dx * dx + dy * dy);
}
void initBullet(Bullet *bullet, int x, int y, int speed) {
bullet->pos.x = x;
bullet->pos.y = y;
bullet->speed.x = 0;
bullet->speed.y = -speed;
bullet->show = 0;
}
void initEnemy(Enemy *enemy, int x, int y, int speed) {
enemy->pos.x = x;
enemy->pos.y = y;
enemy->speed.x = randomInt(-speed, speed);
enemy->speed.y = randomInt(speed / 2, speed);
enemy->show = 0;
}
void updateBullet(Bullet *bullet) {
bullet->pos.x += bullet->speed.x;
bullet->pos.y += bullet->speed.y;
if (bullet->pos.y < 0) {
bullet->show = 0;
}
}
void updateEnemy(Enemy *enemy) {
enemy->pos.x += enemy->speed.x;
enemy->pos.y += enemy->speed.y;
if (enemy->pos.y >= HEIGHT) {
enemy->show = 0;
}
}
void drawBullet(Bullet *bullet) {
if (bullet->show) {
drawChar('|', bullet->pos.x, bullet->pos.y, FOREGROUND_RED);
}
}
void drawEnemy(Enemy *enemy) {
if (enemy->show) {
drawChar('*', enemy->pos.x, enemy->pos.y, FOREGROUND_GREEN);
}
}
void checkBulletHit(Bullet *bullet, Enemy *enemy) {
if (bullet->show && enemy->show && getDistance(bullet->pos, enemy->pos) <= 1) {
bullet->show = 0;
enemy->show = 0;
}
}
void checkEnemyHit(Enemy *enemy, Point *plane) {
if (enemy->show && getDistance(enemy->pos, *plane) <= 2) {
enemy->show = 0;
setColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
drawString("Game Over", WIDTH / 2 - 4, HEIGHT / 2, 0);
setCursorVisible(1);
exit(0);
}
}
int main() {
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
setCursorVisible(0);
srand((unsigned int)time(NULL));
Point plane = {WIDTH / 2, HEIGHT - 2};
Bullet bullet;
Enemy enemy;
int score = 0;
while (1) {
clearScreen();
drawChar('-', 0, HEIGHT - 1, FOREGROUND_BLUE);
drawChar('-', WIDTH - 1, HEIGHT - 1, FOREGROUND_BLUE);
drawChar('|', plane.x, plane.y, FOREGROUND_RED);
drawString("Score: ", 0, 0, FOREGROUND_BLUE);
printf("%d", score);
if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
plane.x--;
if (plane.x < 1) {
plane.x = 1;
}
}
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
plane.x++;
if (plane.x > WIDTH - 2) {
plane.x = WIDTH - 2;
}
}
if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
if (!bullet.show) {
initBullet(&bullet, plane.x, plane.y - 1, 2);
}
}
updateBullet(&bullet);
updateEnemy(&enemy);
if (!bullet.show) {
initEnemy(&enemy, randomInt(1, WIDTH - 2), 0, 1);
}
drawBullet(&bullet);
drawEnemy(&enemy);
checkBulletHit(&bullet, &enemy);
checkEnemyHit(&enemy, &plane);
if (!enemy.show) {
score++;
}
Sleep(20);
}
return 0;
}
```
这个游戏比较简单,玩家用左右箭头控制飞机移动,空格键发射子弹,击落敌机得分,被敌机撞击游戏结束。你可以自行修改代码来实现更多功能。