c++自制小游戏——上

这是一个打敌人的小游戏
第一个步骤——定义,引用

#include <stdio.h>//printf输出
#include <Windows.h>//光标移动
#include <conio.h>//getch函数
#include <ctime>//获取时间做随机数
//飞机的移动      飞机坐标x  飞机坐标y   敌人坐标x 敌人坐标y 子弹坐标x,y 没用的东东   钱    射击距离 血量          
char feiji_yd, vip; int fj_x = 4, fj_y = 7; int ex = 7, ey = 9; int z_x, z_y, slp = 30, mony, zd = 3, hp[2] = { 0,100 };
void feiji(); void toxy(int, int); void bong(); void shangc(); void yh();
//飞机函数       坐标函数togoxy       子弹函数    商店         扣血

接下来是 feiji函数

void feiji() {
	while (1)
	{
		feiji_yd = _getch();
          yh();//判断是否扣血
		switch (feiji_yd)//控制飞机
		{
		case 'w':
			if ( ((fj_y-1)!=ey|| fj_x!=ex)&&(fj_y!=0))
				fj_y -= 1;
			break;
		case 's':
			if (((fj_y + 1) != ey|| fj_x != ex)&&(fj_y!=26))
				fj_y += 1; 
			break;
		case 'a':
			if (((fj_x - 1) != ex || fj_y != ey)&&(  fj_x != 0))
				fj_x -= 1; 
			break;
		case 'd':
			if ( ((fj_x + 1) != ex || fj_y  != ey) && (fj_x != 64))
				fj_x += 1; 
			break;
		case 'g':
			toxy(fj_x, fj_y); printf("A");
			bong();
			break;
		case'b':
			shangc();
			break;
		} 
		toxy(65, 1);
	printf("mony=%d ", mony);//显示 钱和生命值
	toxy(65, 2);
	printf("hp=%d ", hp[1]);
		Sleep(slp);
		toxy(fj_x, fj_y); printf("A");//删除前一个打印的飞机
		if (feiji_yd == 'a') { toxy(fj_x + 1, fj_y); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 'd') { toxy(fj_x - 1, fj_y); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 'w') { toxy(fj_x, fj_y + 1); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 's') { toxy(fj_x, fj_y - 1); printf(" "); toxy(fj_x, fj_y);}
		toxy(ex - 1, ey); printf("!"); toxy(ex + 1, ey); printf("!"); 
	}
}

印一个消一个
接着是移动光标的函数,(T ω T)

void toxy(int x, int y) {
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 移动 
	SetConsoleCursorPosition(hOut, pos);//
}

子弹移动的函数┗( ▔, ▔ )┛

void bong() {
	for ( z_y = fj_y-1; z_y >=((fj_y<zd)?0:(fj_y-zd)); z_y--) {//子弹发射 及射击距离
		z_x = fj_x;
		
        toxy(z_x, z_y); printf("|");
		Sleep(20);
		if ( z_y == 0) {
			toxy(z_x, z_y); printf(" ");
		}
		if(z_y!=fj_y-1)
		toxy(z_x, (z_y + 1)); printf(" ");
		 if (z_y == (fj_y-zd)) {
			toxy(z_x, z_y);
			printf(" ");
		}
		
		if ((z_x == ex||z_x+1==ex||z_x-1==ex) && (z_y == ey)) {//敌人生成 
			toxy(ex,ey); printf(" ");toxy(ex - 1,ey);printf(" ");toxy(ex + 1,ey);printf(" ");
		hello:;
			ex = rand() % 60; ey = rand() % 25;
			if(ex!=fj_x&&ey!=fj_y){toxy(ex, ey); printf("+");toxy(ex - 1, ey); printf("!");toxy(ex + 1, ey);printf("!");}
			else goto hello;//防撞车
			mony += 2;
			if (ex == 30 || ey == 7) {
				mony += 2;
			}
			if (ex == 19 || ey == 4) {   //欧皇快乐机
				hp[1] += 2;
			}
			hp[1] += 2;
		}
	}
}

不要说我的代码可读性差,因为我的代码就是可读性差
全部代码,全部木大∑(っ°Д°;)っ卧槽,不见了————个鬼

