自己編寫的一個小游戲,本來打算做貪吃蛇,結果不會使蛇的身子隨蛇頭方向改變而改變就換了種想法,最后變成了這樣一個另類的小游戲,“笑哭“,下面是程序的主要代碼,如果有興趣也可以下載完整程序代碼資源:https://download.csdn.net/download/yt201758501112/10488198。
Lessonx.h
/
//
//
//
//
/
#ifndef _LESSON_X_H_
#define _LESSON_X_H_
//
#include <Windows.h>
#include <stdio.h>
#include <vector>
using namespace std;
/
//
// 游戲總管類。負責處理游戲主循環、游戲初始化、結束等工作
class CGameMain
{
private:
int m_iGameState; // 游戲狀態,0:結束或者等待開始;1:初始化;2:游戲進行中
? ? CSprite * m_pControlSnake; ? ? ? ? //定義蛇頭部精靈
? ? CSprite * m_pSnakebody1; ? ? ? ? ? ?//定義一節蛇身
? ? CSprite * tmpsprite; ? ? ? ? ? ? //定義兩個中間變量精靈
? ? CSprite * tmpsprite1;
? ? vector<CSprite*> ?m_pSnakefood; ? ? ?//定義食物精靈組
? ? vector<CSprite*> ?m_pzhangai; ? ? ? ?//定義障礙精靈組
? ? CTextSprite * m_pStarttext; ? ? ?//定義開始文本精靈
? ? CTextSprite * m_pgamescore; ? ? ?//定義分數文本精靈
? ? CTextSprite * m_plevel; ? ? ? ?//定義級別文本精靈
? ? CSprite * m_pgameover; ? ? ? ? ?//定義游戲結束時背景精靈
? ? CSprite * m_pbeijing; ? ? ? ? ?//定義開始時背景精靈
? ? CTextSprite * m_ptime; ? ? ? ? //定義游戲時間文本精靈
? ? CTextSprite * m_pend1; ? ? ? ? //定義文本精靈
? ? CTextSprite * m_pend2;
? ? CSound * m_bjmusic; ? ? ? ? ?//定義音效精靈
? ? CSound * m_eatmusic;
? ? int m_foodminX; ? ? ? ?//定義食物出現的最小X坐標
? ? int m_foodmaxX; ? ? ? ? //定義食物出現的最大X坐標
? ? int m_foodminY; ? ? ? ? //定義食物出現的最小Y坐標
? ? int m_foodmaxY; ? ? ? ? //定義食物出現的最大Y坐標
? ? int m_foodcount; ? ? ? ?//定義食物出現的數目
? ? int m_zhangaimaxY; ? ? ?//定義障礙出現的最大Y坐標
? ? int m_zhangaiminY; ? ? ?//定義障礙出現的最小Y坐標
? ? int m_zhangaicount; ? ? //定義障礙出現的數目
? ? float DeltaTime; ? ? ? ?//定義時間
? ? float speedsnake; ? ? ? //定義蛇移動的速度
? ? int weizhi; ? ? ? ?//定義需要的變量
? ? int judge;
? ? float speedbeijing;
? ? int t;
? ? char foodname[1000]; ? ? ?//定義需要存儲的數組
? ? char zhangainame[1000];
? ? int fenshu; ? ? ? ? //定義級別,分數
? ? int level;
? ? float mintime,maxtime;
? ? float basetime;
public:
CGameMain(); ? ? ? ? ? ?//構造函數
~CGameMain(); ? ? ? ? ? //析構函數
// Get方法
int GetGameState() { return m_iGameState; }
// Set方法
void SetGameState( const int iState ) { m_iGameState = iState; }
// 游戲主循環等
void GameMainLoop( float fDeltaTime );
void GameInit();
void GameRun( float fDeltaTime );
void GameEnd();
void OnMouseMove( const float fMouseX, const float fMouseY );
void OnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY );
void OnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY );
void OnKeyDown( const int iKey, const bool bAltPress, const bool bShiftPress, const bool bCtrlPress );
void OnKeyUp( const int iKey );
void OnSpriteColSprite( const char *szSrcName, const char *szTarName );
void OnSpriteColWorldLimit( const char *szName, const int iColSide );
};
/
//
extern CGameMain g_GameMain;
?
#endif // _LESSON_X_H_
?
?
?
?
Lessonx.cpp
/
//
//
//
//
/
#include <Stdio.h>
#include "CommonClass.h"
#include "LessonX.h"
#include <algorithm>
#include <cstring>
#include <time.h>
#include <stdlib.h>
#include<windows.h>
#pragma comment(lib,"winmm.lib")
//
//
CGameMain g_GameMain;
//==============================================================================
//
// 大體的程序流程為:GameMainLoop函數為主循環函數,在引擎每幀刷新屏幕圖像之后,都會被調用一次。
//==============================================================================
//
// 構造函數
CGameMain::CGameMain()
{
m_iGameState = 0;
m_pControlSnake =new CSprite("Snake_0"); ? ? ? ?//導入精靈
m_pSnakebody1 =new CSprite("snake_1");
m_pStarttext =new CTextSprite("start"); ? ? ? ?//導入文本精靈
m_pgamescore=new CTextSprite("gamescore");
m_plevel=new CTextSprite("level");
m_pgameover=new CSprite("gameend");
m_pbeijing=new CSprite("beijing");
m_ptime=new CTextSprite("time");
m_bjmusic=new CSound("bjmusic.wav",1,1); ? ? ? ?//導入音樂精靈
m_eatmusic=new CSound("eatmusic.wav",0,1);
m_pend1=new CTextSprite("end1");
m_pend2=new CTextSprite("end2");
m_foodmaxX=0; ? ? ? ? ?//初始化變量
m_foodmaxY=0;
m_foodminX=0;
m_foodminY=0;
m_foodcount=0;
DeltaTime=0;
? ? speedbeijing=10.f;
? ? speedsnake=15.f;
? ? basetime=0.f;
? ? m_iHook=0;
weizhi=0;
judge=1;
t=0;
fenshu=0;
level=1;
mintime=2.f;
maxtime=4.f;
iTime=0;
}
//==============================================================================
//
// 析構函數
CGameMain::~CGameMain()
{
}
//==============================================================================
//
// 游戲主循環,此函數將被不停的調用,引擎每刷新一次屏幕,此函數即被調用一次
// 用以處理游戲的開始、進行中、結束等各種狀態.
// 函數參數fDeltaTime : 上次調用本函數到此次調用本函數的時間間隔,單位:秒
void CGameMain::GameMainLoop( float fDeltaTime )
{
switch( GetGameState() )
{
// 初始化游戲,清空上一局相關數據
case 1:
{
GameInit();
SetGameState(2); // 初始化之后,將游戲狀態設置為進行中
}
break;
// 游戲進行中,處理各種游戲邏輯
case 2:
{
// TODO 修改此處游戲循環條件,完成正確游戲邏輯
if( true )
{
GameRun( fDeltaTime );
}
else // 游戲結束。調用游戲結算函數,并把游戲狀態修改為結束狀態
{
SetGameState(0);
GameEnd();
}
}
break;
// 游戲結束/等待按空格鍵開始
case 0:
default:
break;
};
}
//=============================================================================
//
// 每局開始前進行初始化,清空上一局相關數據
void CGameMain::GameInit()
{
}
//=============================================================================
//
// 每局游戲進行中
void CGameMain::GameRun( float fDeltaTime )
{
? ? ? ?DeltaTime+=fDeltaTime; ? ? ? //累加時間來判斷是否產生障礙
? ? ? ?basetime+=fDeltaTime; ? ? ? ? ? //累加時間以用來計時
? ? ?m_ptime->SetTextValueFloat(basetime);
? ? ?if(judge==1) ? ? ? ? ? ?//判斷當蛇頭吃到食物時產生新的食物
? ? ?{
? ? ? ? sprintf(foodname,"food%d",m_foodcount++); ? ? ? ? ?//改變食物的名字
? ? ? ? tmpsprite=new CSprite(foodname); ? ? ? ? ?//初始化中間精靈
? ? ? ? tmpsprite->CloneSprite("food0"); ? ? ? ?//克隆初始食物精靈
? ? ? ? m_foodmaxX=31; ? ? ? ? ? ? //根據原圖判斷邊界坐標
? ? ? ? m_foodminX=-49;
? ? ? ? m_foodmaxY=31;
? ? ? ? m_foodminY=-36;
? ? ? ? int iposX=CSystem::RandomRange(m_foodminX,m_foodmaxX); ? ? ? ? ? //隨機產生食物的X坐標
? ? ? ? int iposY=CSystem::RandomRange(m_foodminY,m_foodmaxY); ? ? ? ? ? //隨機產生食物的Y坐標
? ? ? ? tmpsprite->SetSpritePosition(iposX,iposY); ? ? ? ? ? ? ? //根據隨機產生的坐標將精靈置入游戲界面中
? ? ? ? m_pControlSnake->SetSpriteCollisionSend(true); ? ? ? ? ? //設置頭部精靈發送信號
? ? ? ? tmpsprite->SetSpriteCollisionReceive(true); ? ? ? ? ? ?//設置食物精靈接受信號
? ? ? ? judge=0; ? ? ? ? ? ? //把判斷值初始化
? ? ?}
? ? ?if(DeltaTime>mintime&&DeltaTime<maxtime) ? ? ? ? ? ? //當達到相應的時間時產生一障礙
? ? ?{
? ? ? ? ?sprintf(zhangainame,"box%d",m_zhangaicount++); ? ? ? ? ? //改變障礙的名字
? ? ? ? ?tmpsprite1=new CSprite(zhangainame); ? ? ? ? ? ?//初始化中間變量精靈
? ? ? ? ?tmpsprite1->CloneSprite("box0"); ? ? ? ? ? ? //克隆初始障礙
? ? ? ? ?m_zhangaiminY=-35; ? ? ? ? ? ? //根據原圖判斷坐標范圍
? ? ? ? ?m_zhangaimaxY=35;
? ? ? ? ?int posY=CSystem::RandomRange(m_zhangaiminY,m_zhangaimaxY); ? ? ? ? ? ? //隨機產生Y值
? ? ? ? ?int posX=50;
? ? ? ? ?tmpsprite1->SetSpritePosition(posX,posY); ? ? ? ? ? ? //置入障礙
? ? ? ? ?tmpsprite1->SetSpriteCollisionReceive(true); ? ? ? ? ? ? ? //設置障礙可以接受信號
? ? ? ? ?tmpsprite1->SetSpriteLinearVelocity(-speedbeijing,0); ? ? ? ? ? //設置障礙的移動速度
? ? ? ? ?DeltaTime=0; ? ? ? ? ?//初始化變量
? ? ?}
? ? ?if(fenshu>=50&&fenshu<=100) ? ? ? ? ? ? //根據分數的不同變化關卡
? ? ?{
? ? ? ? speedbeijing=15.f; ? ? ? ? ? ? ? //設置第二關障礙,蛇的移動速度
? ? ? ? speedsnake=20.f;
? ? ? ? level=2; ? ? ? ? ?//改變關卡級別
? ? ? ? m_plevel->SetTextValue(level);
? ? ? ? mintime=1.8; ? ? ? ? //改變障礙出現的時間間隔
? ? ? ? maxtime=3.6;
? ? ? ?// m_pbeijing->SetSpriteLinearVelocity(speedbeijing,0);
? ? ?}
? ? ?if(fenshu>100&&fenshu<=150)
? ? ?{
? ? ? ? ?speedbeijing=20.f; ? ? ? ? //設置第三關障礙,蛇的移動速度
? ? ? ? ?speedsnake=25.f;
? ? ? ? ?level=3; ? ? ? ? ? //改變關卡級別
? ? ? ? m_plevel->SetTextValue(level);
? ? ? ? mintime=1.6; ? ? ? ? ? ?//改變障礙出現的時間間隔
? ? ? ? maxtime=3.2;
? ? ? ?// m_pbeijing->SetSpriteLinearVelocity(speedbeijing,0);
? ? ?}
? ? ?if(fenshu>150&&fenshu<=200)
? ? ?{
? ? ? ? ?speedbeijing=25.f; ? ? ? ? ? ? //設置第四關障礙,蛇的移動速度
? ? ? ? ?speedsnake=30.f;
? ? ? ? ?level=4; ? ? ? ? ? //改變關卡級別
? ? ? ? m_plevel->SetTextValue(level);
? ? ? ? mintime=1.4; ? ? ? ? ? ?//改變障礙出現的時間間隔
? ? ? ? maxtime=2.8;
? ? ?// ? m_pbeijing->SetSpriteLinearVelocity(speedbeijing,0);
? ? ?}
? ? ?if(fenshu>200)
? ? ?{
? ? ? ? ?speedbeijing=30.f; ? ? ? ? ? ? //設置第五關障礙,蛇的移動速度
? ? ? ? ?speedsnake=35.f;
? ? ? ? ?level=5; ? ? ? ? ? //改變關卡級別
? ? ? ? ?m_plevel->SetTextValue(level);
? ? ? ? ?mintime=1.0; ? ? ? ? ? //改變障礙出現的時間間隔
? ? ? ? ?maxtime=2;
? ? ? //s ? m_pbeijing->SetSpriteLinearVelocity(speedbeijing,0);
? ? ?}
}
//=============================================================================
//
// 本局游戲結束
void CGameMain::GameEnd()
{
}
//==========================================================================
//
// 鼠標移動
// 參數 fMouseX, fMouseY:為鼠標當前坐標
void CGameMain::OnMouseMove( const float fMouseX, const float fMouseY )
{
}
//==========================================================================
//
// 鼠標點擊
// 參數 iMouseType:鼠標按鍵值,見 enum MouseTypes 定義
// 參數 fMouseX, fMouseY:為鼠標當前坐標
void CGameMain::OnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{
}
//==========================================================================
//
// 鼠標彈起
// 參數 iMouseType:鼠標按鍵值,見 enum MouseTypes 定義
// 參數 fMouseX, fMouseY:為鼠標當前坐標
void CGameMain::OnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY )
{
}
//==========================================================================
//
// 鍵盤按下
// 參數 iKey:被按下的鍵,值見 enum KeyCodes 宏定義
// 參數 iAltPress, iShiftPress,iCtrlPress:鍵盤上的功能鍵Alt,Ctrl,Shift當前是否也處于按下狀態(0未按下,1按下)
void CGameMain::OnKeyDown( const int iKey, const bool bAltPress, const bool bShiftPress, const bool bCtrlPress )
{
? ? if( KEY_SPACE == iKey && 0 == GetGameState() )
{
SetGameState( 1 );
? ? ? ? m_pStarttext->SetSpriteVisible(false); ? ? ? ? ?//設置當按下空格時開始文本精靈消失
? ? }
? ? float fSpeedX=0.f,fSpeedY=0.f;
? ? float fdirector=0.f;
? ? switch(iKey) ? ? ? ? ? ? //設置按鍵對蛇頭精靈速度的影響
? ? {
? ? ? ? case KEY_W:
? ? ? ? ? ? fSpeedY=-speedsnake;
? ? ? ? ? ? fdirector=270.f;
? ? ? ? ? ? break;
? ? ? ? case KEY_A:
? ? ? ? ? ? fSpeedX=-speedsnake;
? ? ? ? ? ? fdirector=180.f;
? ? ? ? ? ? break;
? ? ? ? case KEY_S:
? ? ? ? ? ? fSpeedY=speedsnake;
? ? ? ? ? ? fdirector=90.f;
? ? ? ? ? ? break;
? ? ? ? case KEY_D:
? ? ? ? ? ? fSpeedX=speedsnake;
? ? ? ? ? ? fdirector=0.f;
? ? ? ? ? ? break;
? ? };
? ? m_pControlSnake->SetSpriteLinearVelocity(fSpeedX, fSpeedY); ? ? ? //改變蛇頭的速度
? ? m_pControlSnake->SetSpriteRotation(fdirector); ? ? ? ? ? ?//改變蛇頭的朝向
}
//==========================================================================
//
// 鍵盤彈起
// 參數 iKey:彈起的鍵,值見 enum KeyCodes 宏定義
void CGameMain::OnKeyUp( const int iKey )
{
}
//==========================================================================
//
// 精靈與精靈碰撞
// 參數 szSrcName:發起碰撞的精靈名字
// 參數 szTarName:被碰撞的精靈名字
void CGameMain::OnSpriteColSprite( const char *szSrcName, const char *szTarName)
{
? ? if(stricmp("snake_0",szSrcName)==0&&stricmp(foodname,szTarName)==0) ? ? ? ? ? //當蛇頭與食物碰撞時
? ? {
? ? ? ? weizhi=weizhi-2;
? ? ? ? tmpsprite->SpriteMountToSprite("snake_0",weizhi,0); ? ? ? ? ? //將食物接在蛇頭后面
? ? ? ? judge=1; ? ? ? ? ? ? //改變判斷變量
? ? ? ? fenshu+=10; ? ? ? ? ?//改變分數
? ? ? ? m_pgamescore->SetTextValue(fenshu);
? ? ? ? PlaySound("E:\\eatmusic.wav",0,SND_FILENAME|SND_ASYNC); ? ? ? ?//播放吃食物的音效
? ? }
? ? else
? ? ? ? judge=0;
? ? if(stricmp("snake_0",szSrcName)==0&&stricmp(foodname,szTarName)!=0) ? ? ? ? ? ? //當蛇頭與障礙碰撞時
? ? {
? ? ? ? m_pControlSnake->DeleteSprite(); ? ? ? ? ? ? ?//刪除蛇頭與蛇身
? ? ? ? m_pgameover->SetSpritePosition(0,0); ? ? ? ? ?//導入游戲結束界面
? ? ? ? m_pgameover->SetSpriteVisible(true);
? ? ? ? m_pend1->SetSpritePosition(-2.5,0); ? ? ? ? ? //導入游戲終結文本
? ? ? ? m_pend2->SetSpritePosition(-4.9,6);
? ? ? ? m_pend2->SetTextValue(fenshu);
? ? }
}
//===========================================================================
//
// 精靈與世界邊界碰撞
// 參數 szName:碰撞到邊界的精靈名字
// 參數 iColSide:碰撞到的邊界 0 左邊,1 右邊,2 上邊,3 下邊
void CGameMain::OnSpriteColWorldLimit( const char *szName, const int iColSide )
{
? ? if(stricmp(szName,"snake_0")==0&&iColSide==0) ? ? ? ? ? ? //設置當蛇頭碰到邊界時結束游戲
? ? {
? ? ? ? m_pControlSnake->DeleteSprite();
? ? ? ? m_pgameover->SetSpritePosition(0,0);
? ? ? ? m_pgameover->SetSpriteVisible(true);
? ? ? ? m_pend1->SetSpritePosition(-2.5,0);
? ? ? ? m_pend2->SetSpritePosition(-4.9,6);
? ? ? ? m_pend2->SetTextValue(fenshu);
? ? }
? ? if(stricmp(szName,"snake_0")==0&&iColSide==1)
? ? {
? ? ? ? m_pControlSnake->DeleteSprite();
? ? ? ? m_pgameover->SetSpritePosition(0,0);
? ? ? ? m_pgameover->SetSpriteVisible(true);
? ? ? ? m_pend1->SetSpritePosition(-2.5,0);
? ? ? ? m_pend2->SetSpritePosition(-4.9,6);
? ? ? ? m_pend2->SetTextValue(fenshu);
? ? }
? ? if(stricmp(szName,"snake_0")==0&&iColSide==2)
? ? {
? ? ? ? m_pControlSnake->DeleteSprite();
? ? ? ? m_pgameover->SetSpritePosition(0,0);
? ? ? ? m_pgameover->SetSpriteVisible(true);
? ? ? ? m_pend1->SetSpritePosition(-2.5,0);
? ? ? ? m_pend2->SetSpritePosition(-4.9,6);
? ? ? ? m_pend2->SetTextValue(fenshu);
? ? }
? ? if(stricmp(szName,"snake_0")==0&&iColSide==3)
? ? {
? ? ? ? m_pControlSnake->DeleteSprite();
? ? ? ? m_pgameover->SetSpritePosition(0,0);
? ? ? ? m_pgameover->SetSpriteVisible(true);
? ? ? ? m_pend1->SetSpritePosition(-2.5,0);
? ? ? ? m_pend2->SetSpritePosition(-4.9,6);
? ? ? ? m_pend2->SetTextValue(fenshu);
? ? }
}
?