1. 程式人生 > >C++MFC蛇蛇大作戰製作(不是傳統貪吃蛇)

C++MFC蛇蛇大作戰製作(不是傳統貪吃蛇)

大三的遊戲設計概論課做得大作業《蛇蛇大作戰》
其中的AI蛇還有點BUG,還有貼圖導致的一些問題,但總體來說遊戲正常,邏輯沒問題。
這是我設計的遊戲類圖:
在這裡插入圖片描述
在這裡插入圖片描述
這是總體流程圖:
在這裡插入圖片描述

這是我的資源管理器截圖:
在這裡插入圖片描述下面附上我的程式碼部分:

建立空專案以後,

#pragma once
#include "Snake.h"

class MySnake;

class Csnake :public Snake {
private:
public :

	double v;
	double o;
	vector<body> body;
	int isLive;
	Csnake();
	

};
#pragma once
#include "Csnake.h"
#include "MySnake.h"

class Food
{
public:

	double x, y, z;
	Food(int xx = 2969, int yy = 1645, int zz = 0)
	{
		x = xx;
		y = yy;
		z = zz;
	}


};


class GameManeger
{
public:
	
	int life = 3;
	 vector<Csnake> csnake;
	 MySnake snake;
	 vector<Food> food;
	 int safe(Csnake csnake);
	 void move();
	 void eat();
	 int die();
	 void cmove();
	 void ceat();
	 int cdie();
	 void attack();
	 void defence();
	 void setCsnake();


	GameManeger();
	~GameManeger();
};


#pragma once
#include <afxwin.h>
class MyApp :public CWinApp
{
public:
	BOOL InitInstance();
};


#pragma once
#include "Snake.h"



class MySnake :public Snake {
private:
public:
	
	double v;
	double o;
	
	vector<body> body;	
	MySnake();
	
	
};


#pragma once
#include <afxwin.h>
#include "GameManeger.h"
#include<time.h>
#include<windows.h>
#include<iostream>
#include<mmsystem.h>
#include<vector>
#pragma comment( lib, "Winmm.lib" )





class MyWnd :public CFrameWnd
{
private:
	int skin = 1;
	bool isEnd = false;
	CDC * ndc;
	CDC * mdc;
	CDC * tempdc;
	CRect rect;
	CBitmap *bg, *bgg, *gameBg, *rule, *sna,*Cu, *tempbmp, *toubu,*red,*blue,*skinBmp,*lose,*vectroy;
	CBitmap* foods[5];
	char foodName[20];
	HICON m_hIcon;
	int pos = -600;
	int x = 800;
	int y = 500;
	int state = 0;
	GameManeger gm;
public:
	
	MyWnd();
	~MyWnd();
	DECLARE_MESSAGE_MAP()
	afx_msg void OnPaint();
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};


#pragma once
#include <afxwin.h>
#include<time.h>

#include<vector>
#pragma comment( lib, "Winmm.lib" )

using namespace std;




class body
{
public:
	double x, y;
	body(int xx = 2964, int yy = 1544)
	{
		x = xx;
		y = yy;
	}
};

class Snake {

	
};

下面是.cpp檔案:


#include "Csnake.h"

Csnake::Csnake()
{
		v = 5;
		o = 120;
		isLive = 1;
		for (int i = 0; i < 20; i++)
		{
			body.push_back(::body(2800,1200));
		}
	
}


#include "GameManeger.h"



GameManeger::GameManeger()
{
	
}


GameManeger::~GameManeger()
{
}

void GameManeger::move()
{
	if (snake.body.size() <= 10)
		snake.v = 5;
	static int l = 20;
	if (snake.v != 5)
	{
		l--;
		if (l == 0)
		{
			l = 20;
			if (snake.body.size() > 1)
				snake.body.pop_back();
		}
	}
	for (int i = snake.body.size() - 1; i > 0; i--)
	{
		snake.body[i].x = snake.body[i - 1].x;
		snake.body[i].y = snake.body[i - 1].y;
	}
	if (snake.body.size() == 0)
		return;
	snake.body[0].x += snake.v * cos(snake.o*3.14159 / 180);
	snake.body[0].y += snake.v * sin(snake.o*3.14159 / 180);
}

