当前位置:文档之家› 俄罗斯方块源程序

俄罗斯方块源程序

俄罗斯方块源程序
俄罗斯方块源程序

//这是一个Windows程序,简单的俄罗斯方块程序。最下面附有截图

//这是一个Win32 Application程序

// ToyBricks.cpp : Defines the entry point for the application.

//

#include "stdafx.h"

#include

#include

#include

#define CELL 15 // 【方格】的边长(pix)

#define W 22 // 游戏区宽(22个【方格】边长)

#define H 30 // 游戏区高(30个【方格】边长)

#define MS_NEWBLOCK WM_USER+1 // 消息ID,产生新的【方块】

#define MS_DRAW WM_USER+2

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);/*窗口过程处理*/

int WINAPI WinMain ( HINSTANCE hInstance, //当前实例句柄

HINSTANCE hPrevInstance, //前一实例句柄

PSTR szCmdLine, //指向程序命令行参数的指针

int iCmdShow) //应用程序开始执行窗口时显示方式用int类型标志

{

static char AppName[]="ToyBrick";//定义一个静态字符数组保存字符串"ToyBrick"(机应用程序名)

HWND hwnd; //定义一个窗口句柄

MSG msg; //定义一消息结构体变量

WNDCLASSEX wndclass; //定义一窗口类结构变量,包含窗口类全部信息

int iScreenWide; //定义屏幕显示宽度

wndclass.cbSize=sizeof(wndclass); //窗口类对象大小

wndclass.style=CS_HREDRAW|CS_VREDRAW; //窗口类对象风格

wndclass.lpfnWndProc=WndProc; //窗口处理函数为WndProc

wndclass.cbClsExtra=0; //窗口类无扩展

wndclass.cbWndExtra=0; //窗口类实例没有扩展

wndclass.hInstance =hInstance; //当前实例句柄

wndclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);//窗口最小化图标为默认图标

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);//窗口当前光标为箭头光标

wndclass.hbrBackground=(HBRUSH)GetStockObject (BLACK_BRUSH);//获得当前背景设置为黑色

wndclass.lpszMenuName=NULL; //窗体菜单名为空

wndclass.lpszClassName=AppName;//应用程序名

wndclass.hIconSm=LoadIcon(NULL, IDI_APPLICATION);//为应用程序加载图标if(!RegisterClassEx (&wndclass)) return FALSE; //注册一个窗口类

// 获取显示器分辨率的X值iScreenWide,将程序窗口置于屏幕中央

iScreenWide=GetSystemMetrics (SM_CXFULLSCREEN);

//创建窗口

hwnd =CreateWindow (AppName,

"俄罗斯方块",

WS_MINIMIZEBOX|WS_SYSMENU ,

iScreenWide/2-W*CELL/2, CELL,

(W+1)*CELL-8, H*CELL,

NULL, NULL,

hInstance,

NULL );

if(!hwnd) return FALSE; //没有获得窗口句柄,返回假

ShowWindow (hwnd,iCmdShow); //显示窗口

UpdateWindow (hwnd); //更新窗口

MessageBox(hwnd,"开始游戏","开始",MB_OK);//显示Message

SendMessage(hwnd,MS_NEWBLOCK,0,0);//向当前窗口发送消息

SetTimer (hwnd, 1, 550,NULL);//设置下落时间

while (GetMessage (&msg, NULL, 0, 0))//进入消息循环

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return msg.wParam;

}

// 函数DrawRact: 画【正方形】

// 参数: 设备环境句柄和【正方形】的四角坐标

void DrawRect (HDC hdc, int l, int t, int r, int b)

{

MoveToEx (hdc, l, t, NULL);

LineTo (hdc, r, t);

LineTo (hdc, r, b);

LineTo (hdc, l, b);

LineTo (hdc, l,t);

}

// 函数DrawCell: 画【方格】

// 参数: 设备环境句柄和【方格】的四角坐标

// 每个方格由内外两个【正方形】(DrawCell)画成,使其有立体感

void DrawCell(HDC hdc, int l, int t, int r, int b)

{

DrawRect(hdc,l+1, t+1, r-1, b-1);

DrawRect(hdc,l+3, t+3, r-3, b-3);

}

// 函数DrawBlock: 画【方块】

// 参数: 设备环境句柄和【方块】中四个【方格】在游戏区域中的位置// 每个【方块】由四个【方格】组成7种不同的形状

void DrawBlock (HDC hdc, int block[4][2])

{

int i;

HPEN hpen;

hpen =CreatePen (PS_SOLID,1,RGB(255,255,255));

SelectObject (hdc,hpen);

for(i=0; i<4; i++)

DrawCell (hdc, (block[i][0]-1)*CELL, (block[i][1]-1)*CELL, //....

block[i][0]*CELL, block[i][1]*CELL);

DeleteObject (hpen);

}

// 函数Cover: 清除原来位置的【方块】

// 参数: 设备环境句柄和待清除的【方块】

// 清除【方块】即在该【方块】的每个【方块】处画一个正方形的黑块void Cover (HDC hdc, int org[4][2])

{

int i;

SelectObject (hdc, (HBRUSH)GetStockObject (BLACK_BRUSH));

for(i=0; i<4; i++)

Rectangle ( hdc, (org[i][0]-1)*CELL, (org[i][1]-1)*CELL, //.....

org[i][0]*CELL, org[i][1]*CELL);

}

// 窗口过程函数WndProc

