雷霆战机游戏代码
提示:文章
文章目录
- 前言
- 一、背景
- 二、
- 2.1
- 2.2
- 三、
- 3.1
- 总结
前言
前期疑问:
本文目标:
一、背景
最近
二、
2.1
/** 雷霆战机控制台版* 编译命令:gcc console_airplane.c -o game* 运行命令:./game (Linux/Mac) 或 game.exe (Windows)*/
#include <stdio.h> // 标准输入输出
#include <conio.h> // 控制台输入(Windows特有)
#include <windows.h> // Windows系统调用
#include <stdlib.h> // 随机数、内存分配
#include <time.h> // 时间函数// 游戏区域常量定义
#define WIDTH 40 // 游戏区域宽度
#define HEIGHT 20 // 游戏区域高度
#define BULLET_MAX 100 // 最大子弹数
#define ENEMY_MAX 50 // 最大敌机数// 全局变量
int score = 0; // 玩家得分
int game_over = 0; // 游戏结束标志// 玩家战机结构体
typedef struct {int x, y; // 坐标位置
} Player;// 子弹结构体
typedef struct {int x, y; // 坐标位置int active; // 激活状态(0/1)
} Bullet;// 敌机结构体
typedef struct {int x, y; // 坐标位置int active; // 激活状态(0/1)int type; // 敌机类型(0:V型 1:M型)
} Enemy;/* 初始化游戏数据 */
void initGame(Player *p, Bullet b[], Enemy e[]) {p->x = WIDTH/2; // 玩家初始居中p->y = HEIGHT-2; // 玩家初始位置靠下// 初始化子弹数组for(int i=0; i<BULLET_MAX; i++) b[i].active = 0;// 初始化敌机数组for(int i=0; i<ENEMY_MAX; i++) e[i].active = 0;
}/* 绘制游戏画面 */
void drawGame(Player *p, Bullet b[], Enemy e[]) {system("cls"); // 清屏// 绘制上边界for(int i=0; i<WIDTH+2; i++) printf("#");printf("\n");// 绘制游戏区域for(int y=0; y<HEIGHT; y++) {printf("#"); // 左边界for(int x=0; x<WIDTH; x++) {// 绘制玩家战机if(x==p->x && y==p->y) {printf("A");continue;}int printed = 0;// 绘制子弹for(int i=0; i<BULLET_MAX; i++) {if(b[i].active && b[i].x==x && b[i].y==y) {printf("|");printed=1;break;}}// 绘制敌机if(!printed) {for(int i=0; i<ENEMY_MAX; i++) {if(e[i].active && e[i].x==x && e[i].y==y) {printf("%c", e[i].type?'M':'V');printed=1;break;}}}// 空白位置if(!printed) printf(" ");}printf("#\n"); // 右边界}// 绘制下边界和分数for(int i=0; i<WIDTH+2; i++) printf("#");printf("\nScore: %d\n", score);
}/* 更新游戏状态 */
void updateGame(Player *p, Bullet b[], Enemy e[]) {// 处理键盘输入if(_kbhit()) {switch(_getch()) {case 'a': if(p->x>0) p->x--; break; // 左移case 'd': if(p->x<WIDTH-1) p->x++; break; // 右移case ' ': // 发射子弹for(int i=0; i<BULLET_MAX; i++) {if(!b[i].active) { b[i].x = p->x; b[i].y = p->y-1; b[i].active = 1;break;}}break;case 'q': game_over = 1; break; // 退出游戏}}// 更新子弹位置for(int i=0; i<BULLET_MAX; i++) {if(b[i].active) {b[i].y--; // 子弹向上移动if(b[i].y < 0) b[i].active = 0; // 超出边界}}// 随机生成敌机if(rand()%15 == 0) { // 约1/15概率生成for(int i=0; i<ENEMY_MAX; i++) {if(!e[i].active) {e[i].x = rand()%WIDTH; // 随机X坐标e[i].y = 0; // 从顶部出现e[i].active = 1;e[i].type = rand()%2; // 随机类型break;}}}// 更新敌机位置for(int i=0; i<ENEMY_MAX; i++) {if(e[i].active) {e[i].y++; // 敌机向下移动if(e[i].y >= HEIGHT) e[i].active = 0; // 超出边界// 检测与玩家碰撞if(e[i].x == p->x && e[i].y == p->y) {game_over = 1; // 游戏结束}}}// 检测子弹与敌机碰撞for(int i=0; i<BULLET_MAX; i++) {if(b[i].active) {for(int j=0; j<ENEMY_MAX; j++) {if(e[j].active && b[i].x==e[j].x && b[i].y==e[j].y) {b[i].active = e[j].active = 0; // 双方消失score += e[j].type ? 2 : 1; // 根据类型加分break;}}}}
}/* 主函数 */
int main() {Player player; // 玩家对象Bullet bullets[BULLET_MAX]; // 子弹数组Enemy enemies[ENEMY_MAX]; // 敌机数组srand((unsigned)time(NULL)); // 随机数种子initGame(&player, bullets, enemies); // 初始化// 游戏主循环while(!game_over) {drawGame(&player, bullets, enemies); // 绘制updateGame(&player, bullets, enemies); // 更新Sleep(100); // 控制游戏速度(毫秒)}// 游戏结束显示system("cls");printf("Game Over! Final Score: %d\n", score);system("pause"); // 暂停等待按键return 0;
}
2.2
三、
3.1
总结
未完待续