void GameManeger::eat()
{
	if (snake.body.size() <= 100)
	{
		for (int i = 0; i < food.size(); i++)
		{
			if ((food[i].x - snake.body[0].x )*(food[i].x - snake.body[0].x ) + (food[i].y - snake.body[0].y )*(food[i].y - snake.body[0].y ) < 75 * 75)
			{
				snake.body.push_back(::body(0, 0));
				vector<::Food>::iterator iter = food.begin() + i;
				food.erase(iter);
				return;
			}
		}
	}
}

int GameManeger::die()
{
	if (snake.body[0].x - 35<803 || snake.body[0].x + 35 > 5198 || snake.body[0].y - 35<452 || snake.body[0].y + 35 > 2707)
	{
		for (int i = 1; i < snake.body.size(); i++)
		{
			if (i % 6 == 0) {
				food.push_back(::Food(snake.body[i].x, snake.body[i].y, rand() % 5));
			}

		}
		
		return 1;
	}
	for (int j = 0;j < csnake.size();j++) {

		
			for (int i = csnake[j].body.size() - 1; i > 0; i--)
			{
				if ((csnake[j].body[i].x - snake.body[0].x)*(csnake[j].body[i].x - snake.body[0].x) + (csnake[j].body[i].y - snake.body[0].y)*(csnake[j].body[i].y - snake.body[0].y) <= 70 * 70)
				{
					
					for (int i = 0; i < snake.body.size(); i++)
					{
						if (i % 6 == 0) {
							food.push_back(::Food(snake.body[i].x, snake.body[i].y, rand() % 5));
						}
					}
					
					return 1;
				}
			}
			if ((csnake[j].body[0].x - snake.body[0].x)*(csnake[j].body[0].x - snake.body[0].x ) + (csnake[j].body[0].y - snake.body[0].y )*(csnake[j].body[0].y - snake.body[0].y ) <= 70 * 70)
			{
				if (snake.body.size() < csnake[j].body.size())
				{
					
					for (int i = 0; i < snake.body.size(); i++)
					{
						if (i % 6 == 0) {
							food.push_back(::Food(snake.body[i].x, snake.body[i].y, rand() % 5));
						}
					}
					
					return 1;
				}
			}
		
	}


	return 0;

}


int GameManeger::safe(Csnake csnake)
{

	int k = 0;
	for (int i = 0; i < snake.body.size(); i++)
	{
		if ((snake.body[i].x - csnake.body[0].x)*(snake.body[i].x - csnake.body[0].x) + (snake.body[i].y - csnake.body[0].y)*(snake.body[i].y - csnake.body[0].y) <  200 * 200 && (snake.body[i].x - csnake.body[0].x)*(snake.body[i].x - csnake.body[0].x) + (snake.body[i].y - csnake.body[0].y)*(snake.body[i].y - csnake.body[0].y) < (snake.body[k].x - csnake.body[0].x)*(snake.body[k].x - csnake.body[0].x) + (snake.body[k].y - csnake.body[0].y)*(snake.body[k].y - csnake.body[0].y))
			k = i;
	}
	if ((snake.body[k].x - csnake.body[0].x)*(snake.body[k].x - csnake.body[0].x) + (snake.body[k].y - csnake.body[0].y)*(snake.body[k].y - csnake.body[0].y) < 200 * 200)
	{
		if (cos(csnake.o)*(snake.body[k].y - csnake.body[0].y) - sin(csnake.o)*(snake.body[k].x - csnake.body[0].x)>0)

			return 9;
		else
			return 8;
	}
	return 0;
}

