面向對象程序設計畢業(yè)論文_第1頁
面向對象程序設計畢業(yè)論文_第2頁
面向對象程序設計畢業(yè)論文_第3頁
面向對象程序設計畢業(yè)論文_第4頁
面向對象程序設計畢業(yè)論文_第5頁
已閱讀5頁,還剩37頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

《面向對象程序設計》課程設計《面向對象程序設計》課程設計報告2015/2016學年第二學期系部:計算機科學系專業(yè):計算機網絡工程班級:應本141姓名:學號:指導教師:成績:[課程設計目的]1.鞏固面向對象程序設計語言C++的基本語法和基本操作技能。2.掌握面向對象程序設計方法和使用集成環(huán)境調試和修改程序的方法。3.提高學生分析問題和解決問題的能力,為學習后續(xù)專業(yè)課程打好基礎。[課程設計主要內容]一、系統環(huán)境1、硬件環(huán)境:個人pcWindowsxp(windows7)2、軟件環(huán)境:Visualstdioc++2010Visualc++6.0二、實訓題目具體實現題目1:需求分析類視圖代碼pacman.cpp//pacman.cpp:定義應用程序的入口點。//#include"stdafx.h"#include"pacman.h"#include"GObject.h"#defineWLENTH700#defineWHIGHT740#defineSTAGE_COUNT3#defineMAX_LOADSTRING100//全局變量:HINSTANCEhInst; //當前實例TCHARszTitle[MAX_LOADSTRING]; //標題欄文本TCHARszWindowClass[MAX_LOADSTRING]; //主窗口類名PacMan*p;GObject*e1;GObject*e2;GObject*e3;GObject*e4;//釋放動態(tài)內存函數模板template<classT>voidRealese(Tt){ if(t!=NULL) deletet;}//此代碼模塊中包含的函數的前向聲明:ATOM MyRegisterClass(HINSTANCEhInstance);BOOL InitInstance(HINSTANCE,int,HWND&);LRESULTCALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);INT_PTRCALLBACK About(HWND,UINT,WPARAM,LPARAM);voidResetGObjects();intAPIENTRY_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPTSTRlpCmdLine,intnCmdShow){ UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine);//TODO:在此放置代碼。 ints_n=0; p=newPacMan(P_ROW,P_ARRAY); e1=newRedOne(E_ROW,E_ARRAY); e2=newRedOne(E_ROW,E_ARRAY); e3=newBlueOne(E_ROW,E_ARRAY); e4=newYellowOne(E_ROW,E_ARRAY); GMap*MapArray[STAGE_COUNT]={newStage_1(),newStage_2(),newStage_3()}; GObject::pStage=MapArray[s_n]; Enermy::player=p; MSGmsg; HACCELhAccelTable; //初始化全局字符串 LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING); LoadString(hInstance,IDC_PACMAN,szWindowClass,MAX_LOADSTRING); MyRegisterClass(hInstance);//執(zhí)行應用程序初始化: HWNDhWnd; if(!InitInstance(hInstance,nCmdShow,hWnd)) { returnFALSE; }hAccelTable=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_PACMAN)); DWORDt=0; //主消息循環(huán): while(p->GetTw()!=OVER&&s_n<3) { if(p->Win()) { HDChdc=GetDC(hWnd); s_n++; ResetGObjects(); if(s_n<3) { MessageBoxA(hWnd,"恭喜您過關","吃豆子提示",MB_OK); GObject::pStage=MapArray[s_n]; RECTscreenRect; screenRect.top=0; screenRect.left=0; screenRect.right=WLENTH; screenRect.bottom=WHIGHT; ::FillRect(hdc,&screenRect,CreateSolidBrush(RGB(255,255,255))); GObject::pStage->DrawMap(hdc); } continue; } if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } if(GetAsyncKeyState(VK_DOWN)&0x8000) { p->SetTwCommand(DOWN); } if(GetAsyncKeyState(VK_LEFT)&0x8000) { p->SetTwCommand(LEFT); } if(GetAsyncKeyState(VK_RIGHT)&0x8000) { p->SetTwCommand(RIGHT); } if(GetAsyncKeyState(VK_UP)&0x8000) { p->SetTwCommand(UP); } else { if(GetTickCount()-t>58) { HDChdc=GetDC(hWnd); e1->action(); e2->action(); e3->action(); e4->action(); p->action(); GObject::pStage->DrawPeas(hdc); e1->DrawBlank(hdc); e2->DrawBlank(hdc); e3->DrawBlank(hdc); e4->DrawBlank(hdc); p->DrawBlank(hdc); e1->Draw(hdc); e2->Draw(hdc); e3->Draw(hdc); e4->Draw(hdc); p->Draw(hdc); DeleteDC(hdc); t=GetTickCount(); } } } Realese(e1); Realese(e2); Realese(e3); Realese(e4); for(inti=0;i<STAGE_COUNT;i++) { Realese(MapArray[i]); } if(p->GetTw()==OVER) { MessageBoxA(hWnd,"出師未捷","吃豆子提示",MB_OK);} else { MessageBoxA(hWnd,"恭喜您贏得了勝利","吃豆子提示",MB_OK); } Realese(p);return(int)msg.wParam;}////函數:MyRegisterClass()////目的:注冊窗口類。////注釋:////僅當希望//此代碼與添加到Windows95中的“RegisterClassEx”//函數之前的Win32系統兼容時,才需要此函數及其用法。調用此函數十分重要,//這樣應用程序就可以獲得關聯的//“格式正確的”小圖標。//ATOMMyRegisterClass(HINSTANCEhInstance){ WNDCLASSEXwcex; wcex.cbSize=sizeof(WNDCLASSEX); wcex.style =CS_HREDRAW|CS_VREDRAW; wcex.lpfnWndProc =WndProc; wcex.cbClsExtra =0; wcex.cbWndExtra =0; wcex.hInstance =hInstance; wcex.hIcon =LoadIcon(hInstance,MAKEINTRESOURCE(IDI_PACMAN)); wcex.hCursor =LoadCursor(NULL,IDC_ARROW); wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName =MAKEINTRESOURCE(IDC_PACMAN); wcex.lpszClassName =szWindowClass; wcex.hIconSm =LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));returnRegisterClassEx(&wcex);}////函數:InitInstance(HINSTANCE,int)////目的:保存實例句柄并創(chuàng)建主窗口////注釋:////在此函數中,我們在全局變量中保存實例句柄并//創(chuàng)建和顯示主程序窗口。//BOOLInitInstance(HINSTANCEhInstance,intnCmdShow,HWND&hWnd){hInst=hInstance;//將實例句柄存儲在全局變量中hWnd=CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);if(!hWnd){returnFALSE;}ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);returnTRUE;}////函數:WndProc(HWND,UINT,WPARAM,LPARAM)////目的:處理主窗口的消息。////WM_COMMAND -處理應用程序菜單//WM_PAINT -繪制主窗口//WM_DESTROY -發(fā)送退出消息并返回////LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam){ intwmId,wmEvent; PAINTSTRUCTps; HDChdc; switch(message) { caseWM_COMMAND: wmId=LOWORD(wParam); wmEvent=HIWORD(wParam); //分析菜單選擇: switch(wmId) { caseIDM_ABOUT: DialogBox(hInst,MAKEINTRESOURCE(IDD_ABOUTBOX),hWnd,About); break; caseIDM_START: caseIDM_EXIT: DestroyWindow(hWnd); default: returnDefWindowProc(hWnd,message,wParam,lParam); } break; caseWM_PAINT: hdc=BeginPaint(hWnd,&ps); //TODO:在此添加任意繪圖代碼... GObject::pStage->DrawMap(hdc); EndPaint(hWnd,&ps); break; caseWM_DESTROY: PostQuitMessage(0); ::exit(0); break; default: returnDefWindowProc(hWnd,message,wParam,lParam); } return0;}//“關于”框的消息處理程序。INT_PTRCALLBACKAbout(HWNDhDlg,UINTmessage,WPARAMwParam,LPARAMlParam){ UNREFERENCED_PARAMETER(lParam); switch(message) { caseWM_INITDIALOG: return(INT_PTR)TRUE; caseWM_COMMAND: if(LOWORD(wParam)==IDOK||LOWORD(wParam)==IDCANCEL) { EndDialog(hDlg,LOWORD(wParam)); return(INT_PTR)TRUE; } break; } return(INT_PTR)FALSE;}voidResetGObjects(){ p->SetPosition(P_ROW,P_ARRAY); e1->SetPosition(E_ROW,E_ARRAY); e2->SetPosition(E_ROW,E_ARRAY); e3->SetPosition(E_ROW,E_ARRAY); e4->SetPosition(E_ROW,E_ARRAY);}GMap.h#pragmaonce#include"stdafx.h"#include<list>#defineMAPLENTH19#defineP_ROW10#defineP_ARRAY9#defineE_ROW8#defineE_ARRAY9usingstd::list;classGMap{protected: staticintLD; staticintPD; voidInitOP(); boolmapData[MAPLENTH][MAPLENTH]; boolpeaMapData[MAPLENTH][MAPLENTH]; COLORREFcolor;public: voidDrawMap(HDC&hdc); voidDrawPeas(HDC&hdc); virtual~GMap(); GMap(){ }friendclassGObject;friendclassPacMan;};classStage_1:publicGMap{private: boolstaticinitData[MAPLENTH][MAPLENTH];public: Stage_1();};classStage_2:publicGMap{private: boolstaticinitData[MAPLENTH][MAPLENTH];public: Stage_2();};classStage_3:publicGMap{private: boolstaticinitData[MAPLENTH][MAPLENTH];public: Stage_3();};GMap.cpp#include"stdafx.h"#include"GMap.h"intGMap::LD=36;intGMap::PD=3;voidGMap::InitOP(){ peaMapData[E_ROW][E_ARRAY]=false; peaMapData[P_ROW][P_ARRAY]=false;}voidGMap::DrawMap(HDC&memDC){ for(inti=0;i<MAPLENTH;i++) { for(intj=0;j<MAPLENTH;j++) { if(!mapData[i][j]) { RECTrect; rect.left=j*LD; rect.top=i*LD; rect.right=(j+1)*LD; rect.bottom=(i+1)*LD; FillRect(memDC,&rect,CreateSolidBrush(color)); } } }}voidGMap::DrawPeas(HDC&hdc){ for(inti=0;i<MAPLENTH;i++) { for(intj=0;j<MAPLENTH;j++) { if(peaMapData[i][j]) { Ellipse(hdc,(LD/2-PD)+j*LD,(LD/2-PD)+i*LD,(LD/2+PD)+j*LD,(LD/2+PD)+i*LD); } } }}GMap::~GMap(){}#defineAtrue#defineBfalse boolStage_1::initData[MAPLENTH][MAPLENTH]= { B,B,B,B,B,B,B,B,B,A,B,B,B,B,B,B,B,B,B,//0 B,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,//1 B,A,A,B,A,A,B,B,B,A,B,B,B,A,A,B,A,A,B,//2 B,A,B,B,A,A,A,A,A,A,A,A,A,A,A,B,B,A,B,//3 B,A,B,A,A,A,B,B,B,A,B,B,B,A,A,A,B,A,B,//4 B,A,B,A,A,A,A,A,A,A,A,A,A,A,A,A,B,A,B,//5 B,A,A,A,A,A,B,B,A,A,A,B,B,A,A,A,A,A,B,//6 B,A,B,A,A,A,A,A,A,A,A,A,A,A,A,A,B,A,B,//7 B,A,B,A,A,A,A,A,B,A,B,A,A,A,A,A,B,A,B,//8 A,A,A,A,A,A,A,A,B,B,B,A,A,A,A,A,A,A,A,//9 B,A,B,A,A,A,A,A,A,A,A,A,A,A,A,A,B,A,B,//10 B,A,B,A,A,B,A,A,A,A,A,A,A,B,A,A,B,A,B,//11 B,A,B,A,B,B,B,A,A,A,A,A,B,B,B,A,B,A,B,//12 B,A,A,A,A,B,A,A,A,A,A,A,A,B,A,A,A,A,B,//13 B,A,B,B,A,A,A,A,A,A,A,A,A,A,A,B,B,A,B,//14 B,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,//15 B,A,A,A,A,B,B,B,A,B,A,B,B,B,A,A,A,A,B,//16 B,A,A,A,A,B,A,A,A,A,A,A,A,B,A,A,A,A,B,//17 B,B,B,B,B,B,B,B,B,A,B,B,B,B,B,B,B,B,B,//18 };#undefA#undefBStage_1::Stage_1(){ color=RGB(140,240,240); for(inti=0;i<MAPLENTH;i++) { for(intj=0;j<MAPLENTH;j++) { this->mapData[i][j]=this->initData[i][j]; this->peaMapData[i][j]=/*false;//*/this->initData[i][j]; } } //this->peaMapData[10][10]=true; this->InitOP();}#defineAtrue#defineBfalse boolStage_2::initData[MAPLENTH][MAPLENTH]= { B,B,B,B,B,B,B,B,B,A,B,B,B,A,B,B,B,B,B,//0 A,A,A,A,A,A,A,B,A,A,B,A,A,A,B,A,B,A,A,//1 B,A,A,A,B,A,A,B,A,A,B,A,B,A,B,A,B,A,B,//2 B,B,B,A,B,A,A,B,B,A,B,A,B,A,B,A,B,B,B,//3 B,A,A,A,A,A,A,A,A,A,A,A,B,B,B,A,A,A,B,//4 B,A,A,B,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,//5 B,A,A,B,A,A,A,B,B,B,B,B,B,A,A,B,A,A,B,//6 B,A,A,B,A,B,A,A,A,A,A,A,A,A,A,B,A,A,B,//7 B,A,A,B,A,B,A,A,B,A,B,A,A,B,A,B,A,A,B,//8 A,A,A,B,A,B,A,A,B,B,B,A,A,B,A,B,A,A,A,//9 B,A,A,B,A,B,A,A,A,A,A,A,A,B,A,A,A,A,B,//10 B,A,A,B,A,A,A,B,B,B,B,B,A,B,A,A,A,A,B,//11 B,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,B,//12 B,A,A,A,B,B,B,B,B,B,B,A,A,A,A,A,A,A,B,//13 B,A,A,A,A,A,A,A,A,A,A,A,A,B,A,A,A,A,B,//14 B,B,B,B,B,A,A,A,A,B,B,B,A,B,A,A,A,A,B,//15 B,A,A,A,B,B,B,A,A,A,A,B,A,B,B,B,A,A,B,//16 A,A,A,A,B,A,A,A,A,A,A,B,A,A,A,B,A,A,A,//17 B,B,B,B,B,B,B,B,B,A,B,B,B,A,B,B,B,B,B,//18 };#undefA#undefBStage_2::Stage_2(){ color=RGB(240,140,140); for(inti=0;i<MAPLENTH;i++) { for(intj=0;j<MAPLENTH;j++) { this->mapData[i][j]=this->initData[i][j]; this->peaMapData[i][j]=/*false;//*/this->initData[i][j]; } } //this->peaMapData[10][10]=true; this->InitOP();}#defineAtrue#defineBfalse boolStage_3::initData[MAPLENTH][MAPLENTH]= { B,B,B,B,B,B,B,B,B,A,B,B,B,B,B,B,B,B,B,//0 A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,//1 B,A,A,B,A,A,B,B,B,B,B,B,B,A,A,A,B,A,B,//2 B,A,B,B,A,A,A,A,A,A,A,A,B,A,A,A,B,A,B,//3 B,A,B,A,A,A,B,B,B,B,B,B,B,A,A,A,B,A,B,//4 B,A,B,A,B,B,B,A,A,A,A,A,B,B,B,A,B,A,B,//5 B,A,A,A,B,A,B,A,A,A,A,A,A,A,A,A,B,A,B,//6 B,A,B,A,B,A,A,A,A,A,A,A,A,B,A,A,B,A,B,//7 B,A,B,A,B,B,A,A,B,A,B,A,A,B,A,A,B,A,B,//8 B,A,A,A,A,B,A,A,B,B,B,A,A,B,A,A,B,A,B,//9 B,A,B,A,A,B,A,A,A,A,A,A,A,B,A,A,A,A,B,//10 B,A,B,A,A,B,A,A,A,A,A,A,B,B,B,A,B,A,B,//11 B,A,B,A,A,B,A,B,B,B,B,B,B,A,B,A,B,A,B,//12 B,A,B,A,A,B,A,A,A,A,A,A,A,A,B,A,B,A,B,//13 B,A,B,B,A,B,B,B,B,B,B,A,B,A,B,A,B,A,B,//14 B,A,A,A,A,B,A,A,A,A,A,A,B,A,B,A,B,A,B,//15 B,B,B,B,B,B,A,A,B,B,B,A,B,A,B,A,B,A,B,//16 A,A,A,A,A,A,A,A,B,A,A,A,A,A,B,A,A,A,A,//17 B,B,B,B,B,B,B,B,B,A,B,B,B,B,B,B,B,B,B,//18 };#undefA#undefBStage_3::Stage_3(){ color=RGB(100,300,100); for(inti=0;i<MAPLENTH;i++) { for(intj=0;j<MAPLENTH;j++) { this->mapData[i][j]=this->initData[i][j]; this->peaMapData[i][j]=/*false;//*/this->initData[i][j]; } } //this->peaMapData[10][10]=true; this->InitOP();}GObject.h#include"stdafx.h"#include"GMap.h"#definePLAYERSPEED6#defineENERMYSPEED4#defineLEGCOUNTS5#defineDISTANCE10#defineBLUE_ALERT8#defineD_OFFSET2#defineRD(DISTANCE+D_OFFSET)#include<time.h>enumTWARDS{UP,DOWN,LEFT,RIGHT,OVER};classGObject{protected: intmX; intmY; TWARDStwCommand; POINTpoint; intdRow; intdArray; intspeed; TWARDStw; intframe; boolAchive(); intPtTransfrom(intk); boolCollision(); virtualvoidAchiveCtrl();public: voidSetPosition(intRow,intArray); voidDrawBlank(HDC&hdc); voidvirtualDraw(HDC&hdc)=0; staticGMap*pStage; GObject(intRow,intArray){ frame=1; pStage=NULL; this->dRow=Row; this->dArray=Array; this->point.y=dRow*pStage->LD+pStage->LD/2; this->point.x=dArray*pStage->LD+pStage->LD/2; this->mX=point.x; this->mY=point.y;}voidvirtualaction()=0; intGetRow(); intGetArray(); };classPacMan:publicGObject{protected: virtualvoidAchiveCtrl();public: POINTGetPos(); TWARDSGetTw(); boolWin(); voidDraw(HDC&hdc); voidSetTwCommand(TWARDScommand); voidOver(); PacMan(intx,inty):GObject(x,y) { this->speed=PLAYERSPEED; twCommand=tw=LEFT; } voidaction();};classEnermy:publicGObject{protected: voidCatch(); voidvirtualMakeDecision(boolb)=0; COLORREFcolor;public: staticPacMan*player; voidvirtualDraw(HDC&hdc); Enermy(intx,inty):GObject(x,y) { this->speed=ENERMYSPEED; tw=LEFT; twCommand=UP; } voidvirtualaction();};classRedOne:publicEnermy{protected: voidvirtualMakeDecision(boolb);public: voidDraw(HDC&hdc); RedOne(intx,inty):Enermy(x,y) { color=RGB(255,0,0); }};classBlueOne:publicRedOne{protected: voidvirtualMakeDecision(boolb);public: voidDraw(HDC&hdc); BlueOne(intx,inty):RedOne(x,y) { color=RGB(0,0,255); }};classYellowOne:publicRedOne{protected: voidvirtualMakeDecision(boolb);public: voidDraw(HDC&hdc); YellowOne(intx,inty):RedOne(x,y) { color=RGB(200,200,100); }};GObject.cpp#include"stdafx.h"#include"GObject.h"http://#include"GMap.h"GMap*GObject::pStage=NULL;boolGObject::Achive(){ intn=(point.x-pStage->LD/2)%pStage->LD; intk=(point.y-pStage->LD/2)%pStage->LD; booll=(n==0&&k==0); returnl;}voidGObject::AchiveCtrl(){ if(Achive()) { dArray=PtTransfrom(point.x); dRow=PtTransfrom(point.y); }}intGObject::PtTransfrom(intk){ return(k-(pStage->LD)/2)/pStage->LD;}intGObject::GetRow(){ returndRow;}intGObject::GetArray(){ returndArray;}voidGObject::DrawBlank(HDC&hdc){ RECTrect; rect.top=mY-RD; rect.left=mX-RD; rect.right=mX+RD; rect.bottom=mY+RD; FillRect(hdc,&rect,::CreateSolidBrush(RGB(255,255,255)));}voidGObject::SetPosition(intRow,intArray){ dRow=Row; dArray=Array; this->point.y=dRow*pStage->LD+pStage->LD/2; this->point.x=dArray*pStage->LD+pStage->LD/2;}boolGObject::Collision(){ boolb=false; AchiveCtrl(); if(dArray<0||dRow<0||dArray>MAPLENTH-1||dRow>MAPLENTH-1) { b=true; } elseif(Achive()) { switch(twCommand) { caseLEFT: if(dArray>0&&!pStage->mapData[dRow][dArray-1]) { b=true; } break; caseRIGHT: if(dArray<MAPLENTH-1&&!pStage->mapData[dRow][dArray+1]) {b=true;} break; caseUP: if(dRow>0&&!pStage->mapData[dRow-1][dArray]) { b=true; } break; caseDOWN: if(dRow<MAPLENTH-1&&!pStage->mapData[dRow+1][dArray]) { b=true; } break; } if(!b) { tw=twCommand; } } mX=point.x; mY=point.y; intMAX=pStage->LD*MAPLENTH+pStage->LD/2; intMIN=pStage->LD/2; switch(tw) { caseLEFT: if(dArray>0&&!pStage->mapData[dRow][dArray-1]) { b=true; break; } point.x-=speed; if(point.x<MIN) { point.x=MAX; } break; caseRIGHT: if(dArray<MAPLENTH-1&&!pStage->mapData[dRow][dArray+1]) { b=true; break; } point.x+=speed; if(point.x>MAX) { point.x=MIN; } break; caseUP: if(dRow>0&&!pStage->mapData[dRow-1][dArray]) { b=true; break; } point.y-=speed; if(point.y<MIN) { point.y=MAX; } break; caseDOWN: if(dRow<MAPLENTH-1&&!pStage->mapData[dRow+1][dArray]) { b=true; break; } point.y+=speed; if(point.y>MAX) { point.y=MIN; } break; } returnb;}voidPacMan::AchiveCtrl(){ GObject::AchiveCtrl(); if(Achive()) { if(dRow>=0&&dRow<MAPLENTH&&dArray>=0&&dArray<MAPLENTH) { if(pStage->peaMapData[dRow][dArray]) { pStage->peaMapData[dRow][dArray]=false; } } }}voidPacMan::action(){ Collision();}voidPacMan::SetTwCommand(TWARDScommand){ twCommand=command;}boolPacMan::Win(){ for(inti=0;i<=MAPLENTH;i++) { for(intj=0;j<=MAPLENTH;j++) { if(pStage->peaMapData[i][j]==true) { returnfalse; } } } returntrue;}POINTPacMan::GetPos(){ returnpoint;}voidPacMan::Over(){ tw=OVER;}TWARDSPacMan::GetTw(){ returntw;}voidPacMan::Draw(HDC&memDC){ if(tw==OVER) { } elseif(frame%2==0) { intx1=0,x2=0,y1=0,y2=0; intoffsetX=DISTANCE/2+D_OFFSET; intoffsetY=DISTANCE/2+D_OFFSET; switch(tw) { caseUP: x1=point.x-offsetX; x2=point.x+offsetX; y2=y1=point.y-offsetY; break; caseDOWN: x1=point.x+offsetX; x2=point.x-offsetX; y2=y1=point.y+offsetY; break; caseLEFT: x2=x1=point.x-offsetX; y1=point.y+offsetY; y2=point.y-offsetY; break; caseRIGHT: x2=x1=point.x+offsetX; y1=point.y-offsetY; y2=point.y+offsetY; break; } Arc(memDC,point.x-DISTANCE,point.y-DISTANCE, point.x+DISTANCE,point.y+DISTANCE, x1,y1, x2,y2); MoveToEx(memDC,x1,y1,NULL); LineTo(memDC,point.x,point.y); LineTo(memDC,x2,y2);} elseif(frame%3==0) { Ellipse(memDC,point.x-DISTANCE,point.y-DISTANCE, point.x+DISTANCE,point.y+DISTANCE); } else{ intx1=0,x2=0,y1=0,y2=0; switch(tw) { caseUP: x1=point.x-DISTANCE; x2=point.x+DISTANCE; y2=y1=point.y; break; caseDOWN: x1=point.x+DISTANCE; x2=point.x-DISTANCE; y2=y1=point.y; break; caseLEFT: x2=x1=point.x; y1=point.y+DISTANCE; y2=point.y-DISTANCE; break; caseRIGHT: x2=x1=point.x; y1=point.y-DISTANCE; y2=point.y+DISTANCE; break; } Arc(memDC,point.x-DISTANCE,point.y-DISTANCE, point.x+DISTANCE,point.y+DISTANCE, x1,y1, x2,y2); MoveToEx(memDC,x1,y1,NULL); LineTo(memDC,point.x,point.y); LineTo(memDC,x2,y2); } frame++;}PacMan*Enermy::player=NULL;voidEnermy::Catch(){ intDX=point.x-player->GetPos().x; intDY=point.y-player->GetPos().y; if((-RD<DX&&DX<RD)&&(-RD<DY&&DY<RD)) { player->Over(); }}voidEnermy::Draw(HDC&hdc){ HPENpen=::CreatePen(0,0,color); HPENoldpen=(HPEN)SelectObject(hdc,pen); Arc(hdc,point.x-DISTANCE,point.y-DISTANCE, point.x+DISTANCE,point.y+DISTANCE, point.x+DISTANCE,point.y, point.x-DISTANCE,point.y); intconstLEGLENTH=(DISTANCE)/(LEGCOUNTS); if(frame%2==0) { MoveToEx(hdc,point.x-DISTANCE,point.y,NULL); LineTo(hdc,point.x-DISTANCE,point.y+DISTANCE-LEGLENTH); MoveToEx(hdc,point.x+DISTANCE,point.y,NULL); LineTo(hdc,point.x+DISTANCE,point.y+DISTANCE-LEGLENTH); for(inti=0;i<LEGCOUNTS;i++) { Arc(hdc,point.x-DISTANCE+i*2*LEGLENTH,point.y+DISTANCE-2*LEGLENTH, point.x-DISTANCE+(i+1)*2*LEGLENTH,point.y+DISTANCE, point.x-DISTANCE+i*2*LEGLENTH,point.y+DISTANCE-LEGLENTH, point.x-DISTANCE+(i+1)*2*LEGLENTH,point.y+DISTANCE-LEGLENTH ); } } else{ MoveToEx(hdc,point.x-DISTANCE,point.y,NULL); LineTo(hdc,point.x-DISTANCE,point.y+DISTANCE); MoveToEx(hdc,point.x+DISTANCE,point.y,NULL); LineTo(hdc,point.x+DISTANCE,point.y+DISTANCE); MoveToEx(hdc,point.x-DISTANCE,point.y+DISTANCE,NULL); LineTo(hdc,point.x-DISTANCE+LEGLENTH,point.y+DISTANCE-LEGLENTH); for(inti=0;i<LEGCOUNTS-1;i++) { Arc(hdc,point.x-DISTANCE+(1+i*2)*LEGLENTH,point.y+DISTANCE-2*LEGLENTH, point.x-DISTANCE+(3+i*2)*LEGLENTH,point.y+DISTANCE, point.x-DISTANCE+(1+i*2)*LEGLENTH,point.y+DISTANCE-LEGLENTH, point.x-DISTANCE+(3+i*2)*LEGLENTH,point.y+DISTANCE-LEGLENTH ); } MoveToEx(hdc,point.x+DISTANCE,point.y+DISTANCE,NULL); LineTo(hdc,point.x+DISTANCE-LEGLENTH,point.y+DISTANCE-LEGLENTH); } intR=DISTANCE/5; switch(tw) { caseUP: Ellipse(hdc,point.x-2*R,point.y-2*R, point.x,point.y); Ellipse(hdc,point.x,point.y-2*R, point.x+2*R,point.y); break; caseDOWN: Ellipse(hdc,point.x-2*R,point.y,point.x,point.y+2*R); Ellipse(hdc,point.x,point.y,point.x+2*R,point.y+2*R); break; caseLEFT: Ellipse(hdc,point.x-3*R,point.y-R, point.x-R,point.y+R); Ellipse(hdc,point.x-R,point.y-R, point.x+R,point.y+R); break; caseRIGHT: Ellipse(hdc,point.x-R,point.y-R, point.x+R,point.y+R); Ellipse(hdc,point.x+R,point.y-R, point.x+3*R,point.y+R); break; } frame++; SelectObject(hdc,oldpen); DeleteObject(pen); return;}voidEnermy::action(){ boolb=Collision(); MakeDecision(b); Catch();}voidRedOne::Draw(HDC&hdc){ Enermy::Draw(hdc);}voidRedOne::MakeDecision(boolb){ inti=rand(); if(b) { if(i%4==0) { tw==UP?twCommand=LEFT:twCommand=UP; } elseif(i%3==0) { tw==DOWN?twCommand=UP:twCommand=DOWN; } elseif(i%2==0) { tw==RIGHT?twCommand=UP:twCommand=RIGHT; } else { tw==LEFT?twCommand=DOWN:twCommand=LEFT; } return; } if(i%4==0) { twCommand!=UP?tw==DOWN:twCommand==UP; } elseif(i%3==0) { tw!=DOWN?twCommand=UP:twCommand==DOWN; } elseif(i%2==0) { tw!=RIGHT?twCommand=LEFT:twCommand=RIGHT; } else { tw!=LEFT?twCommand=RIGHT:twCommand=LEFT; }}voidBlueOne::Draw(HDC&hdc){ Enermy::Draw(hdc);}voidBlueOne::MakeDecision(boolb){ constintDR=this->dRow-player->GetRow(); constintDA=this->dArray-player->GetArray(); if(!b&&DR==0) { if(DA<=BLUE_ALERT&&DA>0) { twCommand=LEFT; return; } if(DA<0&&DA>=-BLUE_ALERT) { twCommand=RIGHT; return; } } if(!b&&DA==0) { if(DR<=BLUE_ALERT&&DR>0) { twCommand=UP; return; } if(DR<0&&DR>=-BLUE_ALERT) { twCommand=DOWN; return; } } RedOne::MakeDecision(b);}voidYellowOne::MakeDecision(boolb){ constintDR=this->dRow-player->GetRow(); constintDA=this->dArray-player->GetArray(); if(!b) { if(DR*DR>DA*DA) { if(DA>0) { twCommand=LEFT; return; } elseif(DA<0) { twCommand=RIGHT; return; } } else { if(DR>0) { twCommand=UP; return; } if(DR<0) { twCommand=DOWN; return; } } } RedOne::MakeDecision(b);}voidYellowOne::Draw(HDC&hdc){ Enermy::Draw(hdc);}運行結果第一關圖片:第二關圖片:吃豆人被吃圖片:

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論