这次将代码进行模块化,将相应的游戏功能都放在startup()、show()、updateWinthoutInput()、updateWithInput()几个函数中实现。
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
1.利用函数将简单的飞机游戏进行重构,实现飞机的移动,另外本次的输出与上次的输出也不相同。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
}
//显示画面
void show(){
system("cls");
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
//与用户输入无关的更新
void updateWithoutInput(){
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
2.显示新式子弹,定义变量bullet_x,bullet_y记录子弹的位置。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
int bullet_x,bullet_y;//子弹位置
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = 0;
bullet_y = position_y;
}
//显示画面
void show(){
system("cls");
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");//输出飞机
else if((i==bullet_x)&&(j==bullet_y))
printf("|");//输出子弹
else
printf(" ");
}
printf("\n");
}
}
//与用户输入无关的更新
void updateWithoutInput(){
if(bullet_x>-1)
bullet_x--;
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
if(input == ' '){
bullet_x = position_x-1;//子弹初始位置
bullet_y = position_y;
}
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
3.类似于定义飞机一样定义敌机的位置,定义变量enemy_x,enemy_y记录敌机的位置。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
int bullet_x,bullet_y;//子弹位置
int enemy_x,enemy_y;//敌机位置
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = 0;
bullet_y = position_y;
enemy_x = 0;
enemy_y = position_y;
}
//显示画面
void show(){
system("cls");
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");//输出飞机
else if((i==bullet_x)&&(j==bullet_y))
printf("|");//输出子弹
else if((i==enemy_x)&&(j==enemy_y))
printf("+");//输出敌机
else
printf(" ");
}
printf("\n");
}
}
//与用户输入无关的更新
void updateWithoutInput(){
if(bullet_x>-1)
bullet_x--;
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
if(input == ' '){
bullet_x = position_x-1;//子弹初始位置
bullet_y = position_y;
}
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
4.实现敌机自动下落的效果,在updateWithoutInput()函数中设置speed变量来控制敌机向下降落的速度,例子中表示的就是每执行10次敌机移动一次。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
int bullet_x,bullet_y;//子弹位置
int enemy_x,enemy_y;//敌机位置
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = 0;
bullet_y = position_y;
enemy_x = 0;
enemy_y = position_y;
}
//显示画面
void show(){
system("cls");
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");//输出飞机
else if((i==bullet_x)&&(j==bullet_y))
printf("|");//输出子弹
else if((i==enemy_x)&&(j==enemy_y))
printf("+");//输出敌机
else
printf(" ");
}
printf("\n");
}
}
//与用户输入无关的更新
void updateWithoutInput(){
if(bullet_x>-1)
bullet_x--;
//敌机向下移动
static int speed = 0;
if(speed<20)
speed++;
if(speed == 10){
enemy_x++;
speed =0;
}
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
if(input == ' '){
bullet_x = position_x-1;//子弹初始位置
bullet_y = position_y;
}
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
5.设置飞机击中效果,当子弹位置和敌机位置处于一样时就是击中敌机,这时候敌机不显示;增加变量score记录分数,每击中一次敌机则加一分;增加rand()函数产生随机数,即每当敌机被击中后会随机产生另一分飞机,rand()%10表示产生的随机数在0~9。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
int bullet_x,bullet_y;//子弹位置
int enemy_x,enemy_y;//敌机位置
int score;//游戏得分
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = 0;
bullet_y = position_y;
enemy_x = 0;
enemy_y = position_y;
score = 0;
}
//显示画面
void show(){
system("cls");
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");//输出飞机
else if((i==bullet_x)&&(j==bullet_y))
printf("|");//输出子弹
else if((i==enemy_x)&&(j==enemy_y))
printf("+");//输出敌机
else
printf(" ");
}
printf("\n");
}
printf("得分: %d\n",score);
}
//与用户输入无关的更新
void updateWithoutInput(){
if(bullet_x>-1)
bullet_x--;
//击中敌机
if((bullet_x==enemy_x)&&(bullet_y==enemy_y)){
score++;//分数加一
enemy_x =-1;
enemy_y = rand()%width;//产生新的飞机
bullet_x = -2; //子弹无效
}
if(enemy_x>high){//飞机飞出界面
enemy_x = -1;
enemy_y = rand()%width;
}
//敌机向下移动
static int speed = 0;
if(speed<20)
speed++;
if(speed == 10){
enemy_x++;
speed =0;
}
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
if(input == ' '){
bullet_x = position_x-1;//子弹初始位置
bullet_y = position_y;
}
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
6.优化;增加gotoxy()函数,每当光标移动到原点位置,就进行重画,实现类似于清屏函数的功能,解决飞机闪烁的问题。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//全局变量
int position_x,position_y;//飞机位置
int high,width;//游戏界面大小
int bullet_x,bullet_y;//子弹位置
int enemy_x,enemy_y;//敌机位置
int score;//游戏得分
//初始化
void startup(){
high = 20;
width = 30;
position_x = high/2;
position_y = width/2;
bullet_x = 0;
bullet_y = position_y;
enemy_x = 0;
enemy_y = position_y;
score = 0;
}
//消除飞机闪烁
void gotoxy(int x,int y){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
//显示画面
void show(){
gotoxy(0,0);
int i,j;
for(i=0;i<high;i++){
for(j=0;j<width;j++){
if((i==position_x)&&(j==position_y))
printf("*");//输出飞机
else if((i==bullet_x)&&(j==bullet_y))
printf("|");//输出子弹
else if((i==enemy_x)&&(j==enemy_y))
printf("+");//输出敌机
else
printf(" ");
}
printf("\n");
}
printf("得分: %d\n",score);
}
//与用户输入无关的更新
void updateWithoutInput(){
if(bullet_x>-1)
bullet_x--;
//击中敌机
if((bullet_x==enemy_x)&&(bullet_y==enemy_y)){
score++;//分数加一
enemy_x =-1;
enemy_y = rand()%width;//产生新的飞机
bullet_x = -2; //子弹无效
}
if(enemy_x>high){//飞机飞出界面
enemy_x = -1;
enemy_y = rand()%width;
}
//敌机向下移动
static int speed = 0;
if(speed<20)
speed++;
if(speed == 10){
enemy_x++;
speed =0;
}
}
//与用户输入有关的更新
void updateWithInput(){
char input;
if(kbhit()){
input = getch();
if(input == 'a')
position_y--;
if(input == 'd')
position_y++;
if(input == 'w')
position_x--;
if(input == 's')
position_x++;
if(input == ' '){
bullet_x = position_x-1;//子弹初始位置
bullet_y = position_y;
}
}
}
//主函数
int main(){
startup();
while(1){ //游戏循环
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
这样一个简单的飞机游戏就实现了函数封装,当然还可以类似于简单的飞机游戏一样将飞机和敌机设置的复杂一些。