#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#define true 1
#define false 0
int BackGroundHeight, BackGroundWidth;
int PlaneX, PlaneY;
int EnemyX, EnemyY;
int IncreasedEnemyY;
int SpeedTimes;//飞机的速度是敌机的多少倍
int Pause;
int BulletX, BulletY;
int Fired;
int Killed;
int atoiMy( char ch );
int SpeedEnemyY( );
int returnEnemyX();
void startup();
void show();
void updateWithInput();
void updateWithoutInput();
void judgeOutput( int x, int y );
void gotoxy( int x, int y );
int main( void ) {
startup();
while( 1 ) {
show();
updateWithInput();
updateWithoutInput();
}
return 0;
}
void startup() {
BackGroundHeight = 10;
BackGroundWidth = 20;
PlaneX = 9;
PlaneY = 9;
EnemyX = returnEnemyX();
EnemyY = 1;
Pause = false;
SpeedTimes = 1;
BulletX = PlaneX;
BulletY = PlaneY;
Fired = false;
Killed = false;
}
void show() {
int i = 0, j = 0;
for( i = 0; i <= BackGroundHeight; i++ ) {
for( j = 0; j <= BackGroundWidth; j++ ) {
judgeOutput( j, i );
}
printf("\n");
}
gotoxy( 0, 0 );
}
void updateWithInput() {
char ch = 0;
if( kbhit() ) {
ch = getch();
switch ( ch ) {
case 'a' : {
if( PlaneX > 1 ) {
PlaneX--;
}
break;
}
case 'd' : {
if( PlaneX < BackGroundWidth-1 ) {
PlaneX++;
}
break;
}
case 'w' : {
if( PlaneY > 1 ) {
PlaneY--;
}
break;
}
case 's' : {
if( PlaneY < BackGroundHeight-1 ) {
PlaneY++;
}
break;
}
case 'p' : {
Pause = !Pause;
break;
}
case ' ' : {
Fired = true;
BulletX = PlaneX;
BulletY = PlaneY;
}
default : {
break;
}
}
if( ch <= '9' && ch >= '1' ) {
SpeedTimes = atoiMy( ch );
}
}
}
void updateWithoutInput() {
if( EnemyY < BackGroundHeight-1 ) {
EnemyY += SpeedEnemyY();
}
else {
EnemyX = returnEnemyX();
EnemyY = 1;
}
BulletY--;
if( BulletY == EnemyY && BulletX == EnemyX ) {
EnemyX = returnEnemyX();
EnemyY = 1;
}
}
void judgeOutput( int x, int y ) {
if( x == PlaneX && y == PlaneY ) {
printf("*");
}
else if( x == 0 || x == BackGroundWidth ) {
printf("|");
}
else if( y == 0 || y == BackGroundHeight ) {
printf("-");
}
else if( (x == EnemyX && y == EnemyY) && !Killed ) {
printf("0");
}
else if( (x == BulletX && y == BulletY) && Fired ) {
printf("|");
}
else {
printf(" ");
}
}
void gotoxy(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int returnEnemyX() {
int tmp = 0, enemyx = 0;
srand(time(NULL));
tmp = rand()%BackGroundWidth;
enemyx = tmp > 0 ? tmp : tmp+1;//There is little chance for 0;
return enemyx;
}
int SpeedEnemyY() {
if( Pause ) {
return 0;
}
static int i = 1;
if( i % SpeedTimes == 0 ) {
i = 1;
return 1;
}
else {
i++;
return 0;
}
}
int atoiMy( char ch ) {
return ch - '0';
}
断更的几篇,会陆续补上。(差两篇)将做游戏学编程里的打飞机游戏实现了一遍然后加了几个功能
最新推荐文章于 2022-09-12 01:04:36 发布