帮我用c++写一个射击游戏
时间: 2024-05-02 08:23:31 浏览: 14
好的,我可以为您提供一些关于射击游戏的基本思路和代码示例。以下是一个简单的射击游戏的 C 代码示例,希望能够帮助您入门:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
typedef struct {
int x;
int y;
} Point;
void clearScreen() {
system("cls");
}
void drawPoint(Point p, char ch) {
printf("%c[%d;%df%c", 0x1B, p.y, p.x, ch);
}
void drawLine(Point start, Point end, char ch) {
if (start.x == end.x) {
int y;
for (y = start.y; y <= end.y; y++) {
drawPoint((Point){ start.x, y }, ch);
}
} else if (start.y == end.y) {
int x;
for (x = start.x; x <= end.x; x++) {
drawPoint((Point){ x, start.y }, ch);
}
} else {
printf("Invalid line.\n");
}
}
void drawRect(Point topLeft, Point bottomRight, char ch) {
drawLine(topLeft, (Point){ bottomRight.x, topLeft.y }, ch);
drawLine(topLeft, (Point){ topLeft.x, bottomRight.y }, ch);
drawLine(bottomRight, (Point){ bottomRight.x, topLeft.y }, ch);
drawLine(bottomRight, (Point){ topLeft.x, bottomRight.y }, ch);
}
int clamp(int value, int min, int max) {
if (value < min) {
return min;
} else if (value > max) {
return max;
} else {
return value;
}
}
void moveCursor(Point p) {
printf("%c[%d;%df", 0x1B, p.y, p.x);
}
Point randomPoint(int left, int top, int right, int bottom) {
return (Point){ rand() % (right - left + 1) + left, rand() % (bottom - top + 1) + top };
}
int main() {
int score = 0;
int lives = 3;
int gameover = 0;
Point playerPos = (Point){ SCREEN_WIDTH / 2, SCREEN_HEIGHT - 2 };
Point enemyPos = randomPoint(1, 1, SCREEN_WIDTH - 2, SCREEN_HEIGHT / 3);
Point bulletPos = (Point){ 0, 0 };
int bulletFired = 0;
srand(time(NULL));
while (!gameover) {
// Input
if (_kbhit()) {
int key = _getch();
if (key == 'a') {
playerPos.x = clamp(playerPos.x - 1, 1, SCREEN_WIDTH - 2);
} else if (key == 'd') {
playerPos.x = clamp(playerPos.x + 1, 1, SCREEN_WIDTH - 2);
} else if (key == 'w' && !bulletFired) {
bulletPos = (Point){ playerPos.x, playerPos.y - 1 };
bulletFired = 1;
}
}
// Update
if (bulletFired) {
bulletPos.y--;
if (bulletPos.y <= 0) {
bulletFired = 0;
} else if (bulletPos.x == enemyPos.x && bulletPos.y == enemyPos.y) {
score++;
enemyPos = randomPoint(1, 1, SCREEN_WIDTH - 2, SCREEN_HEIGHT / 3);
bulletFired = 0;
}
}
enemyPos.y++;
if (enemyPos.y >= SCREEN_HEIGHT - 1) {
lives--;
if (lives == 0) {
gameover = 1;
}
enemyPos = randomPoint(1, 1, SCREEN_WIDTH - 2, SCREEN_HEIGHT / 3);
}
// Draw
clearScreen();
drawRect((Point){ 1, 1 }, (Point){ SCREEN_WIDTH - 2, SCREEN_HEIGHT / 3 }, '*');
drawRect(playerPos, (Point){ playerPos.x + 2, playerPos.y }, '#');
drawPoint(enemyPos, 'X');
if (bulletFired) {
drawPoint(bulletPos, '|');
}
moveCursor((Point){ SCREEN_HEIGHT + 1, 1 });
printf("Score: %d Lives: %d", score, lives);
// Sleep
Sleep(50);
}
clearScreen();
printf("Game Over!\n");
printf("Final Score: %d\n", score);
return 0;
}
```
以上代码使用了一些 ANSI 控制台命令来实现比较好的
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)