C语言实现2048游戏

这代码真是把我写伤着了,现在没心情写注释,等以后有时间再写吧。

当然如果你要运行这段代码,在vc环境下你必须先安装easyx,使vc具有图形函数库。

分数功能还没写。

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include <windows.h>
#include<time.h>
#include "visual.h"

void print();
void check();
void clear();
void checknum();
void move_up();
void move_down();
void move_left();
void move_right();


//空位栈
typedef struct{
int x;
int y;
int top;
}empty;       

     
int a[4][4]={0,0,0,0,
			 0,0,0,0,
			 0,0,0,0,
			 0,0,0,0,
			};
empty b[16];
int mark=0;
int temp=0;
                             

void main(){
char str;
//初始化窗口
initgraph(550,420);
setbkmode(TRANSPARENT);
setbkcolor(RGB(187,173,160));
cleardevice();
draw_first();
mark=1;
check();
check();
mark=0;
print();
while(1){
str=getch();
fflush(stdin);
switch(str){
case 'w':move_up();check();break;
case 's':move_down();check();break;
case 'a':move_left();check();break;
case 'd':move_right();check();break;
}
if(temp==0)
checknum();
mark=0;
Sleep(1);
}
}



void print(){
int x,y;
	for(x=0;x<4;x++)
		for(y=0;y<4;y++)
			draw(x,y,a[y][x],mark);
}


void move_up(){
	int x,y,i,j,m;
	for(y=0;y<3;y++) {
		for(x=0;x<4;x++) {
			if(a[y][x]==0)
				for(m=y+1;m<4;m++)
					if(a[m][x]!=0) {
						mark=1;
						while(a[y][x]==0){
							for(m=y;m<3;m++)
								a[m][x]=a[m+1][x];
							
							a[3][x]=0;}
						break;
					}			
		}                          //推进	
	}     
	for(j=0;j<3;j++)
		for(i=0;i<4;i++)
			if(a[j][i]!=0&&a[j][i]==a[j+1][i])
			{a[j][i]*=2;
			for(m=j+1;m<3;m++){
				a[m][i]=a[m+1][i];}
				a[3][i]=0;
			mark=1;}            //相加
			print();
}


void move_down(){
	int x,y,i,j,m;
	for(y=3;y>0;y--) {
		for(x=0;x<4;x++) {
			if(a[y][x]==0)
				for(m=y-1;m>=0;m--)
					if(a[m][x]!=0) {
						while(a[y][x]==0){
							mark=1;
							for(m=y;m>0;m--)
								a[m][x]=a[m-1][x];
							
							a[0][x]=0;}
						break;
					}			
		}                          //推进	
	}   
	for(j=3;j>0;j--)
		for(i=0;i<4;i++)
			if(a[j][i]!=0&&a[j][i]==a[j-1][i])
			{a[j][i]*=2;
			for(m=j-1;m>0;m--){
				a[m][i]=a[m-1][i];}
				a[0][i]=0;
			mark=1;}           //相加	
			print();
}

void move_left(){
	int x,y,i,j,m;
	for(y=0;y<3;y++) {
		for(x=0;x<4;x++) {
			if(a[x][y]==0)
				for(m=y+1;m<4;m++)
					if(a[x][m]!=0) {
						while(a[x][y]==0){
							mark=1;
							for(m=y;m<3;m++)
								a[x][m]=a[x][m+1];
							
							a[x][3]=0;}
						break;
					}			
		}                          //推进	
	}     
	for(j=0;j<3;j++)
		for(i=0;i<4;i++)
			if(a[i][j]!=0&&a[i][j]==a[i][j+1])
			{a[i][j]*=2;
			for(m=j+1;m<3;m++){
				a[i][m]=a[i][m+1];}
				a[i][3]=0;
			mark=1;}            //相加
			print();
}


void move_right(){
	int x,y,i,j,m;
	for(y=3;y>0;y--) {
		for(x=0;x<4;x++) {
			if(a[x][y]==0)
				for(m=y-1;m>=0;m--)
					if(a[x][m]!=0) 
					{
						while(a[x][y]==0){
							mark=1;
							for(m=y;m>0;m--)
							a[x][m]=a[x][m-1];
							a[x][0]=0;}
						break;
					}			
		}                          //推进	
	}   
	for(j=3;j>0;j--)
		for(i=0;i<4;i++)
			if(a[i][j]!=0&&a[i][j]==a[i][j-1])
			{a[i][j]*=2;
			for(m=j-1;m>0;m--){
				a[i][m]=a[i][m-1];}
			a[i][0]=0;
			mark=1;}           //相加	
			print();
}

void check(){
	int x,y,num;
	int max,min;
	Sleep(200);
	srand((int)time(0));
	num=rand()%9;
	if(num>=0&&num<8) num=2;
		else num=4;
		if(mark==1){
do{
x=rand()%4;
y=rand()%4;
}while(a[x][y]!=0);
a[x][y]=num;
print();}

}

void checknum(){
	int x,y,num;
	int max,min;
		for(x=0;x<4;x++)
			for(y=0;y<10;y++){
				if(a[x][y]==2048){
					max=win();
					if(max==1) {cleardevice();print();temp=1;}
				else if(max==2) {clear();mark=1;check();check();mark=0;print();temp=1;}
				}
			}
}