void GameManeger::cmove()
{
	for (int j = 0;j < csnake.size();j++) {

		static int turn = 0;
		if (turn == 12 || turn == -12)
			turn = 0;
		if (turn == 0)
		{
			csnake[j].v = 5;
			if (tan(csnake[j].o*3.141 / 180) != tan(snake.o*3.141 / 180))
			{
				if (cos(csnake[j].o*3.141 / 180) != 0 && cos(snake.o*3.141 / 180) != 0)
				{
					double xx, yy;
					xx = (snake.body[0].y - csnake[j].body[0].y + tan(csnake[j].o*3.141 / 180)*csnake[j].body[0].x - tan(snake.o*3.141 / 180)*snake.body[0].x) / (tan(csnake[j].o*3.141 / 180) - tan(snake.o*3.141 / 180));
					yy = tan(csnake[j].o)*(xx - csnake[j].body[0].x) + csnake[j].body[0].y;
					if (((xx - csnake[j].body[0].x)*(xx - csnake[j].body[0].x) + (yy - csnake[j].body[0].y)*(yy - csnake[j].body[0].y))/csnake[j].v/ csnake[j].v < ((xx - snake.body[0].x)*(xx - snake.body[0].x)
						+ (yy - snake.body[0].y)*(yy - snake.body[0].y))/snake.v/ snake.v && (snake.body[0].x - csnake[j].body[0].x)*(snake.body[0].x - csnake[j].body[0].x)
						+ (snake.body[0].y - csnake[j].body[0].y)*(snake.body[0].y - csnake[j].body[0].y) < 6 * 10000  && cos(csnake[j].o*3.141 / 180)*(xx - csnake[j].body[0].x) + sin(csnake[j].o*3.141 / 180)*(yy - csnake[j].body[0].y) > 0)
					{
						csnake[j].v = 20;
						if (cos(csnake[j].o*3.141/180)*(snake.body[0].y - csnake[j].body[0].y) - sin(csnake[j].o*3.141 / 180)*(snake.body[0].x - csnake[j].body[0].x) > 0)
							turn = 1;
						else
							turn = -1;
					}
				}
			}
			if (turn == 0)
			{
				if (safe(csnake[j]) == 8)
					turn = -11;
				else if (safe(csnake[j]) == 9)
					turn = 11;
				else if (csnake[j].body[0].x - 35 - 50 < 803 && 90 < csnake[j].o && csnake[j].o < 180 || csnake[j].body[0].x + 35 + 50 > 5150 && csnake[j].o<360 && csnake[j].o>270 || csnake[j].body[0].y - 35 - 50<440 && csnake[j].o<270 && csnake[j].o>180 || csnake[j].body[0].y + 35 + 50 > 2707 && csnake[j].o < 90 && csnake[j].o > 0)
					turn = -1;
				else if (csnake[j].body[0].x - 35 - 50 < 803 && 180 < csnake[j].o && csnake[j].o < 270 || csnake[j].body[0].x + 35 + 50 > 5150 && csnake[j].o < 90 && csnake[j].o > 0 || csnake[j].body[0].y - 35 - 50<440 && csnake[j].o<360 && csnake[j].o>270 || csnake[j].body[0].y + 35 + 50 > 2707 && csnake[j].o < 180 && csnake[j].o >90)
					turn = 1;
			}
		}
		if (turn == 0)
		{
			if (rand() % 2 == 1)
			{
				csnake[j].o = csnake[j].o + rand() % 8 + 1;
			}
			else
			{
				csnake[j].o = csnake[j].o - rand() % 8 - 1;
			}
			if (csnake[j].o < 0)
				csnake[j].o = 360;
			if (csnake[j].o > 360)
				csnake[j].o = 0;
		}
		if (turn == -1)
		{
			csnake[j].o = csnake[j].o - 8;
			turn = 0;
		}
		if (turn == 1)
		{
			csnake[j].o = csnake[j].o + 8;
			turn = 0;
		}
		if (turn > -12 && turn < 0)
		{
			csnake[j].o = csnake[j].o - 8;
			turn++;
		}
		if (turn < 12 && turn>0)
		{
			csnake[j].o = csnake[j].o + 8;
			turn--;
		}
		if (turn > 12)
		{
			csnake[j].o = csnake[j].o + 8;
			turn--;
		}
		if (turn < -12)
		{
			csnake[j].o = csnake[j].o - 8;
			turn++;
		}
		if (csnake[j].body.size() <= 10)
			csnake[j].v = 5;
		static int l = 25;
		if (csnake[j].v != 5)
		{
			l--;
			if (l == 0)
			{
				l = 25;
				if (csnake[j].body.size() > 1)
					csnake[j].body.pop_back();
			}
		}
		for (int i = csnake[j].body.size() - 1; i > 0; i--)
		{
			csnake[j].body[i].x = csnake[j].body[i - 1].x;
			csnake[j].body[i].y = csnake[j].body[i - 1].y;
		}
		csnake[j].body[0].x += csnake[j].v * cos(csnake[j].o*3.14159 / 180);
		csnake[j].body[0].y += csnake[j].v * sin(csnake[j].o*3.14159 / 180);
	}
}