#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <ctime>
//飞机的移动      飞机坐标x  飞机坐标y   敌人坐标x 敌人坐标y 子弹坐标x,y 没用的东东   钱    射击距离 血量          
char feiji_yd, vip; int fj_x = 4, fj_y = 7; int ex = 7, ey = 9; int z_x, z_y, slp = 30, mony, zd = 3, hp[2] = { 0,100 };
void feiji(); void toxy(int, int); void bong(); void shangc(); void yh();
//飞机函数       坐标函数togoxy       子弹函数    商店         扣血

int main() {
	srand(time(NULL));                //{
	ex = rand() % 60; ey = rand() % 25;//开始敌人的位置             //
	toxy(ex, ey); printf("o");       //}
	toxy(fj_x, fj_y);
	feiji();
}
void toxy(int x, int y) {
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 移动 
	SetConsoleCursorPosition(hOut, pos);//
}
void feiji() {
	while (1)
	{
		feiji_yd = _getch();
          yh();//判断是否扣血
		switch (feiji_yd)//控制飞机
		{
		case 'w':
			if ( ((fj_y-1)!=ey|| fj_x!=ex)&&(fj_y!=0))
				fj_y -= 1;
			break;
		case 's':
			if (((fj_y + 1) != ey|| fj_x != ex)&&(fj_y!=26))
				fj_y += 1; 
			break;
		case 'a':
			if (((fj_x - 1) != ex || fj_y != ey)&&(  fj_x != 0))
				fj_x -= 1; 
			break;
		case 'd':
			if ( ((fj_x + 1) != ex || fj_y  != ey) && (fj_x != 64))
				fj_x += 1; 
			break;
		case 'g':
			toxy(fj_x, fj_y); printf("A");
			bong();
			break;
		case'b':
			shangc();
			break;
		} 
		toxy(65, 1);
	printf("mony=%d ", mony);//显示 钱和生命值
	toxy(65, 2);
	printf("hp=%d ", hp[1]);
		Sleep(slp);
		toxy(fj_x, fj_y); printf("A");//删除前一个打印的飞机
		if (feiji_yd == 'a') { toxy(fj_x + 1, fj_y); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 'd') { toxy(fj_x - 1, fj_y); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 'w') { toxy(fj_x, fj_y + 1); printf(" "); toxy(fj_x, fj_y);}
		if (feiji_yd == 's') { toxy(fj_x, fj_y - 1); printf(" "); toxy(fj_x, fj_y);}
		toxy(ex - 1, ey); printf("!"); toxy(ex + 1, ey); printf("!"); 
	}
}
void bong() {
	for ( z_y = fj_y-1; z_y >=((fj_y<zd)?0:(fj_y-zd)); z_y--) {//子弹发射 及射击距离
		z_x = fj_x;
		
        toxy(z_x, z_y); printf("|");
		Sleep(20);
		if ( z_y == 0) {
			toxy(z_x, z_y); printf(" ");
		}
		if(z_y!=fj_y-1)
		toxy(z_x, (z_y + 1)); printf(" ");
		 if (z_y == (fj_y-zd)) {
			toxy(z_x, z_y);
			printf(" ");
		}
		
		if ((z_x == ex||z_x+1==ex||z_x-1==ex) && (z_y == ey)) {//敌人生成 
			toxy(ex,ey); printf(" ");toxy(ex - 1,ey);printf(" ");toxy(ex + 1,ey);printf(" ");
		hello:;
			ex = rand() % 60; ey = rand() % 25;
			if(ex!=fj_x&&ey!=fj_y){toxy(ex, ey); printf("+");toxy(ex - 1, ey); printf("!");toxy(ex + 1, ey);printf("!");}
			else goto hello;//防撞车
			mony += 2;
			if (ex == 30 || ey == 7) {
				mony += 2;
			}
			if (ex == 19 || ey == 4) {   //欧皇快乐机
				hp[1] += 2;
			}
			hp[1] += 2;
		}
	}
}
void shangc() {//商场
	vip = _getch();
	switch (vip)
	{
	case 'b':
		if (mony >10) {
         zd++;
		 mony -= 5;
		}
		break;
	case'h ':
		if (mony > 20) {
			hp[1] += 20;
			mony -= 20;
		}
		 break;

	default:
		break;
	}
}
void yh() {//扣血
	hp[0] += 1;
	if (hp[0] == 25) {
		hp[1] -= 2;
		hp[0] = 0;
	}
}

上一篇:线性dp—奶牛渡河


下一篇:《算法竞赛进阶指南》0x00 POJ1845 分治