void clear(){
int x,y;
for(x=0;x<4;x++)
for(y=0;y<4;y++)
a[x][y]=0;

}

下面一个是2048的头文件

#include<iostream>
#include<graphics.h>
#include<time.h>
using namespace std;

//第四个参数为传进来的分数
void draw(int x,int y,int num,int score){
	IMAGE pic;
	char str[10];
	switch(num)
	{
	case 0: loadimage(&pic,"IMAGE","pic0");break;
	case 2:loadimage(&pic,"IMAGE","pic2");break;
	case 4:loadimage(&pic,"IMAGE","pic4");break;
	case 8:loadimage(&pic,"IMAGE","pic8");break;
	case 16:loadimage(&pic,"IMAGE","pic16");break;
	case 32:loadimage(&pic,"IMAGE","pic32");break;
	case 64:loadimage(&pic,"IMAGE","pic64");break;
	case 128:loadimage(&pic,"IMAGE","pic128");break;
	case 256:loadimage(&pic,"IMAGE","pic256");break;
	case 512:loadimage(&pic,"IMAGE","pic512");break;
	case 1024:loadimage(&pic,"IMAGE","pic1024");break;
	case 2048:loadimage(&pic,"IMAGE","pic2048");break;
	}
	putimage(x*100+20,y*100+20,&pic);
	setfillcolor(RGB(117,117,117));
	settextcolor(WHITE);
	settextstyle(20,11,"黑体");
	fillroundrect(440,120,540,320,30,30);
	outtextxy(440,150,"当前分数:");
	_itoa(score,str,10);           //将数字转换为字符串的函数
	outtextxy(450,180,str);
}



//进入图像,已提供了延迟
void draw_first(){
	int x,y;
	IMAGE first;
	loadimage(&first,"IMAGE","first");
	putimage(0,0,&first);
	Sleep(1500);
	setlinecolor(RGB(187,173,160));
	for(y=0;y<10;y++){
	for(x=y;x<550;x+=10)
		line(x,0,x,420);
	    Sleep(50);
	}
		
			cleardevice();
}               


//继续玩清屏返回1,重新开始清屏返回2
int win(){
	MOUSEMSG mouse;
	COLORREF ref;
	int r,g,b;
	int x,y;
	char *yes,*no,*win;
	srand((int)time(0));
	BeginBatchDraw();
	for(x=0;x<550;x++){
		for(y=0;y<420;y++){
			ref=getpixel(x,y);
			r=GetRValue(ref);
			g=GetGValue(ref);
			b=GetBValue(ref);
			putpixel(x,y,RGB(r*0.5,g*0.5,b*0.5));
		}
	}
	FlushBatchDraw();
	EndBatchDraw();
	yes="YES";
	no="NO";
	win="YOU WIN !";
	for(x=0;x<10;x++){
		settextcolor(RGB(rand()%255,rand()%255,rand()%255));
		settextstyle(100,50,NULL);
		outtextxy(50,50,win);
		Sleep(100);
	}
	
	settextstyle(50,25,"微软雅黑");
	settextcolor(GREEN);
	outtextxy(200,150,"继续?");
	settextcolor(BLUE);
	outtextxy(120,250,yes);
	outtextxy(320,250,no);
	setfillcolor(RED);
	
	while(1){
		FlushMouseMsgBuffer();
		mouse=GetMouseMsg();
		if(mouse.uMsg==WM_LBUTTONDOWN)
		{x=mouse.x;
		y=mouse.y;
		
		if(x>120&&x<220&&y>250&&y<300)
		{cleardevice();return 1;}
		if(x>320&&x<420&&y>250&&y<300)
		{cleardevice();return 2;}	 
		}
	}
}     

//显示YOU FAILED,点击确定后清屏
void failed()
{
	MOUSEMSG mouse;
	COLORREF ref;
	int r,g,b;
	int x,y;
	srand((int)time(0));
	BeginBatchDraw();
	for(x=0;x<550;x++){
		for(y=0;y<420;y++){
			ref=getpixel(x,y);
			r=GetRValue(ref);
			g=GetGValue(ref);
			b=GetBValue(ref);
			putpixel(x,y,RGB(r*0.5,g*0.5,b*0.5));
		}
	}
	FlushBatchDraw();
	EndBatchDraw();
	
	for(x=0;x<5;x++){
		settextcolor(RGB(rand()%255,rand()%255,rand()%255));
		settextstyle(100,50,NULL);
		outtextxy(20,50,"YOU FAILED");
		Sleep(1000);
	}
	
	settextstyle(50,25,"微软雅黑");
	settextcolor(GREEN);
	outtextxy(250,200,"OK");
	
	while(1){
		FlushMouseMsgBuffer();
		mouse=GetMouseMsg();
		if(mouse.uMsg==WM_LBUTTONDOWN)
		{x=mouse.x;
		y=mouse.y;
		
		if(x>250&&x<350&&y>200&&y<300)
		{cleardevice();break;}
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值