0.目录?
1. 摘要 2. 思路分析 3. 代码实现 ? ? ? ?3.1 头文件 ? ? ? ?3.2 测试文件 ? ? ? ?3.3 模块文件 4. 总结
注:代码已上传github,欢迎大家克隆哦
1. 摘要?
大家是否还记得10年前微信小游戏打飞机引起的朋友圈混战?
大家是否还记得智能手机刚刚普及时熬夜刷分的干劲?
今天,我们就用C语言重温一波当年的峥嵘岁月。
2. 思路分析?
本游戏包含菜单、游戏主体两个部分
游戏主体又包括了飞机/激光的绘制以及靶子的生成/命中/得分两个模块
是较为简洁的
w ------ 上 s ------ 下 a ------ 左 d ------ 右 空格 ------ 发射激光
3. 代码实现??
3.1 头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
#include <time.h>
#define TARGET_RANGE 47
void ShowTarget(int isKilled, int target_x);
void ShowAircraft(int isFired, int* isKilled, int target_x, int* score, int x, int y);
3.2 测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "aircraftwar.h"
void isFired(int x, int y)
{
int i = 0;
int j = 0;
for (j = 0; j < y - 1; j++)
{
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" |");
printf("\n");
}
}
void Game()
{
srand((unsigned)time(NULL));
int i = 0;
int j = 0;
int x = 0;
int y = 10;
int score = 0;
int isKilled = 0;
int SetTarget = 0;
int target_x = 5;
while (1)
{
int isFired = 0;
if (SetTarget % 2 == 0 && isKilled == 1)
{
target_x = rand() % TARGET_RANGE + 3;
isKilled = 0;
SetTarget++;
}
else if (isKilled == 1)
{
SetTarget++;
}
char input = _getch();
system("cls");
printf("==================================================\n");
switch (input)
{
case 'w':
y--;
break;
case 's':
y++;
break;
case 'a':
x--;
break;
case 'd':
x++;
break;
case ' ':
isFired = 1;
break;
default:
break;
}
ShowTarget(isKilled,target_x);
ShowAircraft(isFired, &isKilled, target_x, &score, x, y);
printf("==================================================\n");
printf(" Score:%d\n", score);
if (score == 10)
{
printf("W i n!!!!!!!\n");
printf("Wait 5 seconds......\n");
Sleep(5000);
system("cls");
break;
}
}
}
void Rule()
{
printf("+ ------ target\n");
printf("Destroy 10 target and win the game\n");
printf("w ------ up\ns ------ down\na ------ left\nd ------ right\nspace ------ shoot\n");
}
void menu()
{
printf("======== Aircraft War ========\n");
printf("======================================================\n");
printf("======== 1.play ========\n");
printf("======== 2.rule ========\n");
printf("======== 0.exit ========\n");
printf("======================================================\n");
}
int main()
{
int choose = 0;
do {
menu();
scanf("%d", &choose);
switch (choose)
{
case 1:
Game();
break;
case 2:
Rule();
break;
case 0:
printf("Exit!\n");
break;
default:
printf("Illegal input!\n");
break;
}
} while (choose);
return 0;
}
3.3 模块文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "aircraftwar.h"
void ShowTarget(int isKilled,int target_x)
{
int i = 0;
if (isKilled == 0)
{
for (i = 0; i < target_x; i++)
{
printf(" ");
}
printf("+");
printf("\n");
}
}
void ShowAircraft(int isFired, int* isKilled, int target_x, int* score, int x,int y)
{
int i = 0;
int j = 0;
if (isFired == 0)
{
for (j = 0; j < y; j++)
{
printf("\n");
}
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" *\n");
for (i = 0; i < x; i++)
{
printf(" ");
}
printf("*****\n");
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" * *");
printf("\n");
}
else
{
for (j = 0; j < y; j++)
{
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" |");
printf("\n");
}
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" *\n");
for (i = 0; i < x; i++)
{
printf(" ");
}
printf("*****\n");
for (i = 0; i < x; i++)
{
printf(" ");
}
printf(" * *");
printf("\n");
if (x + 2 == target_x && *isKilled == 0)
{
*isKilled = 1;
(*score)++;
}
}
}
4. 总结?
总结一下,我觉得小游戏最精髓的部分在于,用一次又一次的画面的重新打印来实现的玩家前后左右移动/射击时的动态效果
祝大家玩的开心!
|