遊戲模版19 Win32 物理引擎 勻速運動

2022-01-11 15:34:41 字數 3598 閱讀 3598

1 #include 2
//c 執行時標頭檔案

3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include

10 #include 11

12//

全域性變數:

13 hinstance hinst; //

當前例項

14 hbitmap bg , ball[2

];15

hdc hdc,mdc,bufdc;

16hwnd hwnd;

17dword tpre,tnow,tcheck;

18 rect rect;//

視窗矩形

19int x[2

];20

int y[2

];21

int vx[2

];22

int vy[2

];23

24//

此**模組中包含的函式的前向宣告:

25atom myregisterclass(hinstance hinstance);

26 bool initinstance(hinstance, int

);27

lresult callback wndproc(hwnd, uint, wparam, lparam);

28int_ptr callback about(hwnd, uint, wparam, lparam);

29void

mypaint(hdc hdc);

3031

32int

apientry _twinmain(hinstance hinstance,

33hinstance hprevinstance,

34lptstr lpcmdline,

35int

ncmdshow)

43//

主訊息迴圈:

44while (getmessage(&msg, null, 0, 0

))48

return (int

) msg.wparam;49}

5051

//函式: myregisterclass()

52//

53//

目的: 註冊視窗類。

54atom myregisterclass(hinstance hinstance)

7374

//75

//函式: initinstance(hinstance, int)

76//

77//

目的: 儲存例項控制代碼並建立主視窗

78//

79//

注釋:80

//81

//在此函式中,我們在全域性變數中儲存例項控制代碼並

82//

建立和顯示主程式視窗。

83//

棋盤拼接以及呼叫initgame()開始棋局

84 bool initinstance(hinstance hinstance, int

ncmdshow)

9596 movewindow(hwnd,10,10,600,450,true

);97

showwindow(hwnd, ncmdshow);

98updatewindow(hwnd);

99100 hdc=getdc(hwnd);

101 mdc=createcompatibledc(hdc);

102 bufdc=createcompatibledc(hdc);

103104 bmp=createcompatiblebitmap(hdc,600,480

);105

selectobject(mdc,bmp);

106107 bg=(hbitmap)loadimagea(null,"

bg.bmp

",image_bitmap,640,480

,lr_loadfromfile);

108 ball[0]=(hbitmap)loadimagea(null,"

ball0.bmp

",image_bitmap,52,26

,lr_loadfromfile);

109 ball[1]=(hbitmap)loadimagea(null,"

ball1.bmp

",image_bitmap,52,26

,lr_loadfromfile);

110111 getclientrect(hwnd,&rect);//

取得內部視窗區域的大小;

112113 x[0]=50;y[0]=50;vx[0]=4;vy[0]=4

;114 x[1]=380;y[1]=380;vx[1]=-4;vy[1]=-4

;115

116 settimer(hwnd,1,10

,null);

117mypaint(hdc);

118119

return

true;

120}

121122

//123

//函式: wndproc(hwnd, uint, wparam, lparam)

124//

125//

目的: 處理主視窗的訊息。

126//

127//

wm_command - 處理應用程式選單

128//

wm_paint - 繪製主視窗

129//

wm_destroy - 傳送退出訊息並返回

130//

131lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam)

159return0;

160}

161162

//mypaint()

163//

1、視窗貼圖

164//

2、計算小球貼圖座標並判斷小球是否碰撞視窗邊緣

165void

mypaint(hdc hdc)else

if(x[i]>=rect.right-26

)189

190//

計算y軸方向座標及速度

191 y[i]+=vy[i];

192if(y[i]<=0

)else

if(y[i]>=rect.bottom-26

)199

}200

201if((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])<=1000

)205 }

遊戲模版19 Win32 物理引擎 勻速運動

1 include 2 c 執行時標頭檔案 3 include 4 include 5 include 6 include 7 include 8 include 9 include 10 include 11 12 全域性變數 13 hinstance hinst 當前例項 14 hbitmap ...

win8 開發之旅 19 足球遊戲揭秘6

我們這裡揭秘什麼了,來揭秘一下.moveresult,goalpost shot這幾個類。首先,這幾個類都屬於moel層,這幾個類都是主要用於儲存相應的實體類的資訊,具體為什麼請聽我一一娓娓道來。moveresult 記錄移動資訊的類,就是記錄運動員的移動的資訊。他繼承與discoid這個類,物體的...

win8 開發之旅 19 足球遊戲揭秘6

我們這裡揭秘什麼了,來揭秘一下.moveresult,goalpost shot這幾個類。首先,這幾個類都屬於moel層,這幾個類都是主要用於儲存相應的實體類的資訊,具體為什麼請聽我一一娓娓道來。moveresult 記錄移動資訊的類,就是記錄運動員的移動的資訊。他繼承與discoid這個類,物體的...