void GameManeger::ceat()
{
	for (int j = 0;j < csnake.size();j++) {

		if (csnake[j].body.size() <= 100)
		{/*
			static int timer = 100;
			timer--;
			if (timer < 2) {
				timer = 100;
				csnake[j].body.push_back(::body(0, 0));
			}*/
			for (int i = 0; i < food.size(); i++)
			{
				if ((food[i].x - csnake[j].body[0].x)*(food[i].x - csnake[j].body[0].x ) + (food[i].y - csnake[j].body[0].y )*(food[i].y - csnake[j].body[0].y ) < 75 *75)
				{
					csnake[j].body.push_back(::body(0, 0));
					food.erase(food.begin() + i);

				}
			}
		}
	}
	

}

int GameManeger::cdie()
{
	for (int j = 0;j < csnake.size();j++) {

		if (csnake[j].body[0].x - 2<803 || csnake[j].body[0].x + 2 > 5198 || csnake[j].body[0].y - 2<452 || csnake[j].body[0].y + 2 > 2707)
		{
			for (int i = 1; i < csnake[j].body.size(); i++)
			{
				if (i % 6 == 0) {
					food.push_back(::Food(csnake[j].body[i].x, csnake[j].body[i].y, rand() % 5));
				}

			}
			if (csnake[j].body.size() != 0) {
				csnake.erase(csnake.begin() + j);
			}
			return 1;
		}
		for (int i = snake.body.size() - 1; i > 0; i--)
		{
			if ((snake.body[i].x - csnake[j].body[0].x)*(snake.body[i].x - csnake[j].body[0].x) + (snake.body[i].y - csnake[j].body[0].y)*(snake.body[i].y - csnake[j].body[0].y) <= 70 * 70)
			{
				for (int i = 1; i < csnake[j].body.size(); i++)
				{
					if (i % 6 == 0) {
						food.push_back(::Food(csnake[j].body[i].x, csnake[j].body[i].y, rand() % 5));
					}
				}
				if (csnake[j].body.size() != 0) {
					csnake.erase(csnake.begin() + j);
				}
				return 1;
			}
		}
		if ((snake.body[0].x - csnake[j].body[0].x )*(snake.body[0].x - csnake[j].body[0].x ) + (snake.body[0].y - csnake[j].body[0].y )*(snake.body[0].y - csnake[j].body[0].y ) <= 70 * 70)
		{
			if (snake.body.size() > csnake[j].body.size())
			{
				for (int i = 1; i < csnake[j].body.size(); i++)
				{
					if (i % 6 == 0) {
						food.push_back(::Food(csnake[j].body[i].x, csnake[j].body[i].y, rand() % 5));
					}
				}
				if (csnake[j].body.size() != 0) {
					csnake.erase(csnake.begin() + j);
				}
				return 1;
			}
		}
		return 0;
	}
}

void GameManeger::attack()
{
}

void GameManeger::defence()
{
}

void GameManeger::setCsnake()
{
	for (int i = 0;i < 3;i++) {
		csnake.push_back(::Csnake());
	}
}


#include "MyApp.h"



MyApp theApp;

#include "MyApp.h"
#include "MyWnd.h"

BOOL MyApp::InitInstance()
{
	MyWnd *pf = new MyWnd;
	
	pf->ShowWindow(m_nCmdShow);
	pf->UpdateWindow();
	this->m_pMainWnd = pf;
	return TRUE;
}

#include "MySnake.h"



