MFC 小遊戲之貪吃蛇

2021-06-01 21:59:18 字數 3404 閱讀 3652

關於雙緩衝 和 onerasebkgnd(cdc* pdc) 的資料請看我部落格的文章。

我是用vc6建立乙個.exe 的文字空間,即再在view類中新增檔案,我將這部分的**貼出

大家可以交流下,如有不好之處,請見諒!

// mfc_snackeview.cpp : implementation of the cmfc_snackeview class

//#include "stdafx.h"

#include "mfc_snacke.h"

#include "mfc_snackedoc.h"

#include "mfc_snackeview.h"

#include #include #include #ifdef _debug

#define new debug_new

#undef this_file

static char this_file = __file__;

#endif

/// cmfc_snackeview

implement_dyncreate(cmfc_snackeview, cview)

begin_message_map(cmfc_snackeview, cview)

//}afx_msg_map

// standard printing commands

on_command(id_file_print, cview::onfileprint)

on_command(id_file_print_direct, cview::onfileprint)

on_command(id_file_print_preview, cview::onfileprintpreview)

end_message_map()

/// cmfc_snackeview construction/destruction

cmfc_snackeview::cmfc_snackeview()

cmfc_snackeview::~cmfc_snackeview()

bool cmfc_snackeview::precreatewindow(createstruct& cs)

/// cmfc_snackeview drawing

void cmfc_snackeview::ondraw(cdc* pdc) //畫介面

//draw body of snake

cpoint b;

memdc.selectstockobject(gray_brush);

for(int i = 1; i <= m_body.getupperbound(); i++)

//draw a food

cbrush br;

br.createsolidbrush(rgb(0, 255, 0));

memdc.selectobject(br);

memdc.fillrect(crect(m_food.x, m_food.y, m_food.x+20, m_food.y+20), &br);

memdc.rectangle(crect(m_food.x, m_food.y, m_food.x+20, m_food.y+20));

//將記憶體中圖拷貝到螢幕上進行顯示

pdc->bitblt(0, 0, rect.width(), rect.height(), &memdc, 0, 0, srccopy);

//繪圖完成後的清理

membitmap.deleteobject();

memdc.deletedc();}/

// cmfc_snackeview printing

bool cmfc_snackeview::onprepareprinting(cprintinfo* pinfo)

void cmfc_snackeview::onbeginprinting(cdc* , cprintinfo* )

void cmfc_snackeview::onendprinting(cdc* , cprintinfo* )

/// cmfc_snackeview diagnostics

#ifdef _debug

void cmfc_snackeview::assertvalid() const

void cmfc_snackeview::dump(cdumpcontext& dc) const

cmfc_snackedoc* cmfc_snackeview::getdocument() // non-debug version is inline

#endif //_debug

/// cmfc_snackeview message handlers

void cmfc_snackeview::initial_game() //遊戲地圖的初始化

void cmfc_snackeview::create_food()

}if(!prove)

break;

} }m_food = cpoint(x, y);

}void cmfc_snackeview::gamestart() //開始遊戲哦

void cmfc_snackeview::ontimer(uint nidevent) //wm_timer訊息

void cmfc_snackeview::move() //蛇移動函式

int over = 0;

cpoint t;

//蛇頭是否吃到自己

for(int i = 0; i <= m_body.getupperbound(); i++) }

if(!check(head) || over)

else

else

m_body.removeat(m_body.getupperbound());

}

//使整個視窗客戶區無效,意味著需要重繪,即呼叫ondraw函式

//invalidaterect(crect(39, 39, 839, 489));

invalidate();

}//鍵盤按鍵判斷

void cmfc_snackeview::onkeydown(uint nchar, uint nrepcnt, uint nflags)

} cview::onkeydown(nchar, nrepcnt, nflags);

}//判斷是否出界

int cmfc_snackeview::check(cpoint head)

void cmfc_snackeview::oneasy()

void cmfc_snackeview::onmedium()

void cmfc_snackeview::onhard()

bool cmfc_snackeview::onerasebkgnd(cdc* pdc)

貪吃蛇小遊戲

1 doctype html 2 html 3 head 4 title snake title 5head 6 body style text align center margih 100px background color aaaaaa 7 canvas id canv width 400 ...

C 貪吃蛇小遊戲

自學了幾天c 嘗試寫了乙個貪吃蛇小遊戲,比較詳細地做了注釋,實現 1 貪吃蛇基本功能 2 長按加速 3 計分板 xaml 及介面 namespace 貪吃蛇 新食物生成 public void newfoodgenerate 計時器每乙個計時週期內的時間處理程式 void timer tick ob...

貪吃蛇小遊戲實現

index.html style.css mapindex.js tools.js 自呼叫函式傳入window的目的,是讓變數可以被壓縮 防止undefined重新命名,也可以被壓縮 function window,undefined 暴露tools給window window.tools tool...