LRESULT CALLBACK WndProc ( HWND hwnd,

UINT iMsg,

WPARAM wParam,

LPARAM lParam )

{

int i,j,k,lines,r;

static int top, sel, flag;

static int cells[W+2][H]; // 控制游戏的【方格矩阵】static int org[4][2], block[4][2]; // 【方块】

HDC hdc;

HPEN hpen;

PAINTSTRUCT ps;

switch (iMsg)

{

case WM_CREATE:

top=H-1;

// 将第一列和最后一列【方格】置1,控制【方块】不超出游戏区域for(i=0; i

{ cells[0][i]=1; cells[W+1][i]=1; }

// 将最底下一行【方格】置1,控制【方块】不超出游戏区域

for(i=0; i

// 其他【方格】置0,游戏方块只能在这里移动

for(i=1; i<=W; i++)

for(j=0; j

cells[i][j]=0;

return 0;

case MS_NEWBLOCK:

flag=0; // flag表示【方块】旋转了几次

for(i=top; i

{

lines =0;

// 循环语句检查是否有某一行全部被【方格】都填满

for(j=1; j<=W+1; j++)

if(! cells[j][i])

{ lines=1; break; }

// 若该行被填满,则将上一行的填充状态复制到该行,依此类推

// 即从该行开始,所有的【方格】都下移一行

if(!lines)

{ for(j=1;j

for(k=i; k>=top; k--)

cells[j][k]=cells[j][k-1];

top++;

InvalidateRect (hwnd, NULL, TRUE);

}

}

// 产生随机数0~7,分别代表【方块】的7种形状

srand( (unsigned)time( NULL ) );

sel =rand()%7;

//【方块】形状初始化

//【方块】的形状由其每个【方格】的位置决定

// 游戏区宽W=22,block[?][0]=9/10/11/12,block[?][1]=0/1/2 // 这样【方块】初始位置在游戏区的最顶部的中央

switch(sel)

{

case 0:

// **

// **

org[0][0]=block[0][0] =10; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =11; org[1][1]=block[1][1] =0;

org[2][0]=block[2][0] =10; org[2][1]=block[2][1] =1;

org[3][0]=block[3][0] =11; org[3][1]=block[3][1] =1;

break;

case 1:

// ****

org[0][0]=block[0][0] =9; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =10; org[1][1]=block[1][1] =0;

org[2][0]=block[2][0] =11; org[2][1]=block[2][1] =0;

org[3][0]=block[3][0] =12; org[3][1]=block[3][1] =0;

break;

case 2:

//*

//**

// *

org[0][0]=block[0][0] =10; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =10; org[1][1]=block[1][1] =1;

org[2][0]=block[2][0] =11; org[2][1]=block[2][1] =1;

org[3][0]=block[3][0] =11; org[3][1]=block[3][1] =2;

break;

case 3:

// *

//**

//*

org[0][0]=block[0][0] =11; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =11; org[1][1]=block[1][1] =1;

org[2][0]=block[2][0] =10; org[2][1]=block[2][1] =1;

org[3][0]=block[3][0] =10; org[3][1]=block[3][1] =2;

break;

case 4:

//*

//*

//**

org[0][0]=block[0][0] =10; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =10; org[1][1]=block[1][1] =1;

org[2][0]=block[2][0] =10; org[2][1]=block[2][1] =2;

org[3][0]=block[3][0] =11; org[3][1]=block[3][1] =2;

break;

case 5:

// *

// *

//**

org[0][0]=block[0][0] =10; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =10; org[1][1]=block[1][1] =1;

org[2][0]=block[2][0] =10; org[2][1]=block[2][1] =2;

org[3][0]=block[3][0] =9; org[3][1]=block[3][1] =2;

break;

case 6:

// *

//***

org[0][0]=block[0][0] =10; org[0][1]=block[0][1] =0;

org[1][0]=block[1][0] =9; org[1][1]=block[1][1] =1;

org[2][0]=block[2][0] =10; org[2][1]=block[2][1] =1;

org[3][0]=block[3][0] =11; org[3][1]=block[3][1] =1;

break;

default:

SendMessage (hwnd, MS_NEWBLOCK, 0, 0);

break;

}

return 0;

case WM_TIMER:

// 每个时间节拍【方块】自动下移一行

for(i=0; i<4; i++)

block[i][1]++;

// 检查【方块】下移是否被档住,即判断下移后新位置是否有【方格】for(i=0; i<4; i++)

if(cells[ block[i][0] ][ block[i][1] ])

{

for(i=0; i<4; i++)

cells[ org[i][0] ][ org[i][1] ]=1;

if(top>org[0][1]-2) top=org[0][1]-2;

if (top<1)

{

KillTimer (hwnd, 1);

MessageBox (hwnd, "游戏结束,即将退出!", "退出", MB_OK);

PostQuitMessage (0);

}

SendMessage (hwnd, MS_NEWBLOCK, 0, 0);

return 0;

}

SendMessage (hwnd, MS_DRAW, 0, 0);

return 0;

// 响应键盘控制

case WM_KEYDOWN:

r=0;

switch((int)wParam)

{

case VK_LEFT:

for(i=0; i<4; i++)

block[i][0]--;

break;

case VK_RIGHT:

for(i=0; i<4; i++)

block[i][0]++;

break;

case VK_DOWN:

for(i=0; i<4; i++)

block[i][1]++;

break;

// 按[向上键],【方块】顺时针旋转

//【方块】的旋转不是真正的旋转,而是根据不同的【方块】形状和

// 该【方块】旋转过的次数来移动其中的一个或几个【方格】,从而

// 达到旋转的效果这样做很复杂,算法不够理想,但是能够保持【方

// 块】旋转时的重心比较稳定。

case VK_UP:

r=1;

flag++; //【方块】旋转加1

switch(sel) // sel代表当前【方块】的形状

{

case 0: break;

case 1:

flag =flag%2;

for(i=0; i<4; i++)

{

block[i][(flag+1)%2] =org[2][(flag+1)%2];

block[i][flag] =org[2][flag]-2+i;

}

break;

case 2:

flag =flag%2;

if(flag)

{ block[0][1] +=2; block[3][0] -=2; } else

{ block[0][1] -=2; block[3][0] +=2; } break;

case 3:

flag =flag%2;

if(flag)

{ block[0][1] +=2; block[3][0] +=2; } else

{ block[0][1] -=2; block[3][0] -=2; }

break;

case 4:

flag=flag%4;

switch(flag)

{

case 0:

block[2][0] +=2; block[3][0] +=2;

block[2][1] +=1; block[3][1] +=1;

break;

case 1:

block[2][0] +=1; block[3][0] +=1;

block[2][1] -=2; block[3][1] -=2;

break;

case 2:

block[2][0] -=2; block[3][0] -=2;

block[2][1] -=1; block[3][1] -=1;

break;

case 3:

block[2][0] -=1; block[3][0] -=1;

block[2][1] +=2; block[3][1] +=2;

break;

}

break;

case 5:

flag=flag%4;

switch(flag)

{

case 0:

block[2][0] +=1; block[3][0] +=1;

block[2][1] +=2; block[3][1] +=2;

break;

case 1:

block[2][0] +=2; block[3][0] +=2;

block[2][1] -=1; block[3][1] -=1;

break;

case 2:

block[2][0] -=1; block[3][0] -=1;

block[2][1] -=2; block[3][1] -=2;

break;

case 3:

block[2][0] -=2; block[3][0] -=2;

block[2][1] +=1; block[3][1] +=1;

break;

}

break;

case 6:

flag =flag%4;

switch(flag)

{

case 0:

block[0][0]++; block[0][1]--;

block[1][0]--; block[1][1]--;

block[3][0]++; block[3][1]++;

break;

case 1:

block[1][0]++; block[1][1]++; break;

case 2:

block[0][0]--; block[0][1]++; break;

case 3:

block[3][0]--; block[3][1]--; break;

}

break;

}

break;

}

// 判断【方块】旋转后新位置是否有【方格】,若有,则旋转取消

for(i=0; i<4; i++)

if(cells[ block[i][0] ][ block[i][1] ])

{

if(r) flag +=3;

for(i=0; i<4; i++)

for(j=0; j<2; j++)

block[i][j]=org[i][j];

return 0;

}

SendMessage(hwnd, MS_DRAW, 0, 0);;

return 0;

// 清除当前【方块】,并在新的位置重新绘制【方块】

case MS_DRAW:

hdc =GetDC (hwnd);

Cover (hdc, org);

for(i=0; i<4; i++)

for(j=0; j<2; j++)

org[i][j]=block[i][j];

DrawBlock (hdc,block);

ReleaseDC (hwnd, hdc);

return 0;

// 按照【方格矩阵】重绘游戏区域的【方格】

case WM_PAINT:

hdc =BeginPaint (hwnd, &ps);

hpen =CreatePen (PS_SOLID,1,RGB(255,255,255));

SelectObject (hdc,hpen);

for (i=top; i

for(j=1; j<=W; j++)

if( cells[j][i] )

DrawCell (hdc, (j-1)*CELL, (i-1)*CELL, j*CELL, i*CELL);

DeleteObject (hpen);

EndPaint (hwnd, &ps);

return 0;

case WM_DESTROY:

KillTimer (hwnd, 1);

PostQuitMessage (0);

return 0;

}

return DefWindowProc (hwnd, iMsg, wParam, lParam); }

俄罗斯方块游戏的开发需求分析

俄罗斯方块游戏的开发 组长:XXX 组员:XXX XXX XXX XXX 05软件工程一班 一、课程设计的目的和意义 俄罗斯方块游戏是一个经典的小游戏,由于它简单有趣,因而得到了广泛的流行,男女老幼都适合。而俄罗斯方块游戏的设计工作复杂且富有挑战性,它包含的内容多,涉及的知识广泛,与图形界面联系较大,包括界面的显示与更新、数据收集等,在设计的过程中,必将运用到各方面的知识,这对于visualbasi语言设 计者而言,是个很好的锻炼机会。 二、系统功能设计 本系统主要设计以下几种功能 1、游戏难度选择功能 游戏难度选择界面设置在程序运行开始时,一共有九种难度供玩家选择,每选一级难度,都会相应地显示出代表该难度的图片。开始时不设置任何默认的难度,如果玩家不选难度直接按“Enter”进入,将会弹出提示框,提示其先选难度再 进入。 2、方块下落、变形功能 在整个俄罗斯方块游戏中,方块的设计是核心。这里设计了一个方块类:Square(),用来生成方块以及实现块的左移、右移、向下、变形、重画、同步显 示、初始化新块等。 3、自动升级功能 当分数累积到一定大小时,系统将自动为玩家提高难度。这里设置了每消除10行方块,就增加一级难度。当难度增加的时候,方块会相应地改变颜色,以作为 对玩家的提示。 4、游戏音乐功能 游戏开始音乐就自动播放,游戏暂停与结束时音乐相应消除。 5、获取帮助功能 这里设置了一个类,用来显示帮助,按F1键就能弹出窗口,显示游戏规则。

三、系统功能设计分析 俄罗斯方块游戏根据功能的不同,设置了如下12个类:Square,Command, GameArea,GameSetting,GameOver,Help,ImagePanel,JieMian,MyPanel, MyTimer,PlayMidi,WinListener,每个类的描述如下: 1、Square,方块类。这个类中定义了生成方块的方法,用二维数组int[][]pattern,存放7种方块的四种状态。在构造方法中以随机的形式生成方块,同时提供了以下几种方法:reset(),leftTurn(),leftMove(),rightMove(),fallDown(),assertValid(int t,int s,int row,int col),dispBlock(int s)。分别实现方块的重画、翻转、 左移、右移、下落、同步显示等功能。 2、Command,处理控制类。这是一个实现ActionListener接口的类,主要处理点击按钮事件。类中定义了三个int型变量:button_play,button_quit,button_pause,和一个boolean型的变量:pause_resume,并赋值。在GameArea类中通过事件响应,在按钮执行方法中调用其值,使用switch语句,根据不同按钮不同的值, 来响应不同的事件。 3、GameArea,游戏界面类。GameArea继承了JFrame,是俄罗斯方块的主要游 戏界面。这个类定义了GameSetting类的gameScr对象和ImagePanel类的imagepanel对象作为游戏区域面板和控制区域面板。在游戏区域,主要是根据相应格子的设置标志来显示相应的图形图片,这样就实现了俄罗斯方块的实时显 示。 4、GameSetting,游戏画布类。这个类生成的对象将作为游戏界面的方块下落区域,画布的设置为15行10列,当中的方格边长为30,类中还定义了一个二维数组int[][]scrArr作为屏幕数组,表示每一个方格。游戏区域中每一个方格是否存在游戏方块是由该方格的值来决定的,如果该方格的值为1,则表示该方格中存在游戏方块;如果该方格中的值为0,则表示该方格中不存在游戏方块,因此二维数组用于记录游戏区域中每个小方格的值。此外,类中还定义了画方块的方法,根据不同的难度画出不同颜色的方块。单击Play按钮时,系统调用initScr()方法,初始化屏幕,将屏幕数组清零。当满足满行删除的条件时,系统调用deleteFullLine()方法,进行删行加分,而且每删除十行,难度自动增加一级,方块颜色改变,并在难度显示框中相应显示。 5、GameOver,游戏结束弹出提示框类。当游戏结束时,系统弹出提示,包括玩 家分数以及询问玩家要继续游戏还是退出。 6、Help,帮助类。在游戏界面,按F1键,弹出提示窗口,获取帮助。 7、ImagePanel,背景图片类。这个类继承了JPanel类,用来作为游戏界面中控 制区域的容器,并添加图片。 8、JieMian,主界面类。这个类继承了JPanel类,作为游戏的第一个界面,也是难度选择界面。定义了9个单选按钮,当玩家未选任何难度就按Enter时,系统会弹出一个提示框,提示玩家先选难度再进入。 9、MyPanel,重写MyPanel类,使Panel的四周留空间。

C语言俄罗斯方块游戏源代码

/*学无止境*/ #include #include #include #define ESC 27 #define UP 328 #define DOWN 336 #define LEFT 331 #define RIGHT 333 #define BLANK 32 #define BOTTOM 2 #define CANNOT 1 #define CAN 0 #define MAX 30 #define F1 315 #define ADD 43 #define EQUAL 61 #define DEC 45 #define SOUNDs 115 #define SOUNDS 83 #define PAUSEP 80 #define PAUSEp 112

void Init(); void Down(); void GoOn(); void ksdown(); void Display(int color); void Give(); int Touch(int x,int y,int dx,int dy); int GeyKey(); void Select(); void DetectFill(); void GetScores(); void Fail(); void Help(); void Quit(); void DrawBox(int x,int y,int Color); void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch); void DrawNext(int Color); int Heng=12,Shu=20; /*横竖*/ int Position[MAX][MAX]; int middle[MAX][MAX]; int ActH,ActS;

C++俄罗斯方块代码(北邮版)

#include #include #include #include #include #include "colorConsole.h" //老师的文件 void begin(); //开始游戏 void frame(); //边框设定 int * getblocks(); //方块产生 void move(int line); //移动 void drawblocks(int line); //方块显示 void clearsquare(int line); //方块擦出 void turn(int line); //方块旋转 bool isavailable(int line); //判断是否能下落 void remember(int line); //记忆方块位置 void deleteline(int line); //方块满一行消除 bool ifgameover(); //判断是否游戏结束 void end(); //游戏结束 #define up 72 #define down 80 #define left 75 #define right 77 #define esc 27 HANDLE handle; int a1[4][4]={{1},{1,1,1}}; //七种方块的二维数组 int a2[4][4]={{0,1},{1,1,1}}; int a3[4][4]={{1,1},{0,1,1}}; int a4[4][4]={{0,0,1},{1,1,1}}; int a5[4][4]={{0,1,1},{1,1}}; int a6[4][4]={{1,1,1,1}}; int a7[4][4]={{1,1},{1,1}}; int row=0; //列数 int score=0; int level=0; int * block1=NULL; int * block2=NULL; int * block3=NULL; int coordinate[12][18]={0}; //坐标数组,边框12*18(最后一行,两边边框计算在内) int judge=0;

俄罗斯方块C语言代码

【转载】88行代码实现俄罗斯方块游戏(含讲解) 来源:https://www.doczj.com/doc/ab11100796.html,/p/8 在正式阅读本文之前,请你记得你应该用娱乐的心态来看, 本代码所使用到的技巧,在工作了的人眼里会觉得很纠结,很蛋疼,很不可理喻,很丑, 注意,是你蛋疼,不关我的事 通常,写一个俄罗斯方块,往往动不动就几百行,甚至上千行,而这里只有88行 正所谓头脑风暴,打破常规。这里将使用很多不平常的手段来减少代码 以下是Win-TC可以成功编译并执行的代码(代码保证单行长度不超过80字符,如果你是Win7系统,那请看后文): 程序代码: #include"graphics.h" #include #include int gcW = 20, gcColor[] = {DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA,MAGENTA, YELLOW}; struct tetris { int _pool[16][32], (*pool)[32], tmap[8][4][16]; int x, y, s, st, t; }gt; void trsInit() { int sp[8][4] = {{15,4369},{23,785,116,547},{71,275,113,802}, {39,305,114,562},{54,561},{99,306},{51,51},{-1}}; int *p, i, j, b; for (p = sp[0]; *p >= 0; ++p) if ( *p == 0 ) *p = p[-2]; gt.pool = >._pool[4]; for (j = 0; j < 7; ++j) for (i = 0; i < 4; ++i) for (b = 0; b < 16; ++b) gt.tmap[j+1][i][b] = (sp[j][i] & 1) * (j + 1), sp[j][i] >>= 1; memset(gt._pool, -1, sizeof(gt._pool));

C语言课程设计俄罗斯方块源代码

1、新建“.h”头文件,将“头文件” 代码粘贴至其中, 2、新建“.c”源文件,将“源代码” 代码粘贴到其中。 3、新建空白工程,将头文件和源代码 添加进去,调试使用。 //头文件 //1.自定义枚举类型,定义7种形态的游戏方块 typedef enum tetris_shape { ZShape=0, SShape, LineShape, TShape, SquareShape, LShape, MirroredLShape }shape; //2.函数声明 //(1)操作方块函数 int maxX();//取得当前方块的最大x坐标 int minX();//取得当前方块的最小x坐标 void turn_left();//当前方块逆时针旋转90度 void turn_right(); int out_of_table(); void transform(); int leftable(); int rightable(); int downable(); void move_left(); void move_right(); //(2)操作游戏桌面的函数 int add_to_table();

void remove_full(); //(3)控制游戏函数 void new_game(); void run_game(); void next_shape(); int random(int seed); //(4)绘图函数 void paint(); void draw_table(); //(5)其他功能函数 void key_down(WPARAM wParam); void resize(); void initialize(); void finalize(); //(6)回调函数,用来处理Windows消息 LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM); //源代码 //1.文件包含 #include #include #include #include"tetris.h" //2.常量定义 #define APP_NAME "TETRIS" #define APP_TITLE "Tetris Game" #define GAMEOVER "GAME OVER" #define SHAPE_COUNT 7 #define BLOCK_COUNT 4 #define MAX_SPEED 5 #define COLUMS 10 #define ROWS 20 #define RED RGB(255,0,0)

C.C++语言-俄罗斯方块源码

注意:本源代码包含头文件,VC6.0请自行下载库文件包,解决没有库文件的问题 环境:WINDOWS7 VC6.0 程序清单:库文件MYFILE.H /****************************************************************************** ********************* File Name : MYFILE.H Copyright : Module Name : CPU : Intel i7 RTOS : Creat Date : 2017/1/13 Author : Yang Abstract Description: C++、C实用函数 ******************************************************************************* *********************/ #ifndef _MYFILE_ #define _MYFILE_ #include #include void introduce() { printf("欢迎使用!MYFILE.H\n"); } /*********************************C++常用类******************************/ template //栈 class STACK { private: int top; T_STACK stackspace[100]; public: STACK() { top =-1; } void PUSH(T_STACK x) {

俄罗斯方块完整源代码

//不多说,直接可以拷贝下面的东西,就可以运行。 package day04; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import https://www.doczj.com/doc/ab11100796.html,ng.String.*; import https://www.doczj.com/doc/ab11100796.html,ng.*; import java.io.*; public class ERSBlock extends JPanel implements ActionListener,KeyListener//应该是继承JPanel { static Button but[] = new Button[6]; static Button noStop = new Button("取消暂停"); static Label scoreLab = new Label("分数:"); static Label infoLab = new Label("提示:"); static Label speedLab = new Label("级数:"); static Label scoreTex = new Label("0"); static Label infoTex = new Label(" "); static Label speedTex = new Label("1");

static JFrame jf = new JFrame(); static MyTimer timer; static ImageIcon icon=new ImageIcon("resource/Block.jpg"); static JMenuBar mb = new JMenuBar(); static JMenu menu0 = new JMenu("游戏 "); static JMenu menu1 = new JMenu("帮助 "); static JMenuItem mi0 = new JMenuItem("新游戏"); static JMenuItem mi1 = new JMenuItem("退出"); static JMenuItem mi1_0 = new JMenuItem("关于"); static JDialog dlg_1; static JTextArea dlg_1_text = new JTextArea(); static int startSign= 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static String butLab[] = {"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"}; static int game_body[][] = new int[19][10]; static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置

俄罗斯方块源代码

1. using System; using System.Collections.Generic; using System.Text; using System.Drawing;//add namespace俄罗斯方块 { public class Block { private short width; private short height; private short top; private short left; private int ID; //方块部件的ID public int[,] shape;//存储方块部件的形状,0为空白,1为有砖块 public Block()//构造函数 { Random randomGenerator = new Random(); int randomBlock = randomGenerator.Next(1, 6);//产生1—4的数 this.ID = randomBlock; switch (this.ID) { case 1: //横条形 this.Width = 4; this.Height = 1; this.Top = 0; this.Left = 3; shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[3, 0] = 1; break; case 2://正方形 this.Width = 2; this.Height = 2; this.Top = 0; this.Left = 4; // Creates the new shape for this block. shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[0, 1] = 1; shape[1, 0] = 1;shape[1, 1] = 1; break; case 3://T形 this.Width = 3; this.Height = 3; this.Top = 0; this.Left = 4; // Creates the new shape for this block. shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[1, 1] = 1; shape[1, 2] = 1; break; case 4://L形 this.Width = 2; this.Height = 3; this.Top = 0; this.Left = 4;

俄罗斯方块C语言程序设计报告

C语言课程设计报告 俄罗斯方块程序设计报告 一、问题描述 俄罗斯方块(Tetris,俄文:Тетрис)是一款电视游戏机和掌上游戏机游戏,它由俄罗斯人阿列克谢·帕基特诺夫发明,故得此名。俄罗斯方块的基本规则是移动、旋转和摆放游戏自动输出的各种方块,使之排列成完整的一行或多行并且消除得分。 在本次设计中,要求支持键盘操作和若干种不同类型方块的旋转变换,并且界面上显示下一个方块的提示以及当前的玩家的得分,随着游戏的进行,等级越高,游戏难度越大,即方块的下落速度越快,相应的等级,等级越高,为玩家提供了不同的选择。 二、功能分析 I、俄罗斯方块游戏需要解决的问题包括: ⑴、随机产生方块并自动下移 ⑵、用Esc键退出游戏 ⑶、用键变体 ⑷、用键和键左右移动方块 ⑸、用空格键使游戏暂停

⑹、能正确判断满行并消行、计分、定级别 ⑺、设定游戏为不同级别,级别越高难度越大 II、俄罗斯方块游戏需要设计的功能函数包括: ⑴、声明俄罗斯方块的结构体 ⑵、函数原型声明 ⑶、制作游戏窗口 ⑷、制作俄罗斯方块 ⑸、判断是否可动 ⑹、随机产生俄罗斯方块类型的序号 ⑺、打印俄罗斯方块 ⑻、清除俄罗斯方块的痕迹 ⑼、判断是否满行并删除满行的俄罗斯方块 三、程序设计 1、程序总体结构设计 (1)、游戏方块预览功能。在游戏过程中,游戏界面右侧会有预览区。由于在此游戏中存在多种不同的游戏方块,所以在游戏方块预览区域中显示随机生成的游戏方块有利于游戏玩家控制游戏的策略。 (2)、游戏方块控制功能。通过各种条件的判断,实现对游戏方块的左移、右移、自由下落、旋转功能,以及行满消除行的功能。 (3)、游戏数据显示功能。在游戏玩家进行游戏过程中,需要按照一定的游戏规则给玩家计算游戏分数。例如,消除一行加100分,游戏分数达到一定数量

俄罗斯方块程序代码

//包含头文件 #include #include #include #include #include #include "Tetris.h" //int score=0; //int lever=1; //char scores[10]; //char levers[10]; /* enum cmd { round, //旋转方块 left, //左移方块 right, //右移方块 down, //下一方块 bottom, //方块沉底 quit //离开游戏 }; //定义绘制方块的状态的枚举类型 enum draw { show, //显示方块 hide //抹掉方块 }; //定义俄罗斯方块的结构体 struct block { int dir[4]; //方块的四个旋转的状态 int color; //方块的颜色 }*/ static T_TrsBlockStyle gz_tBlockStyleTab[7] = {/* 口口 口口口口口口口口口口 口口 口口*/ {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, /*

口口口口口口口口 口口口口口口口口 */ {0x0660, 0x0660, 0x0660, 0x0660, BLUE}, /* 口 口口口口口口口 口口口口口口口 口*/ {0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, /* 口 口口口口口口口 口口口口口口口 口*/ {0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, /* 口口 口口口口口口口口 口口口口口口 */ {0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, /* 口口 口口口口口口口口 口口口口口口 */ {0x0360, 0x4620, 0x0360, 0x4620, GREEN}, /* 口口口 口口口口口口口口口口 口口口 */ {0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; /* //定义俄罗斯方块的信息的结构体 struct blockinfo { int id; //7中方块中的哪一种 byte dir:2; //1种方块中四个方向中的哪个 char x,y; //方块的坐标(不是屏幕中的而是自己设置的游戏区域中的)} curblock,nextblock; */ // 定义游戏区 //unsigned char area[width][high] = {0}; //函数声明

JAVA俄罗斯方块源代码

不多说,,直接可以拷贝下面的东西,然后记得把那个BLOCK的名字改成你自己的类名,这个很关键哦,不然是错的可别怪我,呵呵~~ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import https://www.doczj.com/doc/ab11100796.html,ng.String.*; import https://www.doczj.com/doc/ab11100796.html,ng.*; import java.io.*; public class Block extends JPanel implements ActionListener,KeyListener//应该是继承JPanel { static Button but[] = new Button[6]; static Button noStop = new Button("取消暂停"); static Label scoreLab = new Label("分数:"); static Label infoLab = new Label("提示:"); static Label speedLab = new Label("级数:"); static Label scoreTex = new Label("0"); static Label infoTex = new Label(" "); static Label speedTex = new Label("1"); static JFrame jf = new JFrame(); static MyTimer timer; static ImageIcon icon=new ImageIcon("resource/Block.jpg"); static JMenuBar mb = new JMenuBar(); static JMenu menu0 = new JMenu("游戏 "); static JMenu menu1 = new JMenu("帮助 "); static JMenuItem mi0 = new JMenuItem("新游戏"); static JMenuItem mi1 = new JMenuItem("退出"); static JMenuItem mi1_0 = new JMenuItem("关于"); static JDialog dlg_1; static JTextArea dlg_1_text = new JTextArea(); static int startSign = 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static String butLab[] = {"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"}; static int game_body[][] = new int[19][10]; static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置 static boolean downSign = false;//是否落下 static int blockNumber = 1;//砖块的编号 static int gameScore = 0;//游戏分数 static int speedMark = 1;

VC++ 6.0 MFC 俄罗斯方块 自动求解 代码 源程序

#include #include #include #include #define tDown 1 //方块下落定时器的标识(编号) #define tPaint 2 //重绘定时器的标识(编号) #define tDownTime 500 //方块下落一行位置的时间间隔 #define tPaintTime 50 //窗口重绘的时间间隔 #define ROW 24 //地图的行数目(第23行不用) #define COL 14 //地图的列数目(第0列和第13列不用) #define MAX_CLASS 7 //方块形状数目 #define LEN 20 //每个方格大小为20×20像素 #define StartY -1 * LEN + 5 //-15,绘制俄罗斯方块地图时的边界起始位置#define StartX -1 * LEN + 5 //-15 int iDeleteRows = 0; //总共清除的行 int iTotalNum = 0; //总得分 char WindowTxt[100] = "俄罗斯方块游戏自动求解已关闭"; //窗口标题 char s1[] = "关闭", s2[] = "启动"; //用于启动/关闭自动求解功能时显示不同的标题

bool bAuto; //是否自动求解的标志 bool Pause; //是否暂停的标志 int Map[ROW][COL]; //俄罗斯方块的地图(被占据的方格为1,否则为0) int CurrentBox[4][4]; //当前落下的方块 int CurrentY, CurrentX; //当前落下方块的当前位置(指左上角位置) int NextBox[4][4]; //下一个将落下的方块 int Box[MAX_CLASS][4][4] = //7种方块形状 { { {0,0,0,0}, {1,1,1,1}, {0,0,0,0}, {0,0,0,0} }, { {0,0,0,0}, {0,1,0,0}, {1,1,1,0}, {0,0,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,1,0}, {0,0,0,0} }, { {0,0,0,0}, {0,1,1,0}, {1,1,0,0}, {0,0,0,0} }, { {0,1,1,0}, {0,0,1,0}, {0,0,1,0}, {0,0,0,0} }, { {0,1,1,0}, {0,1,0,0}, {0,1,0,0}, {0,0,0,0}

C语言程序设计-俄罗斯方块源程序

其中的主要逻辑有: (1)由于c的随机性函数不好,所以每次游戏开始根据bios时间设置种子。 (2)得分越高,方块下降速度越快(每200分为单位)。 (3)每下落一个方块加1分,每消除一行加10分,两行加30分,三行加70分,四行加150分。初试分数为100分。 游戏控制: up-旋转;空格-下落到底;左右下方向键-控制方向。P-开始或暂停游戏。ESC-退出。 特点: (1)由于tc不支持中文,所以基本都是英文注释。 (2)函数命名尽可能规范的表达其内部处理目的和过程。 (3)代码加上注释仅有577行。(我下载过的两个俄罗斯方块代码一个在1087行,一个在993行,我的比它们代码少)。 (4)除了消除空格时算法比较复杂,其他算法都比较简单易读。 (5)绘图效率和局部代码效率扔有待提高。 (6)FrameTime参数可能依据不同硬件环境进行具体设置,InitGame需要正确的TC路径。 俄罗斯方块源于大约9年前上大一时的一个梦,我们在学习c语言时,我的同寝室友邀请我合作一起完成俄罗斯方块(课外作业性质),但是当时限于我们的水平比较菜和学习状态比较懒散,我们没有完成。大一的时候我在机房里无意发现别人留下的俄罗斯方块程序,运行,老师发现后激动的问我是我写的吗,我惭愧的摇摇头。那时看到别人做c的大程序深感羡慕(自己只是写几十行的程序)。数年后我仍然看到有不同样式的实现,但是我一直没有实现它,知道今天忽然有这个想法去做,算是弥补多年前的遗憾和心愿吧。 --------------------------------------------- Q&A: ---------------------------------------------- Q:我编译时出现错误:fatal error C1083: Cannot open include file: 'bios.h': Nosuch file or directory,该如何解决? A:正文中的代码,是使用Borland公司的TC2.0编译的,编译结果运行在Windows的16位虚拟机上。bi os.h是属于TC的头文件,在VC或者其他编译器中可能没有这个头文件。因此会产生这个错误。 解决办法: (1)可以下载附件中的压缩包,是使用VC6.0进行编译的版本,编译结果是Windows程序,运行在wind ows 32系统上。两者之间的算法完全一致,区别仅仅是绘图时采用的接口不同,TC下采用BGI(进入图形模式),VC下采用的是GDI(使用DC进行绘图)。 (2)下载TC2.0进行编译。 Q:使用TC3.0进行编译时,提示未能初始化图形模式,请使用initgraph函数。 A:这是因为TC3.0的BGI文件的路径和TC2.0有所不同。TC2.0的BGI文件位于TC路径下。而TC3.0的BGI文件位于BGI文件夹中。请把initgame函数中的initgraph函数中的第三个参数设置为正确的路径。例如:initgraph(&..,&..,"C:\\TC\\BGI"); Q:编译后运行时,弹出错误对话框报告16位虚拟机遇到不可执行的指令,点击确定后退出。 A:该问题在某些环境中出现,可能是基于软件或者硬件环境造成,具体原因暂时未知。为避免该问题,请加载附件中的压缩包VC6.0版本。

C#俄罗斯方块(附代码)

C#俄罗斯方块程序设计 姓名: 学号: 专业:计算机科学与技术学院 班级:级班 时间:2009—2010年第一学期 该实验制作的是小游戏----俄罗斯方块 1.可实现以下基本功能: 用户可自定义添加或删除方块样式及颜色; 用户可自定义修改游戏背景颜色及按键设置。 2.另增加了几个功能: 按键设置改变后点击保存,会弹出对话框提示“保存成功”; 点击“开始”运行游戏,背景音乐自动播放,点击暂停后,背景音乐也随之停止;每消除一行,会有特效声音提示消除成功; 根据消行多少会自动加分并显示。

游戏界面效果图如下: 配置窗体效果图如下:

砖块样式配置效果图如下:

游戏设计分为如下九个部分: 一,新建窗体“配置窗体”(TrmConfig)添加TabControl控件

(1)砖块样式配置 I.abel控件(lblMode) 点击“事件”,选择“Paint” Graphics gp=e.Graphics; gp.Clear(Color.Black); Pen p=new Pen(Color.White); for (int i=31;i<155;i=i+31) gp.DrawLine(p,1,i,155,i); for (int i=31;i<155;i=i+31) gp.DrawLine(p,i,1,i,155); SolidBrush s=new SolidBrush(blockColor); for (int x=0;x<5;x++) { for(int y=0;y<5;y++) { if(struArr[x,y]) { gp.FillRectangle(s,31*x+1,31*y+1,30,30); } } } 点击“事件”,选择“MouseClick” private bool[,] struArr=new bool[5,5]; private Color blockColor=Color.Red; -------------------------------------------------------- if (e.Button!=MouseButtons.Left) return; int xPos,yPos; xPos=e.X/31; yPos=e.Y/31; struArr[xPos,yPos]=!struArr[xPos,yPos]; bool b=struArr[xPos,yPos]; Graphics gp=lblMode.CreateGraphics(); SolidBrush s=new SolidBrush(b ? blockColor:Color.Black); gp.FillRectangle(s,31*xPos+1,31*yPos+1,30,30); gp.Dispose(); II.添加ColorDialog控件 添加label(lblColor)控件 点击“事件”,选择“click” colorDialog1.ShowDialog(); blockColor=colorDialog1.Color; lblColor.BackColor=colorDialog1.Color;

C语言俄罗斯方块游戏源代码

/*学无止境*/ #include <> #include <> #include <> #define ESC 27 #define UP 328 #define DOWN 336 #define LEFT 331 #define RIGHT 333 #define BLANK 32 #define BOTTOM 2 #define CANNOT 1 #define CAN 0 #define MAX 30 #define F1 315 #define ADD 43 #define EQUAL 61 #define DEC 45 #define SOUNDs 115 #define SOUNDS 83 #define PAUSEP 80 #define PAUSEp 112

void Init(); void Down(); void GoOn(); void ksdown(); void Display(int color); void Give(); int Touch(int x,int y,int dx,int dy); int GeyKey(); void Select(); void DetectFill(); void GetScores(); void Fail(); void Help(); void Quit(); void DrawBox(int x,int y,int Color); void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch); void DrawNext(int Color); int Heng=12,Shu=20; /*横竖*/ int Position[MAX][MAX]; int middle[MAX][MAX]; int ActH,ActS;

俄罗斯方块C语言代码

#include #include #include #include #include #ifdef__cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif #define MINBOXSIZE 15 /* 最小方块的尺寸*/ #define BGCOLOR 7 /* 背景着色*/ #define GX 200 #define GY 10 #define SJNUM 10000 /* 每当玩家打到一万分等级加一级*/ /* 按键码*/ #define VK_LEFT 0x4b00 #define VK_RIGHT 0x4d00 #define VK_DOWN 0x5000 #define VK_UP 0x4800 #define VK_HOME 0x4700 #define VK_END 0x4f00 #define VK_SPACE 0x3920 #define VK_ESC 0x011b #define VK_ENTER 0x1c0d /* 定义俄罗斯方块的方向(我定义他为4种)*/ #define F_DONG 0 #define F_NAN 1 #define F_XI 2 #define F_BEI 3 #define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/ #define NEXTROW 12 /* 要出的下一个方块的横从标*/ #define MAXROW 14 /* 游戏屏幕大小*/ #define MAXCOL 20 #define SCCOL 100 /*游戏屏幕大显示器上的相对位置*/ #define SCROW 60 int gril[22][16]; /* 游戏屏幕坐标*/

相关主题
文本预览
相关文档 最新文档