#include"resource.h"
#include<windows.h>
#define size_x_child 150
#define size_y_child 80
#define pos_x_parent_init 350
#define pos_y_parent_init 80
#define block_max_horizonal 8
#define block_max_vertical 20
#define pixel 25
#define N_hbitmap_tetris 5
#define N_block_types 8
#define max_block_types 100
#define WM_HANDLE 0x1000
#define WM_BIRTH 0x1001
#define WM_SCORE 0x1002
#define WM_REFRESH 0x1003
#define WM_PAUSE 0x1004
#define WM_FRESH_CHILD 0x1005
#define WM_NEW_GAME 0x1006
#define index_bitmap_new_game N_hbitmap_tetris+2
#define index_bitmap_continue N_hbitmap_tetris+1
#define index_bitmap_pause N_hbitmap_tetris
#define index_bitmap_score N_hbitmap_tetris+3
#define interval 200
#define max_block_count 4
#define N_child 4
int score,color,color_next, n_next,n,m,pos_height,pos_width, occupied[block_max_vertical][block_max_horizonal], work=1;
HINSTANCE hinstance;
HBRUSH hbrush_child,hbrush_score,hbrush_parent;
HDC hdc_tetris[N_hbitmap_tetris+1],hdc_mem;
RECT rect_parent,rect_child;
HWND hwnd_parent, hwnd_child[N_child+1], hwnd_pause,hwnd_new_game;
HBITMAP hbitmap_pause,hbitmap_continue,hbitmap_new_game,hbitmap_score,hbitmap_mem,hbitmap_tetris[N_hbitmap_tetris+1];
struct node
{
int jishu;
int pos_height_start,pos_width_start;
int pos_height[4][max_block_count];
int pos_width[4][max_block_count];
};
struct node s[max_block_types]=
{
1,0,3, 0,0xff,0xff,0xff, 0,0xff,0xff,0xff, 0,0xff,0xff,0xff, 0,0xff,0xff,0xff,0,0xff,0xff,0xff, 0,0xff,0xff,0xff, 0,0xff,0xff,0xff, 0,0xff,0xff,0xff,
4,0,3, 0,0,0,1,-1,0,1,1,0,1,1,1,0,0,1,2,0,1,2,2,1,1,1,0,0,0,1,2,0,1,0,0,
4,0,3,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
4,0,3,0,0,0,0,-1,0,1,2,0,0,0,0,-1,0,1,2,0,1,2,3,0,0,0,0,0,1,2,3,0,0,0,0,
4,0,3,0,1,1,1,0,1,1,2,0,0,0,1,0,1,1,2,1,0,1,2,0,0,1,0,0,1,2,1,1,0,1,1,
4,0,3,0,0,0,1,0,0,1,2,0,1,1,1,-1,0,1,1,0,1,2,0,0,1,1,1,2,0,1,2,0,0,0,1,
4,0,3,0,0,1,1,0,1,1,2,0,0,1,1,0,1,1,2,0,1,1,2,1,0,1,0,0,1,1,2,1,0,1,0,
4,0,3,0,0,1,1,-1,0,0,1,0,0,1,1,-1,0,0,1,1,2,0,1,0,0,1,1,1,2,0,1,0,0,1,1,
};
int get_random_tetris()
{
SYSTEMTIME time_now;
int n1,n2,n3;
GetLocalTime(&time_now);
n1=(rand()^time_now.wMilliseconds)%N_block_types;
n2=(rand()^time_now.wMilliseconds)%N_block_types;
n3=(rand()^time_now.wMilliseconds)%N_block_types;
if(!n2&&!n2&&!n3)return 0;
else return (((n1+n2+n3)%N_block_types)==N_block_types-1)?(N_block_types-1):(((n1+n2+n3)%N_block_types)+1);
}
int get_random_color()
{
SYSTEMTIME time_now;
GetLocalTime(&time_now);
return (rand()^time_now.wMilliseconds)%N_hbitmap_tetris+1;
}
HBITMAP loadbitmap(int index)
{
BITMAP bitmap;
HBITMAP hbitmap_des;
HDC hdc_des;
HBITMAP hbitmap_temp;
HDC hdc_temp=CreateCompatibleDC(GetDC(hwnd_parent));
SelectObject(hdc_des=CreateCompatibleDC(GetDC(hwnd_parent)),hbitmap_des=CreateCompatibleBitmap(GetDC(hwnd_parent),size_x_child,size_y_child));
SelectObject(hdc_temp,hbitmap_temp=LoadBitmap(hinstance,index ));
GetObject(hbitmap_temp,sizeof bitmap,&bitmap);
StretchBlt(hdc_des,0,0,size_x_child,size_y_child,hdc_temp,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
DeleteObject(hdc_des);
DeleteObject(hbitmap_temp);
DeleteObject(hdc_temp);
return hbitmap_des;
}
void init()//init once
{
int a,b,c;
HDC hdc_temp;
HBITMAP hbitmap_temp;
hbitmap_pause=loadbitmap(IDB_BITMAP1+index_bitmap_pause);
hbitmap_continue=loadbitmap(IDB_BITMAP1+index_bitmap_continue);
hbitmap_score=loadbitmap(IDB_BITMAP1+index_bitmap_score);
hbitmap_new_game=loadbitmap(IDB_BITMAP1+index_bitmap_new_game);
hdc_temp=CreateCompatibleDC(GetDC(hwnd_parent));
for(a=1;a<=N_hbitmap_tetris;a++)
{
SelectObject(hdc_temp,hbitmap_temp=LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_BITMAP1+a-1)));
hbitmap_tetris[a]=CreateCompatibleBitmap(GetDC(hwnd_parent),pixel,pixel);
hdc_tetris[a]=CreateCompatibleDC(GetDC(hwnd_parent));
SelectObject(hdc_tetris[a],hbitmap_tetris[a]);
StretchBlt(hdc_tetris[a],0,0,pixel,pixel,hdc_temp,0,0,100,100,SRCCOPY);
DeleteObject(hbitmap_temp);
}
DeleteObject(hdc_temp);
SetRect(&rect_child,0,0,size_x_child,size_y_child);
SetRect(&rect_parent,0,0,pixel*block_max_horizonal,pixel*block_max_vertical);
hbrush_score=CreatePatternBrush(hbitmap_score);
}
void init_local()
{
hbrush_parent=0;
score=0;
memset(occupied,0,sizeof occupied);
work=1;
}
long __stdcall WndProc2 (HWND hwnd, unsigned int msg, unsigned int wparam, long lparam)
{
HDC hdc;
PAINTSTRUCT ps ;
int a,b;
static char buffer[100];
switch(msg)
{
case WM_COMMAND:
if(lparam==hwnd_pause)
{
if((wparam>>16)==BN_CLICKED)
{
SendMessage(hwnd_parent,WM_PAUSE,0,0);
if(work)
{
SetWindowText(hwnd_pause,"CONTINUE");
SendMessage(hwnd_pause,BM_SETIMAGE,IMAGE_BITMAP,hbitmap_pause);
SetFocus(hwnd_parent);
}
else
SendMessage(hwnd_pause,BM_SETIMAGE,IMAGE_BITMAP,hbitmap_continue);
}
}
else if(lparam==hwnd_new_game)
{
SendMessage(hwnd_parent,WM_NEW_GAME,0,0);
SetFocus(hwnd_parent);
}
break;
case WM_REFRESH:
InvalidateRect(hwnd,&rect_child,0);
SendMessage(hwnd,WM_PAINT,0,0);
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
FillRect(hdc,&rect_child,hbrush_child);
if(hwnd==hwnd_child[3])
{
SetBkMode(hdc,TRANSPARENT);
FillRect(hdc,&rect_child,hbrush_score);
wsprintf(buffer,"%d",score);
TextOut(hdc,89,32,buffer,strlen(buffer));
}
else if(hwnd==hwnd_child[4])
{
SetBkMode(hdc,RGB(0,255,255));
wsprintf(buffer,"NEXT:");
TextOut(hdc,0,40,buffer,strlen(buffer));
for(a=0;a<=s[n_next].jishu-1;a++)
BitBlt(hdc,pixel*(2+s[n_next].pos_width[0][a]),pixel*(1+s[n_next].pos_height[0][a]),pixel,pixel,hdc_tetris[color_next],0,0,SRCCOPY);
}
EndPaint (hwnd, &ps) ;
break;
default:
return DefWindowProc (hwnd, msg, wparam, lparam);
break;
}
}
long __stdcall WndProc (HWND hwnd, unsigned int msg, unsigned int wparam, long lparam)
{
int a,b,c;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect_pos;
if(!work&&msg!=WM_NEW_GAME)
{
if(msg==WM_MOVE);
else if(msg==WM_CLOSE);
else if(msg==WM_DESTROY);
else if(msg==WM_PAUSE){SetTimer(hwnd,1,interval,0);work=1;return 0;}
else return DefWindowProc (hwnd, msg, wparam, lparam);
}
switch(msg)
{
/////////////////////////////////////////////////////////
case WM_CREATE:
init();
break;
case WM_PAUSE:
work=0;
break;
case WM_FRESH_CHILD:
for(a=1;a<=N_child;a++)SendMessage(hwnd_child[a],WM_REFRESH,0,0);
break;
case WM_NEW_GAME:
init_local();
n_next=get_random_tetris();
color_next=get_random_color();
PostMessage(hwnd,WM_BIRTH,0,0);
PostMessage(hwnd,WM_REFRESH,0,0);
SetTimer(hwnd,1,interval,0);
for(a=2;a<=N_child;a++)ShowWindow(hwnd_child[a],SW_NORMAL);
break;
case WM_MOVE:
GetWindowRect(hwnd,&rect_pos);
for(a=1;a<=N_child;a++)
MoveWindow(hwnd_child[a],rect_pos.left+pixel*block_max_horizonal+5,20+rect_pos.top+size_y_child*(a-1)+3*(a-1),size_x_child,size_y_child,1);
break;
case WM_HANDLE:
{
int possible=1;
int temp[3];
for(a=0;a<=s[n].jishu-1;a++)occupied[ pos_height+s[n].pos_height[m][a] ][ pos_width+s[n].pos_width[m][a]]=0;
temp[0]=pos_height;
temp[1]=pos_width;
temp[2]=m;
pos_height+=(wparam&0xff);
pos_width+=lparam;
m=(m+(wparam>>8))%4;
for(a=0;a<=s[n].jishu-1;a++)
{
if(s[n].pos_height[m][a]+pos_height<=-1){possible=0;break;}
if(s[n].pos_height[m][a]+pos_height>=block_max_vertical){possible=0;break;}
if(s[n].pos_width[m][a]+pos_width<=-1){possible=0;break;}
if(s[n].pos_width[m][a]+pos_width>=block_max_horizonal){possible=0;break;}
if(occupied[s[n].pos_height[m][a]+pos_height][s[n].pos_width[m][a]+pos_width]){possible=0;break;}
}
if(possible)
{
for(a=0;a<=s[n].jishu-1;a++)occupied[ pos_height+s[n].pos_height[m][a] ][ pos_width+s[n].pos_width[m][a]]=color;
}
else
{
pos_height=temp[0];
pos_width=temp[1];
m=temp[2];
for(a=0;a<=s[n].jishu-1;a++)occupied[ pos_height+s[n].pos_height[m][a] ][ pos_width+s[n].pos_width[m][a]]=color;
}
return possible;
}
br
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
俄罗斯方块.rar (40个子文件)
cut
main.c 7KB
cut.ncb 41KB
cut.opt 53KB
Debug
cut.ilk 213KB
vc60.pdb 68KB
cut.pdb 465KB
vc60.idb 41KB
cut.exe 188KB
main.obj 35KB
cut.pch 2.65MB
cut.plg 2KB
cut.dsw 531B
cut.dsp 4KB
final
new_game.bmp 117KB
main.c 13KB
res.rc 2KB
resource.h 812B
final.plg 4KB
final.dsw 535B
final.dsp 4KB
tetris1.bmp 39KB
tetris5.bmp 38KB
final.ncb 41KB
final.opt 53KB
Debug
vc60.pdb 68KB
final.pdb 513KB
final.pch 2.66MB
vc60.idb 41KB
final.exe 2.62MB
main.obj 47KB
res.res 2.02MB
final.ilk 232KB
score.bmp 117KB
tetris4.bmp 38KB
res.aps 2.04MB
tetris2.bmp 38KB
tetris3.bmp 40KB
config.data 674B
pause.bmp 821KB
continue.bmp 821KB
共 40 条
- 1
fengchengyuan
- 粉丝: 7
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页