版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、#include#include resource1.h#include #include #include / 蛇的結(jié)構(gòu)體typedef struct snakeint x;int y;struct snake *pnext;snake;/ 豆的結(jié)構(gòu)體typedef struct beanint x;int y;struct bean *pnext;bean;/ 函數(shù)原型void createsnake(snake *psnake);void deleteall(snake *psnake);void showsnake(snake *psnake);void createbean(bean
2、 *pbean,snake *psnake);void showbean(bean *pbean);void deletebean(bean *pbean);void snakerun(snake *psnake);void snakegrowup(snake *psnake);int caneatbean(snake *psnake,bean *pbean);int candie(snake *psnake);/ /char g_classname20 = classname;lresult callback wndproc(hwnd hwnd,uint nmsg,wparam wparam
3、,lparam lparam);hbitmap hbitmap;int callback winmain(hinstance hinstance,hinstance hpreinstance,lpstr pcmdline, int ncmdshow) hbrush hbrush = createsolidbrush(rgb(100,100,410);hicon hicon = loadicon(hinstance,makeintresource(idi_wnd_icon);hicon hiconsm = loadicon(hinstance,makeintresource(idi_wnd_ic
4、onsm); hbitmap = loadbitmap(hinstance,makeintresource(idb_wdnbitmap);/ 設(shè)計類wndclassex wndclass;wndclass.cbclsextra = null;wndclass.cbsize = sizeof(wndclass);wndclass.cbwndextra = null;wndclass.hbrbackground = hbrush;wndclass.hcursor = loadcursor(hinstance,idc_arrow);wndclass.hicon = hicon;wndclass.hi
5、consm = hiconsm;wndclass.hinstance = hinstance;wndclass.lpfnwndproc = wndproc;wndclass.lpszclassname = g_classname;wndclass.style = cs_hredraw|cs_vredraw;wndclass.lpszmenuname = null;/ 注冊if (!registerclassex(&wndclass) messagebox(null,注冊失敗,消息,mb_ok);/ 創(chuàng)建hwnd hwnd = createwindow(g_classname, 貪吃蛇 ,ws_
6、overlappedwindow,200,50, 600+16,600+38,null, null, hinstance, null);if (!hwnd) messagebox(null,”創(chuàng)建失敗匚消息,mb_ok);/ 顯示showwindow(hwnd,ncmdshow);/ 消息循環(huán)msg msg;while (getmessage(&msg,null,0,0)translatemessage(&msg);/ 翻譯dispatchmessage(&msg);/ 回調(diào)函數(shù)return 0;hdc dc = null;bean *pbean = null;snake *psnake =
7、null;wparam,lparamint vk = vk_right;lresult callback wndproc(hwnd hwnd,uint nmsg,wparam lparam)switch (nmsg)case wm_create:createsnake(&psnake); / 創(chuàng)建蛇createbean(&pbean,psnake);dc = getdc(hwnd);break;case wm_keydown:switch (wparam) case vk_return:/ 按下后就開始settimer(hwnd,1,150,null);break;case vk_up:v k
8、 = vk_up;break;case vk_down:v k = vk_down;break;case vk_left:v k = vk_left;break;case vk_right:v k = vk_right;break;case :killtimer(hwnd,1);break; break;case wm_timer:/* 創(chuàng)建背景 */rect rect;getwindowrect(hwnd,&rect);hdc hmedc = createcompatibledc(dc);selectobject(hmedc,hbitmap);stretchblt(dc,0,0,rect.r
9、ight-rect.left,rect.bottom-rect.top,hmedc,0,0,500,600,srccopy);deletedc(hmedc);/* 背景創(chuàng)建結(jié)束*/ 各種函數(shù)if (candie(psnake)killtimer(hwnd,1);messagebox(null,笨蛋,撞到自己身上,我死啦!,消息,mb_ok);if ( caneatbean(psnake,pbean) ) snakegrowup(psnake);deletebean(&pbean);createbean(&pbean,psnake);snakerun(&psnake); elsesnakerun
10、(&psnake);showsnake(psnake);showbean(pbean);case wm_paint:/* 創(chuàng)建背景 */rect rect;getwindowrect(hwnd,&rect);hdc hmedc = createcompatibledc(dc);selectobject(hmedc,hbitmap);stretchblt(dc,0,0,rect.right-rect.left,rect.bottom-rect.top,hmedc,0,0,500,600,srccopy);/* 背景創(chuàng)建結(jié)束*/showsnake(psnake); / 顯示蛇showbean(pb
11、ean); / 顯示豆 deletedc(hmedc);break;case wm_close:deleteall(&psnake);deletebean(&pbean);destroywindow(hwnd);break;case wm_destroy:releasedc(hwnd,dc);postquitmessage(0);break;case wm_quit:break;return defwindowproc(hwnd,nmsg,wparam,lparam); / 創(chuàng)建蛇 create snake()void createsnake(snake *psnake)snake *u =
12、null,*w = null;int i = 3; / 開始的蛇有三截int x = 0; / 蛇頭的初始位置int y = 0; / 蛇尾的初始位置while (i-)u = (snake *)malloc(sizeof(snake);u-pnext = null;u-x = x;u-y = y;x +=20;if (null = *psnake)*psnake = u;elsew-pnext = u;w = u;/* 蛇的三屆創(chuàng)建完畢*/ 創(chuàng)建豆void createbean(bean *pbean,snake *psnake) snake *ji;int x = 0; / 隨機的位置x
13、坐標(biāo)int y = 0; / 隨機的位置y 坐標(biāo)srand(time(null);x = (unsigned)rand()%29)*20;y = (unsigned)rand()%29)*20;ji = psnake; while(psnake)if (x = psnake-x) & (y = psnake-y) ) x = (unsigned)rand()%29)*20;y = (unsigned)rand()%29)*20; psnake = ji; psnake = psnake-pnext;bean *u;u = (bean *)malloc(sizeof(bean);u-pnext
14、= null;u-x = x;u-y = y;*pbean = u;/ 刪掉全部蛇void deleteall(snake *psnake)snake *del;while(*psnake)del = *psnake;(*psnake) = (*psnake)-pnext; free(del);/ 刪掉豆void deletebean(bean *pbean)free(*pbean);*pbean = null;/ 顯示蛇void showsnake(snake *psnake)while (psnake)rectangle(dc,psnake-x,psnake-y,psnake-x+20,p
15、snake-y+20); psnake = psnake-pnext;/ 顯示豆void showbean(bean *pbean)rectangle(dc,pbean-x,pbean-y,pbean-x+20,pbean-y+20);/ 貪吃蛇移動void snakerun(snake *psnake)snake *ji, / 記錄變化的蛇頭*round; / 循環(huán)遍歷int x, / 蛇頭的當(dāng)前位置x 坐標(biāo)y; / 蛇頭的當(dāng)前位置 y 坐標(biāo)ji = *psnake;round = *psnake;while (round-pnext)round = round-pnext;x = roun
16、d-x;y = round-y;switch(vk)case vk_up:if (y = 0)y = 600;y -=20;break;case vk_down:if (y=600)y = 0;y +=20;break;case vk_left:if (x = 0)x = 600;x -= 20;break;case vk_right:if (x = 600)x = 0;x +=20;break;*psnake = (*psnake)-pnext;ji-x = x;ji-y = y;ji-pnext = null;round-pnext = ji;/ 下一步能吃到豆嗎? 返回 1 吃到int
17、caneatbean(snake *psnake,bean *pbean)while (psnake-pnext)psnake = psnake-pnext;if (psnake-x=pbean-x)&(psnake-y=pbean-y)return 1;return 0;/ 長大void snakegrowup(snake *psnake)snake *u;u = (snake *)malloc(sizeof(snake);u-pnext = null;u-x = pbean-x;u-y = pbean-y;while(psnake-pnext) psnake = psnake-pnext;psnake-pnext = u;/ 蛇會死嗎 返回 1 會死int candie(snake *psnake)snake *ji = null;int x = 0,y = 0;ji = psnake;while (psnake-pnext)psnake = psnake-
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 工業(yè)互聯(lián)網(wǎng)產(chǎn)業(yè)園風(fēng)險評估與應(yīng)對策略
- 二零二五年度廣告代理居間傭金合同范本3篇
- 天然礦泉水建設(shè)項目可行性研究報告申請立項備案
- 2025年中國高爐礦渣粉行業(yè)深度分析、投資前景、趨勢預(yù)測報告(智研咨詢)
- 食用菌產(chǎn)業(yè)園一期項目可行性研究報告申請備案
- 2025年度杭州服裝租賃合同3篇
- 《大學(xué)》精讀(北京師范大學(xué))學(xué)習(xí)通測試及答案
- 巖土工程勘察服務(wù)投標(biāo)方案(技術(shù)方案)
- 疼痛護(hù)理課件
- 副院長兼總工程師的崗位說明書
- 農(nóng)民專業(yè)合作社章程參考
- 十二經(jīng)脈循行原文背誦
- 財務(wù)會計制度及核算軟件備案報告書
- 肌骨超聲簡介
- 高一家長會課件ppt
- 神經(jīng)外科臨床實習(xí)教學(xué)計劃
- 基本光刻工藝流程
- 胸腔閉式引流護(hù)理-2023年中華護(hù)理學(xué)會團(tuán)體標(biāo)準(zhǔn)
評論
0/150
提交評論