`
925695531
  • 浏览: 22371 次
  • 性别: Icon_minigender_1
文章分类
社区版块
存档分类
最新评论

自己做的一个俄罗斯方块

 
阅读更多

昨天晚上突发奇想想做一下,然后就真实的感受到了自己的菜,不过终究还是做出来了,虽然丑的一逼,还那么多bug,不过还是很高兴,不会任何东西然后把它写出来还是很不容易了

我用了graphics.h,这个vc没有,我就把它抄下写进去了,,,

graphics.h:

/******************************************************
 * EasyX Library for C++ (Ver:v20121115(beta))
 * http://www.easyx.cn
 *
 * graphics.h
 *	基于 EasyX.h,实现在 VC 下实现简单绘图。
 *	同时,为了兼容 Turbo C/C++ 和 Borland C/C++ 等一系
 *	列开发环境中的 BGI 绘图库函数,做了相应扩展。
 ******************************************************/

#pragma once

#include <easyx.h>

// 兼容 initgraph 绘图模式的宏定义(无实际意义)
#define DETECT	0
#define VGA		0
#define VGAHI	0

// BGI 格式的初始化图形设备,默认 640 x 480
HWND initgraph(int* gdriver, int* gmode, char* path)
{
	return initgraph(640, 480, NULL);
}

// 画多边形
void drawpoly(int numpoints, const int *polypoints)
{
	polygon(numpoints, polypoints);
}

// 画填充的多边形
void fillpoly(int numpoints, const int *polypoints)
{
	fillpolygon(numpoints, polypoints);
}

// 获取最大的宽度值
int getmaxx()
{
	return getwidth() - 1;
}

// 获取最大的高度值
int getmaxy()
{
	return getheight() - 1;
}

MyGraph.h:

#include <graphics.h>
#include <conio.h>

class Cell
{
	public :
		int x , y ;
		bool  vis ;
		void setx( int newx )
		{
			x = newx ; 
		}
		void sety( int newy )
		{
			y = newy ;
		}
		void setvis( bool newvis )
		{
			vis = newvis ;
		}
		bool getvis()
		{
			return vis ; 
		}
		int  getx()
		{
			return x ;
		}
		int  gety()
		{
			return y ;
		}
		void printcell()
		{
			fillrectangle( x * 40 , y * 40 , ( x + 1 ) * 40 , ( y + 1 ) * 40 ) ;
		}
		Cell()
		{
			x = 0 ; 
			y = 0 ; 
			vis = 0 ;
		}
};


class Grid
{
	public :
		int   x , y ;
		int   num , next ;
		bool  changeable ;
		
		void  setx( int newx )
		{
			x = newx ;
		}
		void  sety( int newy )
		{
			y = newy ;
		}
		void  setnum( int newnum )
		{
			num = newnum ;
		}
		void  setchangeable( bool flag )
		{
			changeable = flag ;
		}
		void setnext()
		{
			if( num == 0 ) next = 7 ;
			else if( num == 1 ) next = 1 ;
			else if( num == 2 ) next = 8 ;
			else if( num == 3 ) next = 10 ;
			else if( num == 4 ) next = 14 ;
			else if( num == 5 ) next = 17 ;
			else if( num == 6 ) next = 18 ; 
			else if( num == 7 ) next = 0 ; 
			else if( num == 8 ) next = 9 ; 
			else if( num == 9 ) next = 10 ; 
			else if( num == 10 ) next = 2 ; 
			else if( num == 11 ) next = 12 ;
			else if( num == 12 ) next = 13 ;
			else if( num == 13 ) next = 3 ;
			else if( num == 14 ) next = 15 ;
			else if( num == 15 ) next = 16 ;
			else if( num == 16 ) next = 4 ; 
			else if( num == 17 ) next = 5 ;
			else if( num == 18 ) next = 6 ;
		}
		int getx()
		{
			return x ; 
		}
		int gety()
		{
			return y ;
		}
		int getnext()
		{
			return next ;
		}
		int getnum()
		{
			return num ; 
		}
		bool getchangeable()
		{
			return changeable ; 
		}
		void  Changenum()
		{
			num = next ;
			setnext() ;
		}
		Grid()
		{
			x = 0 ;
			y = 0 ;
			changeable = 0 ;
			num = 0 ;
			next = 0 ;
		}
};

main:

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include "MyGraph.h"
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h> 

int MAX = 50000 ;

bool  GameOver = 0 ; 

Cell cell[16][9] ;
Grid grid ;
int key1 , key ;

bool  CellGrid[19][4][4]={
			{
				{1,1,1,1},      // 0
				{0,0,0,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{1,1,0,0},		//1	
				{1,1,0,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{1,0,0,0},		//2
				{1,1,1,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{0,0,1,0},		//3
				{1,1,1,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{0,1,0,0},		//4
				{1,1,1,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{1,1,0,0},		//5
				{0,1,1,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{0,1,1,0},		//6
				{0,0,1,1},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{1,0,0,0},		//7
				{1,0,0,0},
				{1,0,0,0},
				{1,0,0,0}
			},
			{
				{1,1,0,0},		//8
				{1,0,0,0},
				{1,0,0,0},
				{0,0,0,0}
			},
			{
				{1,1,1,0},		//9
				{0,0,1,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{0,1,0,0},		//10
				{0,1,0,0},
				{1,1,0,0},
				{0,0,0,0}
			},
			{
				{1,0,0,0},		//11
				{1,0,0,0},
				{1,1,0,0},
				{0,0,0,0}
			},
			{
				{1,1,1,0},		//12
				{1,0,0,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{1,1,0,0},		//13
				{0,1,0,0},
				{0,1,0,0},
				{0,0,0,0}
			},
			{
				{1,0,0,0},		//14
				{1,1,0,0},
				{1,0,0,0},
				{0,0,0,0}
			},
			{
				{1,1,1,0},		//15
				{0,1,0,0},
				{0,0,0,0},
				{0,0,0,0}
			},
			{
				{0,1,0,0},		//16
				{1,1,0,0},
				{0,1,0,0},
				{0,0,0,0}
			},
			{
				{0,1,0,0},		//17
				{1,1,0,0},
				{1,0,0,0},
				{0,0,0,0}
			},
			{
				{1,0,0,0},		//18
				{1,1,0,0},
				{0,1,0,0},
				{0,0,0,0}
			}
};

void ChangeAble()
{
	int  i , j ;
	int   x = grid.getx() ;
	int   y = grid.gety() ;
	bool  flag = 0 ; 
	for( i = 0 ; i < 4 ; i ++ )
		for( j = 0 ; j < 4 ; j ++ )
		{
			if( y + i >= 14 || x + j >= 8 ) continue ;
			if( cell[y+i][x+j].getvis() && CellGrid[grid.getnext()][i][j] )
			{
				flag = 1 ;
				break ;
			}
		}
	if( flag == 1 ) grid.setchangeable( 0 ) ;
	else grid.setchangeable( 1 ) ;
}

bool check( int Dir )
{
	int  dx[3] = { 0 , -1 , 1 } ;
	int  dy[3] = { 1 , 0 ,  0 } ;
	int num = grid.getnum() ;
	int x = grid.getx() ;
	int y = grid.gety() ;
	for( int i = 0 ; i < 4 ; i ++ )
		for( int j = 0 ; j < 4 ; j ++ )
		{
			if( cell[y+i+dy[Dir]][x+j+dx[Dir]].getvis() && CellGrid[num][i][j] ) return false ;
			if( CellGrid[num][i][j] && ( x + j + dx[Dir] < 0 || x + j + dx[Dir] > 8 ) ) return false ;
		}
	return true ;
}

bool jduge()
{
	int num = grid.getnum() ;
	int x = grid.getx() ;
	int y = grid.gety() ;
	for( int i = 0 ; i < 4 ; i ++ )
		for( int j = 0 ; j < 4 ; j ++ )
		{
			if( cell[y+i][x+j].getvis() && CellGrid[num][i][j] ) return false ;
		}
	return true ;
}

int  main()
{
	int i , j , score = 0 ;

	srand( (unsigned)time( NULL ) );
	initgraph( 360 , 600 ) ;

	for( i = 0 ; i < 15 ; i ++ )
		for( j = 0 ; j < 9 ; j ++ )
		{
			cell[i][j].sety( i )   ;
			cell[i][j].setx( j )   ; 
			cell[i][j].setvis( 0 ) ;
		}
	for( i = 0 ; i <= 8 ; i ++ )
	{
		cell[15][i].sety( 15 )  ;
		cell[15][i].setx( i )   ;
		cell[15][i].setvis( 1 ) ;
	}

	while( !GameOver )
	{
		if( score >= 10 ) MAX = 40000 ; 
		else if( score >= 20 ) MAX = 30000 ; 
		else if( score >= 30 ) MAX = 20000 ; 
		else if( score >= 50 ) MAX = 10000 ; 
		else if( score >= 100 ) MAX = 5000 ;
		int temp ;
		temp = rand() % 7 ; 
		grid.setnum( temp ) ;
		grid.setx( 0 ) ;
		grid.sety( 0 ) ;
		grid.setnext() ;
		if( !jduge() )
		{
			GameOver = 1 ; 
			break ;
		}
		while( 1 ){
			cleardevice();
			for( i = 0 ; i < 15 ; i ++ )
				for( j = 0 ; j < 9 ; j ++ )
					if( cell[i][j].getvis() ) cell[i][j].printcell() ;
			for( i = 0 ; i < 4 ; i ++ )
				for( j = 0 ; j < 4 ; j ++ )
					if( !cell[grid.gety()+i][grid.getx()+j].getvis() && CellGrid[grid.getnum()][i][j] ) 
						cell[grid.gety()+i][grid.getx()+j].printcell() ;

			if( !check( 0 ) ) 
			{
				bool  Flag[16] ;
				memset( Flag , 0 , sizeof( Flag ) ) ;
				for( i = 0 ; i < 4 ; i ++ )
					for( j = 0 ; j < 4 ; j ++ )
					{
						if( CellGrid[grid.getnum()][i][j] )
							cell[grid.gety()+i][grid.getx()+j].setvis( 1 ) ;
					}
				for( i = 0 ; i < 15 ; i ++ )
				{
					int flag = 0 ; 
					for( j = 0 ; j < 9 ; j ++ ){
						if( !cell[i][j].getvis() ) flag = 1 ; 
						if( cell[i][j].getvis() ) Flag[i] = 1 ;
					}
					if( !flag ){
						for( j = 0 ; j < 9 ; j ++ )
							cell[i][j].setvis( 0 ) ;
						Flag[i] = 0 ;
						score ++ ;
					}
				}
				for( i = 14 ; i >= 0 ; i -- )
				{
					if( Flag[i] == 0 )
					{
						for( j = i - 1 ; j >= 0 ; j -- )
							if( Flag[j] == 1 ) break ;
						if( j < 0 ) continue ;
						for( int k = 0 ; k < 9 ; k ++ )
						{
							bool vis = cell[j][k].getvis() ;
							cell[i][k].setvis( vis ) ; 
							cell[j][k].setvis( 0 ) ; 
							Flag[i] = 1 ; 
							Flag[j] = 0 ;
						}
					}
				}
				break ;
			}

			int Count = 0 ;
			while( !kbhit() )
			{
				Count ++ ; 
				if( Count == MAX ) break ; 
			}
			
			if( Count == MAX )
			{
				int y = grid.gety() ;
				grid.sety( y + 1 ) ;
				continue ; 
			}
			key = getch() ;
			if( key == 72 )          //up
			{
				ChangeAble() ; 
				if( grid.getchangeable() )
					grid.Changenum() ;
			}
			else if( key == 80 )    // down 
			{
				while( check( 0 ) ) 
				{
					int y = grid.gety() ; 
					grid.sety( y + 1 ) ;
				}
			}
			else if( key == 75 )    // left 
			{
				if( check( 1 ) ) 
				{
					int x = grid.getx() ;
					grid.setx( x - 1 ) ;
				}
			}
			else if( key == 77 )    // right 
			{
				if( check( 2 ) )
				{
					int x = grid.getx() ;
					grid.setx( x + 1 ) ;
				}
			}
			key1 = key ;
		}
	}


	getch();
	closegraph();  
	cout << "你的成绩是:" << score << endl ; 
    return 0 ;
}


分享到:
评论

相关推荐

    用VB做的俄罗斯方块

    用VB做俄罗斯方块 步骤很明确清晰 易懂,非常适合初学者学习 来吧~~开发一个属于自己的经典小游戏

    c#写的一个俄罗斯方块

    自己写的一个经典的俄罗斯方块,资源是网上找的,是比较经典的一款。

    自己做的简单的俄罗斯方块

    vc做的一个俄罗斯方块 挺简单的 思路完全是自己想出来的

    html做的俄罗斯方块

    自己用html做的一个俄罗斯方块《==========下下去学习吧

    wpf做的俄罗斯方块

    自己用wpf做的俄罗斯方块,适合初学者,实现简单功能

    java俄罗斯方块(自己编写)

    自己编写的,希望大家多给点意见!!直接发到我的邮箱也行,邮箱:jia-jianhong@163.com

    俄罗斯方块网页版JS

    俄罗斯方块2、键盘操作:系统缺省设置使用右边的窗口,用光标操作,"←"左移一格;"→"右移一格;"↑"旋转方块;↓ 方块丢下(方块下落到底),"End"健可以一格格的下落,用户还可以自定义习惯的按键来操作游戏。 3、计分...

    俄罗斯方块MFC C/C++编写

    俄罗斯方块,MFC,C/C++编写,是学习MFC的好帮手。编译环境是VC++6.0,可以运行,重新开始。自己改写的别人的,亲测有效,增加了重新开始功能。

    pygame做的俄罗斯方块游戏

    自己用pygame开发的做的俄罗斯方块游戏,支持单机、支持人机对战、支持多语言,目前支持中英文两种语言,可以灵活增加其他语言,另外有自定义了按钮控件和表单组件,可以方便使用和扩展。

    java俄罗斯方块游戏

    俄罗斯方块是个老幼皆宜的小游戏,它实现由四块正方形的色块组成,然后存储在一个数组的四个元素中,计算机随机产生不同七种类型的方块,根据计算机时钟控制它在一定的时间不停的产生,用户根据键盘的四个方向键控制...

    俄罗斯方块源代码加详细注释

    属于自己的俄罗斯方块,注释很详细了,包含音效文件,新手可以拿去参考制作自己的俄罗斯方块游戏。

    自己用C+SDK写的俄罗斯方块

    初学,用C+SDK写了个俄罗斯方块,最简单的功能,希望有人能指教指教。邮箱lihongtaill@163.com

    自己在游戏学院时做的俄罗斯方块

    自己在游戏学院的时候做的,俄罗斯方块~~, 虽然没做完 但已经可以玩老~~ 使用D3D做的图形

    自己做的俄罗斯方块--大家帮看下

    自己暑假做的C#俄罗斯方块--大家帮看下

    俄罗斯方块MFC俄罗斯方块带注解

    本人自己用c++,MFC做的俄罗斯方块

    VC 俄罗斯方块 VC 俄罗斯方块

    这是一个用VC写的俄罗斯方块小程序,对初学者来说非常有用,如果有什么问题的可以联系我,里面的积分和速度的控制还没有完成,有兴趣的朋友可以自己完成。cumthyw@126.com

    C语言编程实现俄罗斯方块代码

    这是我自己编写的利用C语言实现俄罗斯方块游戏的源代码,简单实用,大家可以下载后自己修改变成自己的源代码

    java 俄罗斯方块 毕业设计 开题报告

    自己做的开题报告,java 俄罗斯方块 毕业设计。。。不谢

    自己做的JAVA版俄罗斯方块

    绝对可运行 开发环境NETBEANS6.8 自己做的JAVA版俄罗斯方块 自己做的JAVA版俄罗斯方块 自己做的JAVA版俄罗斯方块 自己做的JAVA版俄罗斯方块 自己做的JAVA版俄罗斯方块 绝对可运行

Global site tag (gtag.js) - Google Analytics