有点简陋,请指教
#include <stdio.h>
#include <stdlib.h>
#include <string.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 gotoxy(int x,int y) { //光标移动到(x,y)位置
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void HideCursor(){
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
}
void startup() {//数据初始化
high=18;
width=30;
position_x=high/2;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
enemy_x=0;
enemy_y=15;
score=0;
HideCursor();//隐藏光标
}
void show() {//显示画面
int i,j;
gotoxy(0,0);//清屏
for (i=0;i<high;i++) {
for (j=0;j<width;j++) {
if ((iposition_y)) {
printf(""); //输出飞机
} else if ((ibullet_y)) {
printf("|");//输出飞机子弹 ‘|’
} else if ((ienemy_y)) {
printf("@");//输出敌机
} else {
printf(" “); //输出空格
}
}
printf(”\n");
}
printf(“得分:%d \n”,score);
}
void updateWithoutlnput() {//与用户输入无关的更新
if ((bullet_xenemy_y)) {
score++;
enemy_x=0;
enemy_y= rand()%width;
bullet_x=-1;
}
if (bullet_x>-1) {
bullet_x--;
}
static int speed =0;//用于控制飞机下落速度
if (speed<10){
speed++;
}
if (speed==10) {
if (enemy_x>high) {
enemy_x=0;
enemy_y= rand()%width;
} else {
if (speed==10){
enemy_x++;
speed=0;
}
}
}
Sleep(10 );
}
void updateWithlnput() {
char input;//与用户输入有关的更新
if (kbhit()) { //当按键时执行
input=getch();
}
if (input=='s') {
position_x++;
}
if (input=='w') {
position_x--;
}
if (input=='a') {
position_y--;
}
if (input=='d') {
position_y++;
}
if (input==' ') {
bullet_x=position_x-1;
bullet_y=position_y;
}
}
int main(void) {
startup();//数据初始化
while (1) { //游戏循环执行
show();//显示画面
updateWithoutlnput();//与用户无关
updateWithlnput();//与用户有关
}
return 0;
}