📄 g2.cpp
字号:
// G2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "G2.h"
#include <stdio.h>
#define MAX_LOADSTRING 100
#define PLAYER1_STARTX 200
#define PLAYER1_STARTY 300
#define PLAYER2_STARTX 500
#define PLAYER2_STARTY 400
#define BONUS_CX 42
#define BONUS_CY 45
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
CGame *g_pGame;
HWND HWnd;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////////////////////
//
//Dis:Control the game
//CGame Class
//
CGame::CGame()
{
m_hInst = NULL;//实体
m_hWnd = NULL;//窗体
m_bActive = FALSE;//是否有效
m_bShowStats = true;//是否显示帧数
m_bSingle = TRUE;//是否单人
g_pGame = this;//指针指向本身
}
CGame::~CGame()
{
}
/////////////////////////////////////////////////////////
//
//
///////////////////////////////////////////////////////////
//随机Max-Min
inline int random( int min, int max )
{
return (min + rand() % (max - min + 1));
}
///////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( g_pGame )//类有效的话
return g_pGame->MsgProc( hWnd, uMsg, wParam, lParam );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
////////////////////////////////////////////////////////
//ItSelf Message Messagement
////////////////////////////////////////////////////////
LRESULT CGame::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc1;
switch( uMsg )
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
/*
case WM_LBUTTONDOWN:
Add(LOWORD (lParam),HIWORD (lParam));
break;*/
//如果按键
case WM_KEYDOWN:
switch(wParam)
{
/* case VK_RIGHT:
MOVE=true;
break;
*/
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_F6:
m_player[0].Reborn();
break;
}
break;
case WM_PAINT:
hdc1 = BeginPaint(hWnd, &ps);
// BitBlt(hdc1,ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right-ps.rcPaint.left,ps.rcPaint.bottom-ps.rcPaint.top,hdcMem,ps.rcPaint.left,ps.rcPaint.top,SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
////////////////////////////////////////////////////////
//运行游戏
////////////////////////////////////////////////////////
int CGame::Run()
{
MSG msg;
while( TRUE )
{//如果有消息在队列
if (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage( &msg, NULL, 0, 0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if( m_bActive )
{
UpdateFrame();
}
else
WaitMessage();
}
DDTerm();
return msg.wParam;
}
///////////////////////////////////////////////////////
//DrawWorld()++++++++++++++++++++++++++++++++++++++++
//画世界
///////////////////////////////////////////////////////
void CGame::DrawWorld()
{
CExlpode* explode;
SelectObject(hdcBuff,m_pBmpList[0]);//m_pBmpList[0]
::StretchBlt(hdcMem,0,0,800,600,hdcBuff,0,0,40,40,SRCCOPY);
//炸弹
//-----------
//炸弹处理
for(int j = 0; j < 2; j ++ )
{
explode=&m_player[0].m_explode[j];
if( !(explode->m_active) )
{
continue;
}
//前提是explode->m_active 有效
//如果没有过5秒(水泡还在)就画水泡
if(!explode->m_explode)
explode->Draw(&hdcMem,&hdcBuff);
else //否则表示水泡要爆拉!!!
// //要爆炸了
explode->Boom(&hdcMem,&hdcBuff);
}
//-------------
//画敌人
for( int i = 0; i < m_nMaxEnemys; i ++ )
{
m_enemy[i].Draw(&hdcMem,&hdcBuff);
}
//画玩家
m_player[0].Draw(&hdcMem,&hdcBuff);
m_bonus.Draw(&hdcMem,&hdcBuff);
}
////////////////////////////////////////////////////////
//Update ! It is soul of this class (CGame Class)
////////////////////////////////////////////////////////
void CGame::UpdateFrame()
{
//Update 时间
static DWORD lastTick = timeGetTime();
static int frameCount;
static float fps;
if(m_gameState==GS_OVER)
{
char buffer2[40]="You Lose ! Ha Ha >_< ";
TextOut(hdc,200, 50, buffer2,strlen(buffer2) );
MessageBox(NULL,"","确定",MB_OK);
ResetGame();
return;
}
ProcessInput();
//
DrawWorld();
//
//帧的计算
frameCount ++;
DWORD thisTick = timeGetTime();
if( thisTick - lastTick > 1000 )
{
fps = (float)frameCount / (thisTick - lastTick) * 1000;
frameCount = 0;
lastTick = thisTick;
}
if( m_bShowStats )
{
// Show FPS
char buf[255];
char buf3[255]="SHIFT 左键为放水弹 空格键为发炮";
sprintf( buf, "FPS: %.2f /Sec Emenys Left: %d", fps,m_nEnemysLeft);
TextOut(hdcMem,0, 0, buf,strlen(buf) );
TextOut(hdcMem,300, 0, buf3,strlen(buf3) );
}
SelectObject(hdcBuff,m_pBmpList[7]);
for(int j=0;j<m_player[0].m_nLife;j++)
::TransparentBlt(hdcMem,j*23,20,19,19,hdcBuff,0,0,19,19,RGB(0,0,255));
::BitBlt(hdc,0,0,700,500,hdcMem,0,0,SRCCOPY);
}
//////////////////////////////////////////////////////
//BoreEnemy() 产生敌人或设定位置(给没有处理的敌人when enemyleft>0)
//
//
//////////////////////////////////////////////////////
BOOL CGame::BoreEnemy()
{
if( m_nEnemysLeft <= 0 )//m_nEnemysLeft 总共要消灭的敌数(要出现的敌数)
return FALSE;
int which = random( 0, 4 );//取0-4
int x, y;
//将对m_nMaxEnemys 个敌人 位置初始化
//每次只对一个无活动的敌人初始化(游戏一开始旧设为敌人无活动)
for( int i = 0; i < m_nMaxEnemys; i ++ )
if( !m_enemy[i].m_active )
{
switch( which )//四个位置
{
case 0:
x = 2; y = 2;
break;
case 1:
x = 194; y = 2;
break;
case 2:
x = 386; y = 2;
break;
case 3:
x = 2; y = 160;
break;
case 4:
x = 386; y = 160;
break;
}
int temp = rand() % 100;
if( temp < 50 )//50%概率 产生类型和速度
{
// m_enemy[i].m_type = 0;
m_enemy[i].m_speed = 3;
}
else if( temp < 80 )
{
// m_enemy[i].m_type = 1;
m_enemy[i].m_speed = 4;
}
else
{
// m_enemy[i].m_type = 2;
m_enemy[i].m_speed = 5;
}
//方向都向下
m_enemy[i].m_x = x;
m_enemy[i].m_y = y;
m_enemy[i].m_dir = DIR_DOWN;
/*
//如果剩下4,8,11 就出现宝物和红时
if( m_nEnemysLeft == 4 ||
m_nEnemysLeft == 11 ||
m_nEnemysLeft == 18 )
{
m_enemy[i].m_bBonus = TRUE;
m_enemy[i].m_redTime = 0;
}
else
m_enemy[i].m_bBonus = FALSE;
if( m_enemy[i].m_type == 2 )
m_enemy[i].m_level = 2;
*/
m_enemy[i].Reborn();
m_nEnemysLeft --;
if( ++which > 2 )
which = 0;
return TRUE;
}
return FALSE;
}
////////////////////////////////////////////////////
//BoreBonus +++++++++++++++++++++++++++++++++++++
// 奖品的诞生
///////////////////////////////////////////////////
BOOL CGame::BoreBonus(int x,int y)
{
int temp = rand() % 100;
int offsize=0;
if( temp < 50 )
{m_bonus.m_type = BONUS_FIRESPEED;offsize=4;}
else if( temp < 60 )
{m_bonus.m_type = BONUS_CLOCK;offsize=2;}
else if( temp < 70 )
{m_bonus.m_type = BONUS_SHOE;offsize=3;}
else if( temp < 80 )
{m_bonus.m_type = BONUS_LIFE;offsize=1;}
else if(temp<85)
{m_bonus.m_type =BONUS_FastTurtle;offsize=5;}
else {m_bonus.m_type = BONUS_SlowTurtle;offsize=6;}
if(offsize==0)
{
m_bonus.m_active=false;
return false;
}
if(offsize>4)
offsize=5;
m_bonus.BitBlt(&m_pBmpList[8+offsize]);
m_bonus.m_x = x;
m_bonus.m_y = y;
m_bonus.m_lastTime =
m_bonus.m_flickerTime = timeGetTime();
if(offsize>4)
m_bonus.Create(36,41,TRUE);
else
m_bonus.Create(BONUS_CX,BONUS_CY,TRUE);
return TRUE;
}
//////////////////////////////////////////////////////
//EatBonus -CPlayer ++++++++++++++++++++++++++++++++++
/////////////////////////////////////////////////////
void CGame::EatBonus( CPlayer& player )
{
// m_DirectSound.Play( EFFECT_PICK );
int i;
PlaySound("sound\\get.wav",NULL,SND_FILENAME|SND_ASYNC);
switch( m_bonus.m_type )
{
case BONUS_LIFE:
player.m_nLife = player.m_nLife+3;
break;
case BONUS_CLOCK:
m_bEnemyLocked = TRUE;
m_lockTime = timeGetTime();
break;
case BONUS_SHOE:
player.m_speed += 3;
break;
case BONUS_FIRESPEED:
for(i=0;i<2;i++)
player.m_bullet[i].m_speed+=6;
break;
case BONUS_FastTurtle:
player.m_speed += 6;
break;
case BONUS_SlowTurtle:
player.m_speed -= 6;
break;
}
m_bonus.m_active = FALSE;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//处理数据中心-输入,行做行为等
//////////////////////////////////////////////////////
//处理输入
void CGame::ProcessInput()
{
static DWORD lastTick = timeGetTime();
DWORD time = timeGetTime() - lastTick;
CBullet* bullet;
CExlpode* explode;
int x, y;
DIRECTION dir;
int surface;
int i, j, k;
// // process player's input
WORD input[2];
//+++++++++++++++++++++++++++++++
//玩家输入和活动处理
if(timeGetTime() - lastTick>100)//对它速度控制
{
m_Input.GetKey(input[0],input[1]);
for( k = 0; k < 2; k ++ )
{
if( !m_player[k].m_active )
continue;
x = m_player[k].m_x;
y = m_player[k].m_y;
dir = m_player[k].m_dir;
if( !m_player[k].m_bBoring && m_gameState != GS_OVER )
{
m_player[k].ProcessInput(input[k], time);
//Play Sound //if( m_player[k].ProcessInput(input[k], time) )
}
//捡宝
if(m_player[k].HitTest(m_bonus))
EatBonus(m_player[k]);
//-----------
//炸弹处理
for( j = 0; j < 2; j ++ )
{
explode=&m_player[k].m_explode[j];
if( !(explode->m_active) )
{
continue;
}
if(explode->Exploding())
{
for( i = 0; i < m_nMaxEnemys; i ++ )
{
if( !m_enemy[i].m_bBoring&&explode->HitTest(m_enemy[i]))
{ explode->m_active=FALSE;
m_enemy[i].m_active = FALSE;
BoreBonus(m_enemy[i].m_x,m_enemy[i].m_y);
m_nEnemys --;
m_player[k].m_nScore += 100;
}
}
}
}
//-------------
//子弹处理
//------------------------------------------------------
for( j = 0; j < 2; j ++ )
{
bullet = &m_player[k].m_bullet[j];
if( !bullet->m_active )
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -