C++实现的经典小游戏

注:

DEV C++环境运行效果不佳或无法运行

第一种

实现走迷宫游戏:

#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<windows.h>
#include<iostream>
using namespace std;
char pr[10]= {1,' ','E','O'} ;
int level,n,m,map[50][50],i,j,x,y,k,ex,ey;
char c[1001];
int main() {
	system("cls");
	puts("if you no full screen,please press full screen");
	getch();
	system("cls");
	puts("please choose level");
again:
	;
	cin>>level;
	if(level>1||level<1) {
		puts("haven't this level");
		goto again;
	}
	puts("loading...");
	sprintf(c,"%d.map",level);
	freopen(c,"r",stdin);
	cin>>n>>m;
	for(i=1; i<=n; i++)
		for(j=1; j<=m; j++)
			cin>>map[i][j];
	fclose(stdin);
	for(i=1; i<=n; i++)
		for(j=1; j<=m; j++)
			if(map[i][j]==3) {
				x=i;
				y=j;
			} else if(map[i][j]==2) {
				ex=i;
				ey=j;
			}
	while(x!=ex&&y!=ey) {
		system("cls");
		for(i=1; i<=n; i++,putchar('\n'))
			for(j=1; j<=m; j++)
				putchar(pr[map[i][j]]);
		k=getch();
		if(k=='w')
			if(map[x-1][y]&&(x-1)) {
				map[x-1][y]=3;
				map[x][y]=1;
				x--;
			}
		if(k=='s')
			if(map[x+1][y]&&(x+1)<=n) {
				map[x+1][y]=3;
				map[x][y]=1;
				x++;
			}
		if(k=='a')
			if(map[x][y-1]&&(y-1)) {
				map[x][y-1]=3;
				map[x][y]=1;
				y--;
			}
		if(k=='d')
			if(map[x][y+1]&&(y+1)<=m) {
				map[x][y+1]=3;
				map[x][y]=1;
				y++;
			}
	}
	system("cls");
	return 0;
}

第二种

实现贪吃蛇游戏:

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>

using namespace std;