MySnake::MySnake() {

	v = 5;
	o = 135;
	for (int i = 0; i < 20; i++)
	{
		body.push_back(::body());
	}
}
		

#include "MyWnd.h"



MyWnd::MyWnd()
{
	
	Create(NULL, "War of Snakes");
	MoveWindow(80, 50, 1600, 1000, FALSE);
	m_hIcon = AfxGetApp()->LoadIcon("icon.ico");
	SetIcon(m_hIcon, true);
	CClientDC dc(this);
	GetClientRect(&rect);
	mdc = new CDC;
	mdc->CreateCompatibleDC(&dc);
	tempdc = new CDC;
	tempdc->CreateCompatibleDC(&dc);
	tempbmp = new CBitmap;
	tempbmp->CreateCompatibleBitmap(&dc, rect.right - rect.left, rect.bottom - rect.top);
	tempdc->SelectObject(tempbmp);
	vectroy = new CBitmap;
	vectroy->m_hObject = LoadImage(NULL, "vectroy.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	lose = new CBitmap;
	lose->m_hObject = LoadImage(NULL, "lose.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	skinBmp = new CBitmap;
	skinBmp->m_hObject = LoadImage(NULL, "skin.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	Cu = new CBitmap;
	Cu->m_hObject = LoadImage(NULL, "Cu.bmp", IMAGE_BITMAP, 140, 70, LR_LOADFROMFILE);
	toubu = new CBitmap;
	toubu->m_hObject = LoadImage(NULL, "toubu.bmp", IMAGE_BITMAP, 60, 30, LR_LOADFROMFILE);
	red = new CBitmap;
	red->m_hObject = LoadImage(NULL, "red.bmp", IMAGE_BITMAP, 140, 70, LR_LOADFROMFILE);
	blue = new CBitmap;
	blue->m_hObject = LoadImage(NULL, "blue.bmp", IMAGE_BITMAP, 140, 70, LR_LOADFROMFILE);
	rule = new CBitmap;
	rule->m_hObject = LoadImage(NULL, "rule.bmp", IMAGE_BITMAP, rect.right, rect.bottom, LR_LOADFROMFILE);
	bg = new CBitmap;
	bg->m_hObject = LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, rect.right, rect.bottom, LR_LOADFROMFILE);
	bgg = new CBitmap;
	bgg->m_hObject = LoadImage(NULL, "bgg.bmp", IMAGE_BITMAP, 500, 500, LR_LOADFROMFILE);
	gameBg = new CBitmap;
	gameBg->m_hObject = LoadImage(NULL, "ground.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	SetTimer(1, 10, NULL);
	

	for (int i = 0; i < 5; i++)
	{
		foods[i] = new CBitmap;
		sprintf_s(foodName, "food%d.bmp", (i + 1));
		foods[i]->m_hObject = LoadImage(NULL, foodName,	IMAGE_BITMAP, 160, 80, LR_LOADFROMFILE);

	}
}


MyWnd::~MyWnd()
{
}
BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd)
	ON_WM_PAINT()
	ON_WM_TIMER()

	ON_WM_LBUTTONDOWN()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


void MyWnd::OnPaint()
{
	CPaintDC dc(this); 
	sndPlaySoundW(L"music.wav", SND_ASYNC | SND_LOOP);
}



void MyWnd::OnTimer(UINT_PTR nIDEvent)
{
	CClientDC dc(this);
	CPoint point;
	

	switch (state)
	{
	case 0:
		
		mdc->SelectObject(bg);
		tempdc->BitBlt(0, 0, rect.right, rect.bottom, mdc, 0, 0, SRCCOPY);
		mdc->SelectObject(bgg);
		tempdc->BitBlt(pos, 150, 500, 500, mdc, 0, 0, SRCAND);
		dc.BitBlt(0, 0, 1600, 1000, tempdc, 0, 0, SRCCOPY);
		if (pos <= 480) {
			pos += 10;
		}
		
		break;
	
	case 1:
		
		

		GetCursorPos(&point);

		x = point.x - 5 - 800;
	    y = point.y - 37 - 450;

		if (cos(gm.snake.o*3.14159 / 180)*y - x * sin(gm.snake.o*3.14159 / 180) < 0)
			gm.snake.o -= 8;
		if (cos(gm.snake.o*3.14159 / 180)*y - x * sin(gm.snake.o*3.14159 / 180) > 0)
			gm.snake.o += 8;
		if (gm.snake.o == 360)
			gm.snake.o = 0;
		if (gm.snake.o == -1)
			gm.snake.o == 359;
		if(!isEnd)   gm.move();
		
		
		mdc->SelectObject(gameBg);
		
		tempdc->BitBlt(0, 0, 1600, 1000, mdc, gm.snake.body[0].x - 800, gm.snake.body[0].y - 450, SRCCOPY);
		srand(GetTickCount());
		if (gm.food.size() < 80) {
			if (rand() % 1000 < 50)
				gm.food.push_back(::Food(845 + rand() % (4392), 492 + rand() % 2160, rand() % 5));
		}
		
		for (int i = 0; i < gm.food.size(); i++)
		{
			if (gm.food[i].z == 0)
			{
				mdc->SelectObject(foods[0]);			
			}
			
			if (gm.food[i].z == 1)
			{
				mdc->SelectObject(foods[2]);				
			}
			if (gm.food[i].z == 2)
			{
				mdc->SelectObject(foods[3]);				
			}
			if (gm.food[i].z == 3)
			{
				mdc->SelectObject(foods[1]);								
			}
			if (gm.food[i].z == 4)
			{
				mdc->SelectObject(foods[4]);
				
			}
			tempdc->BitBlt(760 + gm.food[i].x - gm.snake.body[0].x, 420 + gm.food[i].y - gm.snake.body[0].y , 80, 80, mdc, 80, 0, SRCAND);
			tempdc->BitBlt(760 + gm.food[i].x - gm.snake.body[0].x , 420 + gm.food[i].y - gm.snake.body[0].y , 80, 80, mdc, 0, 0, SRCPAINT);
		}

		gm.eat();

		gm.die();
		if (gm.die() == 1) {
			gm.life--;
			gm.snake.body[0].x = 2964;
			gm.snake.body[0].y = 1544;
			for (int i = gm.snake.body.size() - 1;i > 20;i--) {
				gm.snake.body.pop_back();
			}
		}
		mdc->SelectObject(red);
		gm.cmove();
		gm.ceat();
		gm.cdie();
		for (int j = 0;j < gm.csnake.size();j++) {
			
			for (int i = gm.csnake[j].body.size() - 1; i >= 0; i--)
			{
				if (i % 3 == 0) {

					tempdc->BitBlt(800 + gm.csnake[j].body[i].x - gm.snake.body[0].x - 35, 430 + gm.csnake[j].body[i].y - gm.snake.body[0].y , 70, 70, mdc, 70, 0, SRCAND);
					tempdc->BitBlt(800 + gm.csnake[j].body[i].x - gm.snake.body[0].x - 35, 430 + gm.csnake[j].body[i].y - gm.snake.body[0].y, 70, 70, mdc, 0, 0, SRCPAINT);
				}

			}
		}
		if (skin == 1) {
			mdc->SelectObject(Cu);
		}
		if (skin == 2) {
			mdc->SelectObject(red);
		}
		if (skin == 3) {
			mdc->SelectObject(blue);
		}
		
		for (int i = gm.snake.body.size() - 1; i >= 0; i--)
		{
			if (i % 3 == 0) {

				tempdc->BitBlt(800 + gm.snake.body[i].x - gm.snake.body[0].x - 35, 430 + gm.snake.body[i].y - gm.snake.body[0].y , 70, 70, mdc, 70, 0, SRCAND);
				tempdc->BitBlt(800 + gm.snake.body[i].x - gm.snake.body[0].x - 35, 430 + gm.snake.body[i].y - gm.snake.body[0].y , 70, 70, mdc, 0, 0, SRCPAINT);
			}

		}
		mdc->SelectObject(toubu);
		tempdc->BitBlt(800 + 20 * cos((gm.snake.o - 90)*3.141/180) - 12, 450 + 26 * sin((gm.snake.o - 90)*3.141 / 180) - 12, 30, 30, mdc, 0, 0, SRCAND);
		tempdc->BitBlt(800 + 20 * cos((gm.snake.o - 90)*3.141/180) - 12, 450 + 26 * sin((gm.snake.o - 90)*3.141 / 180) - 12, 30, 30, mdc, 30, 0, SRCPAINT);
		tempdc->BitBlt(800 + 20 * cos((gm.snake.o + 90)*3.141 / 180) - 12, 450 + 26 * sin((gm.snake.o + 90)*3.141 / 180) - 12, 30, 30, mdc, 0, 0, SRCAND);
		tempdc->BitBlt(800 + 20 * cos((gm.snake.o + 90)*3.141 / 180) - 12, 450 + 26 * sin((gm.snake.o + 90)*3.141 / 180) - 12, 30, 30, mdc, 30, 0, SRCPAINT);

		if (gm.csnake.size() == 0) {
			 isEnd = true;
			mdc->SelectObject(vectroy);
			tempdc->BitBlt(275, 286, 1050, 408, mdc, 1050, 0, SRCAND);
			tempdc->BitBlt(275, 286, 1050, 408, mdc, 0, 0, SRCPAINT);
		
		}
		else {
			if (gm.life == 0) {
				isEnd = true;
				mdc->SelectObject(lose);
				tempdc->BitBlt(275, 286, 1050, 408, mdc, 1050, 0, SRCAND);
				tempdc->BitBlt(275, 286, 1050, 408, mdc, 0, 0, SRCPAINT);
				
			}
		}
		dc.BitBlt(0, 0, 1600, 1000, tempdc, 0, 0, SRCCOPY);
		break;
	case 2:
		mdc->SelectObject(rule);
		dc.BitBlt(0, 0, rect.right, rect.bottom, mdc, 0, 0, SRCCOPY);
		break;
	case 3:
		mdc->SelectObject(skinBmp);
		dc.BitBlt(0, 0, 1600, 1000, mdc, 0, 0, SRCCOPY);
		break;
	default:
		break;
	}
	
	
	CFrameWnd::OnTimer(nIDEvent);
}





void MyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{

	if (state == 1 && isEnd) {
		state = 0;
	}
	
	if (state == 2 && point.x >= rect.right - 350 && point.x <= rect.right-150 && point.y >= 180 && point.y <= 350) {
		state = 0;
	}
	if (state==0&&point.x >= 680 && point.x <= 810 && point.y >= rect.bottom-170 && point.y <= rect.bottom-130) {
		gm.setCsnake();
		isEnd = false;
		gm.life = 3;
		state = 1;
		for (int i = gm.snake.body.size() - 1;i > 20;i--) {
			gm.snake.body.pop_back();
		}
	}
	if (state == 0 && point.x >= 440 && point.x <= 520 && point.y >= rect.bottom - 170 && point.y <= rect.bottom - 130) {
		state = 2;
	}
	if (state == 0 && point.x >= 970 && point.x <= 1050 && point.y >= rect.bottom - 170 && point.y <= rect.bottom - 130) {
		state = 3;
	}
	if (state == 1&& gm.snake.body.size()>=10) {
		gm.snake.v = 10;
	}
	if (state == 3) {
		if (point.x > 179 && point.x < 517 && point.y>317 && point.y < 779) {
			skin = 1;
			state = 0;
		}
		if (point.x > 642 && point.x < 982 && point.y>305 && point.y < 774) {
			skin = 2;
			state = 0;
		}
		if (point.x > 1100 && point.x < 1410 && point.y>317 && point.y < 770) {
			skin = 3;
			state = 0;
		}
	}
	
	CFrameWnd::OnLButtonDown(nFlags, point);
}


void MyWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if (nChar == VK_ESCAPE) {
		state = 0;
	}

	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}


void MyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
	if (state == 1) {
		gm.snake.v = 5;
	}

	CFrameWnd::OnLButtonUp(nFlags, point);
}

其中的bitmap可以自行換掉,ontimer中貼圖改變下尺寸就ok了。
如果想要完整工程可以去我的資源中找。