3 俄羅斯方塊專案

2022-03-13 03:03:42 字數 4452 閱讀 6993

巨集定義如下:

全域性dc

2 hdc g_hdcbuf;//

快取dc

3 hdc g_hdcbg;//

背景dc

4 hbitmap g_hbmp;//

背景建立四個函式,乙個用於載入資源,乙個用於初始化,乙個用於畫圖,乙個用於**資源

1

void load_game(hwnd);

2void

init_game();

3void paint_game();

4 void clear_game();

load_game()函式

1

void load_game(hwnd hwnd)

2

在init_game()中畫格仔中畫邊框,以及畫背景

1

void init_game()

2

paint_game()函式

1

void paint_game()217

}18}19

//刪除畫刷

20deleteobject(selectobject(g_hdcbuf, getstockobject(null_brush)));

2122

//顯示當前下降的方塊

23for (int x = 0; x < 4; x++)

2435}36

}3738//

顯示下乙個方塊

39for (int y = 0; y < 4; y++)

4051}52

}5354//

g_hdcbuf存放在g_hdc中 把快取dc內的東西重新整理到遊戲dc

55 bitblt(g_hdc,0,0,x_size,y_size,g_hdcbuf,0,0

,srccopy);

56 }

clear_game()函式

1

void clear_game()

2

由於背景只需要畫一次,所以load_game(),init_game()在initinstance函式中呼叫

1 bool initinstance(hinstance hinstance, int

ncmdshow)

215 load_game(hwnd);

16 init_game();

1718showwindow(hwnd, ncmdshow);

19updatewindow(hwnd);

2021

return

true;

22 }

此時截圖:

俄羅斯方塊用4*4的格仔表示:

用陣列表示

1 int g_asttetris[4][4] =

2, , , },

4 , , , },

5 , , , },

6 , , , },

7 , , , },

8 , , , },

9 , , , },

10};

11 int g_astcur[4][4];//

當前是那一塊方塊

12 int g_astnext[4][4];//

下一塊是哪一塊方塊

在init_game中初始化方塊:

1

#define cnt sizeof(g_asttetris)/sizeof(g_asttetris[0])

2void init_game()318

}19}20

//刪除畫刷

21deleteobject(selectobject(g_hdcbuf, getstockobject(null_brush)));

2223

//初始化方塊24 srand(gettickcount());

25 int x = rand() % cnt;

26 //隨機初始化當前方塊

27 memcpy(g_astcur, g_asttetris[x], sizeof(g_astcur));

28 }

設定方塊的初始位置:

1 int g_x, g_y;//

當前方塊下降到那個位置

並在init_game中初始化

1

//方塊出現的初始位置

2 g_x = (10 - 4) / 2

;3 g_y = 1;

在paint_game中畫方塊:

1

void paint_game()

217 }

18 }

19 20 //顯示下乙個方塊

21 for (int y = 0; y < 4; y++)

22

33 }

34 }

3536//

g_hdcbuf存放在g_hdc中 把快取dc內的東西重新整理到遊戲dc

37 bitblt(g_hdc,0,0,x_size,y_size,g_hdcbuf,0,0

,srccopy);

38 }

此時專案截圖:

設定定時器動起來:

在訊息迴圈中加入以下**:

1

case

wm_create:

2 settimer(hwnd, 1, 200, 0);3

break;4

case

wm_timer:5//

下降6if (checkmeet(g_x,g_y+1))//

可以下降710

else

//停止下降

1121

paint_game();

22break;23

case

wm_command:

24 wmid =loword(wparam);

25 wmevent =hiword(wparam);

26//

分析選單選擇:

27switch

(wmid)

2838

break;39

case

wm_paint:

40 hdc = beginpaint(hwnd, &ps);

41//

todo: 在此新增任意繪圖**...

42paint_game();

43 endpaint(hwnd, &ps);

44break;45

46case

wm_keydown:

47if (g_gameover)//

遊戲結束

4851

switch

(wparam)

5258

break;59

case

vk_right:

60if (checkmeet(g_x + 1

, g_y))

6164

break;65

case vk_up://

變形66

break;67

case vk_down://

移到下面

68break;69

}70paint_game();

71break;

checkmeet函式:

//

無碰撞返回true,否則返回false

bool checkmeet()}}

return

true;

}

俄羅斯方塊高階 AI俄羅斯方塊

前文回顧 致青春 python實現俄羅斯方塊 人工智慧大火的今天,如果還是自己玩俄羅斯方塊未免顯得太low,為什麼不對遊戲公升級,讓機器自己去玩俄羅斯方塊呢?有了這個想法之後利用週六週日兩天的時間去蒐集了大量的資料,在電腦宕機好多次之後終於將ai俄羅斯方塊實現了。所謂讓機器自己去玩俄羅斯方塊,就是讓...

俄羅斯方塊

俄羅斯方塊 tetris,俄文 是一款風靡全球的電視遊戲機 和掌上遊戲機遊戲,它由俄羅斯人阿列克謝 帕基特諾夫 發明,故得此名。俄羅斯方塊的基本規則是移動 旋轉和擺放遊戲自動輸出的各種方塊,使之排列成完整的一行或多行並且消除得分。由於上手簡單 老少皆宜,從而家喻戶曉,風靡世界。俄羅斯方塊的開發者是阿...

俄羅斯方塊

include include include include includeusing namespace std include include define mem a,b memset a,b,sizeof a const int sudu 40 const int dir 4 2 cons...