用c语言编写一个射击游戏
时间: 2023-12-07 20:03:46 浏览: 86
好的,我可以给你提供一个基本的射击游戏的框架,你可以在此基础上进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define WIDTH 30
#define HEIGHT 20
#define ENEMY_NUM 5
int score = 0;
typedef struct {
int x;
int y;
} position;
typedef struct {
position pos;
int speed;
} enemy;
typedef struct {
position pos;
int speed;
int isFiring;
} player;
void setCursorPosition(int x, int y) {
COORD position = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void showScore() {
setCursorPosition(WIDTH + 5, 5);
printf("Score: %d", score);
}
void drawPlayer(player *p) {
setCursorPosition(p->pos.x, p->pos.y);
printf(" ^ ");
setCursorPosition(p->pos.x, p->pos.y + 1);
printf(" / \\ ");
setCursorPosition(p->pos.x, p->pos.y + 2);
printf("/ \\");
}
void erasePlayer(player *p) {
setCursorPosition(p->pos.x, p->pos.y);
printf(" ");
setCursorPosition(p->pos.x, p->pos.y + 1);
printf(" ");
setCursorPosition(p->pos.x, p->pos.y + 2);
printf(" ");
}
void movePlayer(player *p) {
if (kbhit()) {
char c = getch();
if (c == 'a' && p->pos.x > 1) {
erasePlayer(p);
p->pos.x -= p->speed;
drawPlayer(p);
}
if (c == 'd' && p->pos.x < WIDTH - 5) {
erasePlayer(p);
p->pos.x += p->speed;
drawPlayer(p);
}
if (c == ' ') {
p->isFiring = 1;
}
}
}
void drawEnemy(enemy *e) {
setCursorPosition(e->pos.x, e->pos.y);
printf(" * ");
setCursorPosition(e->pos.x, e->pos.y + 1);
printf(" * * ");
setCursorPosition(e->pos.x, e->pos.y + 2);
printf("* *");
}
void eraseEnemy(enemy *e) {
setCursorPosition(e->pos.x, e->pos.y);
printf(" ");
setCursorPosition(e->pos.x, e->pos.y + 1);
printf(" ");
setCursorPosition(e->pos.x, e->pos.y + 2);
printf(" ");
}
void moveEnemy(enemy *e) {
eraseEnemy(e);
e->pos.y += e->speed;
if (e->pos.y >= HEIGHT - 3) {
e->pos.y = 0;
e->pos.x = rand() % (WIDTH - 5) + 1;
score++;
showScore();
}
drawEnemy(e);
}
void drawBullet(position *pos) {
setCursorPosition(pos->x, pos->y);
printf("|");
}
void eraseBullet(position *pos) {
setCursorPosition(pos->x, pos->y);
printf(" ");
}
void moveBullet(position *pos) {
eraseBullet(pos);
pos->y--;
drawBullet(pos);
}
int isHit(enemy *e, position *pos) {
return (pos->x >= e->pos.x && pos->x <= e->pos.x + 4 && pos->y == e->pos.y) ? 1 : 0;
}
void resetBullet(player *p, position *pos) {
p->isFiring = 0;
eraseBullet(pos);
}
int main() {
srand(time(NULL));
hideCursor();
player p = {{WIDTH / 2, HEIGHT - 3}, 2, 0};
enemy enemies[ENEMY_NUM];
for (int i = 0; i < ENEMY_NUM; i++) {
enemies[i].pos.x = rand() % (WIDTH - 5) + 1;
enemies[i].pos.y = i * 4;
enemies[i].speed = rand() % 2 + 1;
}
while (1) {
drawPlayer(&p);
movePlayer(&p);
for (int i = 0; i < ENEMY_NUM; i++) {
moveEnemy(&enemies[i]);
if (isHit(&enemies[i], &p.pos)) {
setCursorPosition(WIDTH / 2 - 4, HEIGHT / 2);
printf("Game Over!\n");
system("pause");
return 0;
}
}
if (p.isFiring) {
position bullet = {p.pos.x + 2, p.pos.y - 1};
while (bullet.y > 0) {
moveBullet(&bullet);
for (int i = 0; i < ENEMY_NUM; i++) {
if (isHit(&enemies[i], &bullet)) {
eraseEnemy(&enemies[i]);
enemies[i].pos.x = rand() % (WIDTH - 5) + 1;
enemies[i].pos.y = 0;
enemies[i].speed = rand() % 2 + 1;
score += 10;
showScore();
resetBullet(&p, &bullet);
}
}
Sleep(20);
}
resetBullet(&p, &bullet);
}
Sleep(20);
}
return 0;
}
```
这个游戏的规则是玩家可以左右移动,按空格键发射子弹,击中敌人得分。当敌人到达屏幕底部或者与玩家相撞时,游戏结束。你可以在此基础上添加更多的游戏元素和功能。
阅读全文