C++有趣程序(3)
本文最后更新于 434 天前,其中的信息可能已经有所发展或是发生改变。

自创贪吃蛇:

#include <iostream> 
#include <windows.h> 
#include <conio.h> 
#include <time.h> 
#include <stdlib.h> 
using namespace std; 
const int N = 20;
void Get_xy(int x,int y) { 
	HANDLE hout; 
	COORD pos; 
	pos.X=x*2; 
	pos.Y=y; 
	hout=GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleCursorPosition(hout,pos); 
} 
void Color(int num)  { 
	HANDLE hout; 
	hout = GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleTextAttribute(hout,num); 
} 
void Initial() { 
	int i,j; 
	int wall[N+2][N+2]={{0}}; 
	for(i=1;i<=N;i++) 
	for(j=1;j<=N;j++) 
	wall[i][j]=1; 
	Color(11); 
	for(i=0;i<N+2;i++) { 
		for(j=0;j<N+2;j++) { 
			if(wall[i][j]) cout<<"■"; 
			else cout<<"□"; 
		} 
		cout<<endl; 
	} 
	Get_xy(N+3,1); Color(20); 
	cout<<"按'W','S','A','D'进行操作"<<endl; 
	Get_xy(N+3,2); Color(20); 
	cout<<"按任意键暂停"<<endl; 
	Get_xy(N+3,3); Color(20); 
	cout<<"得分:"<<endl; 
} 
void game() { 
	int** snake=NULL; 
	int len = 1, i, score = 0; 
	int apple[2], tail[2]; 
	char ch = 'p'; 
	Initial(); 
	srand((unsigned)time(NULL)); 
	apple[0]=rand()%N+1; 
	apple[1]=rand()%N+1; 
	Get_xy(apple[0],apple[1]); 
	Color(12); 
	cout<<"●"<<endl; 
	snake=(int**)realloc(snake,sizeof(int*)*len); 
	for(i=0;i<len;i++) 
		snake[i]=(int*)malloc(sizeof(int)*2); 
	snake[0][0]=N/2; 
	snake[0][1]=N/2+1; 
	Get_xy(snake[0][0],snake[0][1]); Color(14); 
	cout<<"⊙"<<endl; 
	int flag = 1; 
	while(1)  { 
		if(flag) { 
			tail[0]=snake[len-1][0]; 
			tail[1]=snake[len-1][1]; 
			Get_xy(tail[0],tail[1]); 
			Color(11); 
			cout<<"■"<<endl; 
		} 
		flag = 1; 
		for(i=len-1;i>0;i--) { 
			snake[i][0]=snake[i-1][0]; 
			snake[i][1]=snake[i-1][1]; 
			Get_xy(snake[i][0],snake[i][1]); 
			Color(14); 
			cout<<"★"<<endl; 
		} 
		if (kbhit()) { 
			Get_xy(0,N+3); 
			ch=getche(); 
		} 
		switch (ch) { 
			case 'W': 
			case 'w': snake[0][1]--; break; 
			case 'S': 
			case 's': snake[0][1]++; break; 
			case 'A': 
			case 'a': snake[0][0]--; break; 
			case 'D': 
			case 'd': snake[0][0]++; break; 
			default :break; 
		} 
		for (i=1;i<len;i++) { 
			if(snake[0][0]==snake[i][0] && snake[0][1]==snake[i][1]) { 
				Get_xy(N/2,N/2); Color(30); 
				cout<<"Game over!"<<endl; 
				exit(0); 
			} 
		} 
		Get_xy(snake[0][0],snake[0][1]); 
		Color(14); cout<<"⊙"<<endl; 
		Sleep(abs(200-0.5*score)); 
		if(snake[0][0]==apple[0] && snake[0][1]==apple[1]) { 
			flag=0;
			score++;
			len++;
			srand((unsigned)time(NULL)); 
			snake=(int**)realloc(snake,sizeof(int*)*len); 
			snake[len-1]=(int*)malloc(sizeof(int)*2); 
			Get_xy(N+6,3); Color(20);
			cout<<score<<endl; 
			apple[0]=rand()%N+1;
			apple[1]=rand()%N+1; 
			Get_xy(apple[0],apple[1]); 
			Color(12); 
			cout<<"●"<<endl; 
		} 
		if(snake[0][0]==-1 || snake[0][0]==N+1 || snake[0][1]==-1 || snake[0][1]==N+1) { 
			Get_xy(N/2,N/2); Color(30); 
			cout<<"Game Over!"<<endl; 
			for(i=0;i<len;i++) 
			free(snake[i]); 
			Sleep(INFINITE); 
			exit(0); 
		} 
		if(len>=N*N-1) { 
			Get_xy(N/2,N/2); Color(30); 
			cout<<"Win!"<<endl; 
			for(i=0;i<len;i++) 
				free(snake[i]); 
			Sleep(INFINITE); 
			exit(0); 
		} 
	} 
} 
int main() { 
	game(); 
	return 0;
}

版权声明:本文为博主Zhang, Xuheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://nth.ink/cpp/games/P402.html

(广告由我们的赞助商提供,内容与本站无关)

评论

  1. NSObject 23786 博主
    Macintosh Chrome 80.0.3987.149
    1年前
    2020-4-08 11:18:24

    ????。

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