const int h=50,w=50,MaxLen=400;
void gotoxy(short y,short x) {
	COORD pos= {x,y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
struct node {
	int x,y;
};
node s[MaxLen];
node food;
int dir,len;
int Map[h+5][w+5];
int Time[7]= {0},level;


void FoodCreate() {
	srand ((unsigned)time(NULL));
	while(1) {
		food.x=rand()%h,food.y=rand()%w;
		if(food.x>0&&food.y>0&&Map[food.x][food.y]==0)break;
	}
	gotoxy(food.x,food.y),printf("@");
	gotoxy(h+1,0);
}

void init() {
	system("cls");
	for(int i=0; i<=h; i++) {
		for(int j=0; j<=w; j++) {
			if(i==0||j==0||i==h||j==w)Map[i][j]=1,printf("#");
			else Map[i][j]=0,printf(" ");
		}
		printf("\n");
	}

	len=2;
	dir=0;
	s[1].x=12,s[1].y=4;
	s[len].x=12,s[len].y=3;
	Map[s[1].x][s[1].y]=Map[s[len].x][s[len].y]=1;
	gotoxy(s[1].x,s[1].y),printf("*");
	gotoxy(s[len].x,s[len].y),printf("*");
	gotoxy(h+1,0);

	FoodCreate();
}

int move() {
	node next=s[1];
	switch(dir) {
		case 0:
			next.y++;
			break;
		case 1:
			next.x--;
			break;
		case 2:
			next.y--;
			break;
		case 3:
			next.x++;
			break;
	}
	if(Map[next.x][next.y])return 0;
	if(next.x==food.x&&next.y==food.y) {
		len++;
		FoodCreate();
	} else {
		gotoxy(s[len].x,s[len].y),printf(" ");
		Map[s[len].x][s[len].y]=0;
	}

	gotoxy(next.x,next.y),printf("*");
	gotoxy(h+1,0);
	Map[next.x][next.y]=1;
	for(int i=len; i>1; i--)s[i]=s[i-1];
	s[1]=next;
	Sleep(100);
	return 1;
}

void GameOver() {
	for(int i=1; i<=3; i++) {
		gotoxy(s[1].x,s[1].y);
		printf(" ");
		Sleep(300);
		gotoxy(s[1].x,s[1].y);
		printf("*");
		Sleep(300);
	}
	gotoxy(h+1,0);
	printf("GameOver\n");
	printf("Press any key to continue...");
}
void Welcome() {
	printf("为了您的游戏体验,请先调整控制台字体和布局(记得不要忘了默认设置):\n");
	printf("右键白色框->属性->字体 选择点阵字体并调整字体大小为8×8\n");
	printf("再选择布局设置窗口大小,推荐60×60\n\n");
	printf("WASD控制方向\n");

	printf("\n任意键进入贪吃蛇皮...");
	getch();

}
int main() {
	Welcome();
	init();
	while(1) {
		if(kbhit()) {
			char ch=getch();
			int temp=dir;
			switch(ch) {
				case 'd':
					temp=0;
					break;
				case 'w':
					temp=1;
					break;
				case 'a':
					temp=2;
					break;
				case 's':
					temp=3;
					break;
			}
			if((temp+dir)%2)dir=temp;
		}
		if(move()==0) {
			GameOver();
			getch();
			init();
		}
	}

}

第三种

实现推箱子游戏(此游戏基于DEV C++)

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include <windows.h>

using namespace std;
void Game_Menu(HANDLE hout);
void Game_description(HANDLE hout);
void gotoxy(HANDLE hout, int x, int y);
int DrawMap(HANDLE hout);
void Move(HANDLE hout);
int finish();
void setmap(int n);
void color(int m);
bool flag = true;
int pass = 1;
#define R 10
#define C 10
#define framex 20
#define framey 14
int map[R][C] = {0};
int map1[R][C] = {
	{ 0,0,1,1,1,0,0,0 },
	{ 0,0,1,3,1,0,0,0 },
	{ 0,0,1,0,1,1,1,1 },
	{ 1,1,1,0,0,4,3,1 },
	{ 1,3,4,4,0,1,1,1 },
	{ 1,1,1,5,4,1,0,0 },
	{ 0,0,0,1,3,1,0,0 },
	{ 0,0,0,1,1,1,0,0 }
};
int map2[R][C]= {
	{1,1,1,1,1,0,0,0,0,0},
	{1,5,0,0,1,0,0,0,0,0},
	{1,0,4,4,1,0,1,1,1,0},
	{1,0,4,0,1,0,1,3,1,0},
	{1,1,1,0,1,1,1,3,1,0},
	{0,1,1,0,0,0,0,3,1,0},
	{0,1,0,0,0,1,0,0,1,0},
	{0,1,0,0,0,1,1,1,1,0},
	{0,1,1,1,1,1,0,0,0,0}
};
int map3[R][C]= {
	{ 0,0,0,1,1,1,1,1,1,1 },
	{ 0,0,1,1,0,0,1,0,5,1 },
	{ 0,0,1,0,0,0,1,0,0,1 },
	{ 0,0,1,4,0,4,0,4,0,1 },
	{ 0,0,1,0,4,1,1,0,0,1 },
	{ 1,1,1,0,4,0,1,0,1,1 },
	{ 1,3,3,3,3,3,0,0,1,0 },
	{ 1,1,1,1,1,1,1,1,1,0 },
};
void Game_Menu(HANDLE hout) { 
	system("cls");
	gotoxy(hout, framex, 1);
	cout << "*******************";
	gotoxy(hout, framex, 3);
	cout << "   推箱子    ";
	gotoxy(hout, framex, 5);
	cout << "  按s或S开始   ";
	gotoxy(hout, framex, 7);
	cout << "  按q或Q退出   ";
	gotoxy(hout, framex, 9);
	cout << "游戏前关闭中文输入 ";
	gotoxy(hout, framex, 11);
	cout << "*******************";
	_getch();
};
void Game_description(HANDLE hout) { //操作提示
	system("cls");
	gotoxy(hout, framex, 1);
	cout << "*****************************";
	gotoxy(hout, framex, 3);
	cout << "   按方向键上下左右移动  ";
	gotoxy(hout, framex, 5);
	cout << " 所有箱子到达目的地游戏胜利 ";
	gotoxy(hout, framex, 7);
	cout << "     按q或Q退出     ";
	gotoxy(hout, framex, 9);
	cout << "*****************************";
};
void gotoxy(HANDLE hout, int x, int y) {
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hout, pos);
};
void print(int i) {
	switch(i) {
		case 0:
			color(0x7);
			cout << " "; 
			break;
		case 1:
			color(8);
			cout << "■";
			break;
		case 3:
			color(0xE);
			cout << "◇";
			break;
		case 4:
			color(4);
			cout << "□";
			break;
		case 5:
			color(3);
			cout << "♀"; 
			break;
		case 7:   
			color(6);
			cout << "◆";
			break;
		case 8:   
			color(3);
			cout << "♀";
			break;
		default:
			break;
	}
}
int DrawMap(HANDLE hout) {
	gotoxy(hout, framex + C, framey - 3);
	color(0xF);
	cout << "第" << pass << "关";
	for(int i = 0; i < R; i++) {
		gotoxy(hout, framex, framey + i);
		for(int j = 0; j < C; j++) { 
			print(map[i][j]);
		}
	}
	return 0;
};
void Move(HANDLE hout) {
	int x = 0, y = 0;
	for(int i = 0; i < R; i++) {
		for(int j = 0; j < C; j++) {
			if(map[i][j] == 5 || map [i] [j] == 8) {
				x = i;
				y = j;
				break;
			}
		}
	}
	gotoxy(hout, framex, framey + R);
	color(0xF);
	printf("当前位置:(%d, %d)", x, y);
	int ch = _getch();

	switch(ch) {

		case 'w':
		case 'W':
		case 72:
			if(map[x - 1][y] == 0 || map[x - 1][y] == 3) {
				map[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(map[x][y]);
				map[x - 1][y] += 5;
				gotoxy(hout, framex + 2 * y, framey + x - 1);
				print(map[x - 1][y]);
			} else if(map[x - 1][y] == 4 || map[x - 1][y] == 7) {
				if(map[x - 2][y] == 0 || map[x - 2][y] == 3) {
					map[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(map[x][y]);
					map[x - 1][y] += 1;
					gotoxy(hout, framex + 2 * y, framey + x - 1);
					print(map[x - 1][y]);
					map[x - 2][y] += 4;
					gotoxy(hout, framex + 2 * y, framey + x - 2);
					print(map[x - 2][y]);
				}
			}
			break;

		case 's':
		case 'S':
		case 80:
			if(map[x + 1][y] == 0 || map[x + 1][y] == 3) {
				map[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(map[x][y]);
				map[x + 1][y] += 5;
				gotoxy(hout, framex + 2 * y, framey + x + 1);
				print(map[x + 1][y]);
			} else if(map[x + 1][y] == 4 || map[x + 1][y] == 7) {
				if(map[x + 2][y] == 0 || map[x + 2][y] == 3) {
					map[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(map[x][y]);
					map[x + 1][y] += 1;
					gotoxy(hout, framex + 2 * y, framey + x + 1);
					print(map[x + 1][y]);
					map[x + 2][y] += 4;
					gotoxy(hout, framex + 2 * y, framey + x + 2);
					print(map[x + 2][y]);
				}
			}
			break;

		case 'a':
		case 'A':
		case 75:
			if(map[x][y - 1] == 0 || map[x][y - 1] == 3) {
				map[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(map[x][y]);
				map[x][y - 1] += 5;
				gotoxy(hout, framex + 2 * y - 2, framey + x);
				print(map[x][y - 1]);
			} else if(map[x][y - 1] == 4 || map[x][y - 1] == 7) {
				if(map[x][y - 2] == 0 || map[x][y - 2] == 3) {
					map[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(map[x][y]);
					map[x][y - 1] += 1;
					gotoxy(hout, framex + 2 * y - 2, framey + x);
					print(map[x][y - 1]);
					map[x][y - 2] += 4;
					gotoxy(hout, framex + 2 * y - 4, framey + x);
					print(map[x][y - 2]);
				}
			}
			break;

		case 'd':
		case 'D':
		case 77:
			if(map[x][y + 1] == 0 || map[x][y + 1] == 3) {
				map[x][y] -= 5;
				gotoxy(hout, framex + 2 * y, framey + x);
				print(map[x][y]);
				map[x][y + 1] += 5;
				gotoxy(hout, framex + 2 * y + 2, framey + x);
				print(map[x][y + 1]);
			} else if(map[x][y + 1] == 4 || map[x][y + 1] == 7) {
				if(map[x][y + 2] == 0 || map[x][y + 2] == 3) {
					map[x][y] -= 5;
					gotoxy(hout, framex + 2 * y, framey + x);
					print(map[x][y]);
					map[x][y + 1] += 1;
					gotoxy(hout, framex + 2 * y + 2, framey + x);
					print(map[x][y + 1]);
					map[x][y + 2] += 4;
					gotoxy(hout, framex + 2 * y + 4, framey + x);
					print(map[x][y + 2]);
				}
			}
			break;
		case 'q':
		case 'Q':
			flag = false;
		default:
			break;
	}
};
int finish() { //判断游戏结束
	for(int i = 0; i < R; i++) {
		for(int j = 0; j < C; j++) {
			if(map[i][j] == 4)return 0;
		}
	}
	return 1;
};
void setmap(int n) {
	switch(n) {
		case 1:
			memcpy(map, map1, sizeof(map1));
			break;
		case 2:
			memcpy(map, map2, sizeof(map2));
			break;
		case 3:
			memcpy(map, map3, sizeof(map3));
			break;
	}
};
void color(int m) { //改变输出符号的颜色
	HANDLE consolehend;
	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(consolehend, m);
};
int main() {
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);

	Game_Menu(hout);
	char ch = getch();
	setmap(pass);
	Game_description(hout);
	DrawMap(hout);
	if(ch == 'q' || ch == 'Q') {
		return 0;
	}
	while(flag) {
		Move(hout);
		if(finish()) {
			DrawMap(hout);
			gotoxy(hout, framex, framey + R);
			cout << " 恭喜,成功过关!";
			gotoxy(hout, framex, framey + R + 2);
			cout << "重玩(R)";
			ch = getch();
			system("cls");
			pass++;
			if(ch == 'r' || ch == 'R')pass--;
			if(pass > 3) {
				gotoxy(hout, framex, framey);
				cout << " 您已通过全部关卡!";
				getch();
				flag = false;
			} else {
				setmap(pass);
				Game_description(hout);
				DrawMap(hout);
			}
		}
	}
	return 0;
}

第四种

实现简单24点游戏

#include<iostream>
#include<string>
#include <stdlib.h>
#include<time.h>
using namespace std;
char card[] = { 'A','2','3','4','5','6','7','8','9','10','J','Q','K' };
char buf[4];
double nums[4];
char ope[4] = { '+','-','*','/' };


void cre() { 
	int i = 0;
	int j;
	cout << "生成的四张牌面为:";
	srand((unsigned)time(0));
	for (i = 0; i<4; i++) {
		j =rand() % 13;
		buf[i] = card[j];
	}
	cout << buf[0] << ";" << buf[1] << ";" << buf[2] << ";" << buf[3] << "。" << endl;
	for (i = 0; i<4; i++) {
		if (buf[i] == 'A') nums[i] = 1;
		else if(buf[i] == '2') nums[i] = 2;
		else if (buf[i] == '3') nums[i] = 3;
		else if (buf[i] == '4') nums[i] = 4;
		else if (buf[i] == '5') nums[i] = 5;
		else if (buf[i] == '6') nums[i] = 6;
		else if (buf[i] == '7') nums[i] = 7;
		else if (buf[i] == '8') nums[i] = 8;
		else if (buf[i] == '9') nums[i] = 9;
		else if (buf[i] == '10') nums[i] = 10;
		else if (buf[i] == 'J') nums[i] = 11;
		else if (buf[i] == 'Q') nums[i] = 12;
		else if (buf[i] == 'K') nums[i] = 13;
	}
}

double calcute(double a, double b, char index) {
	if (index == '+') return a + b;
	else if (index == '-') return a - b;
	else if (index == '*') return a*b;
	else if (index == '/')
		if (b != 0)
			return a / b; 
}

void exh() {
	double temp[3], tem[2]; 
	double sum;
	int judge = 0; 
	for (int i = 0; i < 4; i++) { 
		for (int j = 0; j < 4; j++) { 
			for (int k = 0; k < 4; k++) { 
				for (int m = 0; m < 3; m++) {
					if (nums[m + 1] == 0 && ope[i] == '/') break;
					temp[m] = calcute(nums[m], nums[m + 1], ope[i]);
					temp[(m + 1) % 3] = nums[(m + 2) % 4];
					temp[(m + 2) % 3] = nums[(m + 3) % 4]; 
					for (int n = 0; n < 2; n++) {
						if (temp[n + 1] == 0 && ope[j] == '/') break;
						tem[n] = calcute(temp[n], temp[n + 1], ope[j]);
						tem[(n + 1) % 2] = temp[(n + 2) % 3]; 
						if (tem[1] == 0 && ope[k] == '/') break;
						sum = calcute(tem[0], tem[1], ope[k]); 
						if (sum == 24) { 
							judge = 1;  
							if (m == 0 && n == 0)
								cout << "((" << nums[0] << ope[i] << nums[1] << ")" << ope[j] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl;
							else if (m == 0 && n == 1)
								cout << "(" << nums[0] << ope[i] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[j] << nums[3] << ")=" << sum << endl;
							else if (m == 1 && n == 0)
								cout << "(" << nums[0] << ope[j] << "(" << nums[1] << ope[i] << nums[2] << ")" << ope[k] << nums[3] << "=" << sum << endl;
							else if (m == 1 && n == 1)
								cout << nums[0] << ope[k] << "((" << nums[1] << ope[i] << nums[2] << ")" << ope[j] << nums[3] << ")=" << sum << endl;
							else if (m == 2 && n == 0)
								cout << "(" << nums[0] << ope[j] << nums[1] << ")" << ope[k] << "(" << nums[2] << ope[i] << nums[3] << ")=" << sum << endl;
							else if (m == 2 && n == 0)
								cout << nums[0] << ope[k] << "(" << nums[1] << ope[j] << "(" << nums[2] << ope[i] << nums[3] << "))=" << sum << endl;
						}
					}
				}
			}
		}
	}
	if (judge == 0)
		cout << "这四张扑克牌无法找到一个合理的解" << endl; 
}
int main() {
	int i;
	int select = 1;
	cout<< " ################################################" << endl
	    << " #            #" << endl
	    << " #    欢迎进入24点游戏    #" << endl
	    << " #            #" << endl
	    << " ################################################" << endl;

	while (select) {
		cout<< " ################################################" << endl
		    << " #            #" << endl
		    << " #    是否开始游戏     #" << endl
		    << " #            #" << endl
		    << " #   0.是    1.否    #" << endl
		    << " #            #" << endl
		    << " ################################################" << endl;
		cout << "请输入你的选择(0或1):";
		cin >> i;
		switch (i) {
			case 0:
				cre();
				exh();
				break;
			case 1:
				select = 0;
				break;
			default:
				cout << "请在0和1之间选择!" << endl;
		}
	}
	return 0;
}

第五种

实现俄罗斯方块游戏(无需图形库)

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

#define MOD 28
#define SIZE_N 19
#define SIZE_M 12

int cur_x,cur_y;
int score,mark,next,map[SIZE_N][SIZE_M],Gamespeed=300;

int shape[28][6]= {
	{0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0},
	{0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1},
	{-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1},
	{-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0},
	{-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0},
	{0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1},
	{0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0}
};

void gotoxy(int x,int y) {
	COORD c;
	c.X=x-1;
	c.Y=y-1;
	SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void Gameover() {
	int i,j,flag=0;
	for(j=1; j<SIZE_M-1; j++) {
		if(map[1][j]!=0) {
			flag=1;
			break;
		}
	}
	if(flag==1) {
		for(i=1; i<SIZE_N-1; i++) {
			gotoxy(2,i+1);
			for(j=1; j<SIZE_M-1; j++) {
				printf("□");
			}
			puts("");
		}
		gotoxy(7,9);
		printf("GAME OVER!");
		gotoxy(1,SIZE_N+1);
		exit(0);
	}
}
void ShowMap(int id) {
	int i,j;
	gotoxy(1,1);
	if(id!=-1) {
		for(i=0; i<SIZE_N; i++) {
			for(j=0; j<SIZE_M; j++) {
				if(i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1)printf(" ");
				else if(i==0 || i==SIZE_N-1)printf("--");
				else if(j==0 || j==SIZE_M-1)printf("|");
				else if(map[i][j]==2) printf("■");
				else if(i==cur_x+shape[id][0] && j==cur_y+shape[id][1] ||
				        i==cur_x+shape[id][2] && j==cur_y+shape[id][3] ||
				        i==cur_x+shape[id][4] && j==cur_y+shape[id][5] ||
				        i==cur_x && j==cur_y)
					printf("■");
				else if(map[i][j]==0) printf(" ");
			}
			if(i==1)printf(" 下一个 :");
			if(i==11)printf(" 等分 : %d",score);
			if(i==14)printf(" 速度 : %d",score/100+1);
			puts("");
		}
	} else {
		mark=1;
		for(i=0; i<SIZE_N; i++) {
			for(j=0; j<SIZE_M; j++) {
				if(i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1)printf(" ");
				else if(i==0 || i==SIZE_N-1)printf("--");
				else if(j==0 || j==SIZE_M-1)printf("|");
				else if(map[i][j]==2) printf("■");
				else if(map[i][j]==0) printf(" ");
			}
			if(i==1)printf(" next:");
			if(i==11)printf(" score : %d",score);
			if(i==14)printf(" speed : %d",score/100+1);
			puts("");
		}
	}

	gotoxy(30,6);
	printf(" ");
	for(i=0; i<6; i=i+2) {
		gotoxy(30+2*shape[id][i+1],6+shape[id][i]);
		printf(" ");
	}
	gotoxy(30,6);
	printf("■");
	for(i=0; i<6; i=i+2) {
		gotoxy(30+2*shape[next][i+1],6+shape[next][i]);
		printf("■");
	}
	Sleep(Gamespeed);
}

void init(int id) {
	int i,j;
	memset(map,0,sizeof(map));
	for(i=0; i<SIZE_N; i++) {
		for(j=0; j<SIZE_M; j++)
			if(i==SIZE_N-1 || j==0 || j==SIZE_M-1)
				map[i][j]=-1;
	}
	cur_x=0;
	cur_y=5;
	ShowMap(id);
}

int judge_in(int x,int y,int id) {
	int i;
	if(map[x][y]!=0)return 0;
	for(i=0; i<6; i=i+2) {
		if(map[ x+shape[id][i] ][ y+shape[id][i+1] ]!=0)return 0;
	}
	return 1;
}

void fun_score() {
	int i,j,ii,jj;
	for(i=1; i<SIZE_N-1; i++) {
		int flag=0;
		for(j=1; j<SIZE_M-1; j++) {
			if(map[i][j]!=2) {
				flag=1;
				break;
			}
		}
		if(flag==0) {
			int k=3;
			while(k--) {
				gotoxy(2,i+1);
				for(ii=1; ii<SIZE_M-1; ii++) {
					if(map[i][ii]==2) {
						if(k%2==1)printf(" ");
						else printf("■");
					}
				}
				Sleep(100);
			}
			for(ii=i; ii>1; ii--) {
				for(jj=1; jj<SIZE_M-1; jj++) map[ii][jj]=map[ii-1][jj];
			}
			ShowMap(-1);
			score+=10;
			if(score%100==0 && score!=0)Gamespeed-=50;
		}
	}
}

int main() {
	int i,id,set=1;

	srand(time(NULL));
	id=rand()%MOD;
	id=(id+MOD)%MOD;
	next=rand()%MOD;
	next=(next+MOD)%MOD;

	init(id);

	while(1) {
Here:
		mark=0;
		if(set==0) {
			id=next;
			next=rand()%MOD;
			next=(next+MOD)%MOD;
			cur_x=0;
			cur_y=5;
			set=1;
		}

		while(!kbhit()) {
			Gameover();
			if(judge_in(cur_x+1,cur_y,id)==1) cur_x++;
			else {
				map[cur_x][cur_y]=2;
				for(i=0; i<6; i=i+2)
					map[ cur_x+shape[id][i] ][ cur_y+shape[id][i+1] ]=2;
				fun_score();
				set=0;
			}
			if(mark!=1)ShowMap(id);
			goto Here;
		}

		char key;
		key=getch();

		if(key==72) {
			int tmp=id;
			id++;
			if( id%4==0 && id!=0 )id=id-4;
			if(judge_in(cur_x,cur_y,id)!=1)id=tmp;
		} else if(key==80 && judge_in(cur_x+1,cur_y,id)==1)cur_x++;
		else if(key==75 && judge_in(cur_x,cur_y-1,id)==1)cur_y--;
		else if(key==77 && judge_in(cur_x,cur_y+1,id)==1)cur_y++;
		else if(key==27) {
			gotoxy(1,SIZE_N+1);
			exit(0);
		}
	}
	getch();
	return 0;
}

  • 28
    点赞
  • 236
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
以下是一个简单的 C++ 贪吃蛇小游戏实现示例: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN}; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); //清屏 for (int i = 0; i < width+2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width+2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x > width || x < 0 || y > height || y < 0) gameOver = true; for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); // 控制游戏速度 } return 0; } ``` 这是一个基本的贪吃蛇小游戏实现,其中用到了数组存储尾巴的坐标,用枚举变量表示贪吃蛇移动的方向。通过控制台输入和输出来实现游戏的交互和界面显示,同时利用 `Sleep()` 函数来控制游戏的速度。 运行程序后,使用键盘上的 WASD 键来控制贪吃蛇的移动,X 键退出游戏。每吃到一个水果得分加 10 分,当贪吃蛇碰到墙壁或自己的身体时游戏结束。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值