📄 test.cpp
字号:
#include "stdafx.h"
#include <stuff.h>
#include <windows.h>
#include <ctime>
#include <fstream>
#include "Editor\\inputbox.h"
#include "Plumber.h"
void Move();
void Draw(HDC hBBufDC);
void Dead();
char *GetDirection(unsigned int Param);
void PlaySound(char *FileName, unsigned int Mode);
void LoadLevel(char *Map);
LRESULT CALLBACK WndProc(HWND Wnd,UINT uMsg, WPARAM wParam, LPARAM lParam);
// Windows
HWND hWndMe;
HWND hWnd;
struct BMP
{
/***** MAP *****/
HBITMAP BMPGraphics;
HDC DCGraphics;
/***************/
HBITMAP BMPPlayer;
HDC DCPlayer;
};
struct PlayerInf
{
char* Direction;
char* Pointing;
int x;
int y;
int Doing;
bool HasClogger;
bool HasShiftKey;
int NumBridges;
int Oxygen;
};
struct Teleport
{
int x[5];
int y[5];
bool Cloged[5];
};
int NumTeleports=0;
bool GasON;
Ball Boulder;
Teleport Toilett;
PlayerInf Player;
Map Level;
Map Gas;
BMP Graphics;
#define LEFT 1
#define RIGHT 0
#define NORMAL 0
#define PUSCHING 1
#define SWIMMING 2
// Time between moves (Milisec)
int Speed =100;
// Timer value
UINT uTimer =0;
#define TIMER 801
HINSTANCE hInstance;
#define GRIDSIZE 32
#define WINDOWWIDTH 704+6 //22
#define WINDOWHEIGHT 576+32 //16
struct FramePerSec
{
time_t Start;
time_t End;
int Num;
int Fps;
bool ON;
};
FramePerSec FPS;
char *Direction ="STOP";
/*******************************************************************\
* RETURNS DIRECTION CHOSEN *
\*******************************************************************/
char *GetDirection(unsigned int Param)
{
if(Param==VK_DOWN)
{
Direction = "DOWN";
}
else if(Param==VK_LEFT)
{
Direction = "LEFT";
}
else if(Param==VK_UP)
{
Direction = "UP";
}
else if(Param==VK_RIGHT)
{
Direction = "RIGHT";
}
else
Direction = "STOP";
return Direction;
}
void Explode(int ExplosionX, int ExplosionY)
{
bool Boom=false;
if(GasON && Gas.Level[ExplosionX][ExplosionY]>='1' && Gas.Level[ExplosionX][ExplosionY]<='4')
{
for(int x=0; x<3; x++)
{
for(int y=0; y<3; y++)
{
if(Level.Level[ExplosionX+x-1][ExplosionY+y-1]!=WALL)
{
Boom=true;
Level.Level[ExplosionX+x-1][ExplosionY+y-1]=FLAME;
}
}
}
}
if(Boom)
PlaySound("Dynamite.wav",SND_ASYNC);
}
/*******************************************************************\
* THE TIMER HAS TICKED *
\*******************************************************************/
void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT uId, DWORD dSysTime)
{
Move();
}
/*******************************************************************\
* A SIMPLER BitBlt *
\*******************************************************************/
void Paint(int x, int y,HDC hBBufDC, int xPOS, int yPOS)
{
BitBlt(hBBufDC, x*GRIDSIZE, y*GRIDSIZE, GRIDSIZE, GRIDSIZE, Graphics.DCGraphics, GRIDSIZE*xPOS, GRIDSIZE*yPOS, SRCCOPY);
}
/*******************************************************************\
* *
* *
* *
* PROGRAM START *
* *
* *
* *
\*******************************************************************/
int WINAPI WinMain(HINSTANCE Hin, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
FPS.ON=true;
if(FPS.ON)
time(&FPS.Start);
Level.MapSizeX =22;
Level.MapSizeY =16;
Gas.MapSizeX =22;
Gas.MapSizeY =16;
GasON =true;
Toilett.Cloged[0] =true;
Toilett.Cloged[1] =true;
if(strlen(szCmdLine) == 0)
{
char Map[30];
if(InputBox(0,"Type in the name of the level with out \".txt\" && Dir","Open Level","Level Name",Map) != 1)
PostQuitMessage(0);
LoadLevel(Map);
}
else
LoadLevel(szCmdLine);
Player.Direction ="DOWN";
Player.Pointing ="DOWN";
Player.Doing =NORMAL;
Player.Oxygen =0;
hInstance=Hin;
srand(time(0));
char* WindowClass = "Adventure"; // Name of class
char* WindowTitle = "Billy The Plumber"; // Text in window title bar
int WindowWidth = WINDOWWIDTH; // Width of window
int WindowHeight = WINDOWHEIGHT; // Height of window
MSG msg;
// Create window
hWndMe = SetUpWindow(WindowClass, WindowTitle, WindowWidth, WindowHeight, hInstance, WndProc);
// Show window
ShowWindow(hWndMe,nCmdShow);
// Update window
UpdateWindow(hWndMe);
// Receive messages
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*******************************************************************\
* THE WINDOW HAS REVEIVED A MESSAGE *
\*******************************************************************/
LRESULT CALLBACK WndProc(HWND Wnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(FPS.ON)
{
FPS.Num++;
time (&FPS.End);
if(difftime(FPS.End,FPS.Start)>=1)
{
time(&FPS.Start);
FPS.Fps=FPS.Num;
FPS.Num=0;
}
}
hWnd = Wnd;
switch (uMsg)
{
case WM_CREATE:
{
HDC hDC = GetDC(hWnd);
/********************** PLAYER ********************/
Graphics.BMPPlayer = LoadBMP("graphics\\Player.bmp" , 0, 0);
if(!Graphics.BMPPlayer)
{
MessageBox(hWnd,"graphics\\Player.bmp","Error", MB_ICONERROR);
PostQuitMessage(0);
}
Graphics.DCPlayer = CreateCompatibleDC(hDC);
SelectObject(Graphics.DCPlayer, Graphics.BMPPlayer);
/********************* GRAPHICS *******************/
Graphics.BMPGraphics = LoadBMP("graphics\\Graphics.bmp" , 0, 0);
if(!Graphics.BMPGraphics)
{
MessageBox(hWnd,"graphics\\Graphics.bmp","Error", MB_ICONERROR);
PostQuitMessage(0);
}
Graphics.DCGraphics = CreateCompatibleDC(hDC);
SelectObject(Graphics.DCGraphics, Graphics.BMPGraphics);
/**************************************************/
// Release and delete the temporary device context
ReleaseDC(hWnd, hDC);
DeleteObject(hDC);
break;
}
case WM_DESTROY:
{
DeleteObject(Graphics.BMPGraphics);
ReleaseDC(hWnd, Graphics.DCPlayer);
DeleteObject(Graphics.BMPGraphics);
ReleaseDC(hWnd, Graphics.DCPlayer);
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
// Set up window to be painted
PAINTSTRUCT ps;
HDC hDC = GetDC(hWnd);
BeginPaint(hWnd, &ps);
/*** Set up the background of the back-buffer device context ***/
// Window size
RECT rWnd;
// Gets the window size
GetClientRect(hWnd, &rWnd);
// Make the back buffer compatible with the window
HDC hBBufDC = CreateCompatibleDC(hDC);
// Make the bitmap compatible with the window
HBITMAP hBBuf = CreateCompatibleBitmap(hDC, rWnd.right, rWnd.bottom);
SelectObject(hBBufDC, hBBuf); // Place the bitmap in the back-buffer // Select a black pen
// Set text background to transparent
SetBkMode(hBBufDC, TRANSPARENT);
// Set text color to blue
SetTextColor(hBBufDC, RGB(180,0,0));
// Draws the board
Draw(hBBufDC);
int len=strlen(Level.CurrentMap);
int g=0;
if(len==1)
g=12;
if(len==2)
g=8;
if(len==3)
g=4;
TextOut(hBBufDC, 288+g, 535, Level.CurrentMap, strlen(Level.CurrentMap));
if(FPS.ON)
{
//TextOut(hBBufDC, 10, 10, "FPS:", strlen("FPS:"));
TextOut(hBBufDC, 50, 10, IntToChar(FPS.Fps), strlen(IntToChar(FPS.Fps)));
}
// Copy the backbuffer to the window
BitBlt(hDC, 0, 0, rWnd.right, rWnd.bottom, hBBufDC, 0, 0, SRCCOPY);
// Finish painting
EndPaint(hWnd, &ps);
// Release device contexts and delete used objects
ReleaseDC(hWnd, hDC);
ReleaseDC(hWnd, hBBufDC);
DeleteObject(hDC);
DeleteObject(hBBufDC);
DeleteObject(hBBuf);
break;
}
case WM_KEYDOWN:
{
if(wParam == VK_TAB)
LoadLevel(IntToChar(CharToInt(Level.CurrentMap)+1));
if(wParam == VK_ESCAPE)
Dead();
Player.Direction = GetDirection(wParam);
if(Player.Direction!="STOP")
Player.Pointing=Player.Direction;
Move();
uTimer = SetTimer(hWnd, TIMER, Speed, &TimerProc);
Player.Direction="STOP";
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
/*******************************************************************\
* DRAWS GAS *
\*******************************************************************/
void DrawGas(HDC hBBufDC)
{
for(int x=0; x<Gas.MapSizeX; x++)
{
for(int y=0; y<Gas.MapSizeY; y++)
{
// LEFT TO RIGHT
if(Gas.Level[x][y] == '1' || Gas.Level[x][y] == '5')
{
if(Level.Level[x][y] == WALL)
Paint(x,y,hBBufDC, 0, 5);
else
{
for(int i=0; i<Gas.MapSizeX; i++)
{
if(CheckIfPushable(x-i, y, Level.Level))
{
if(i!=0)
Gas.Level[x][y]='5';
break;
}
else if(Level.Level[x-1][y]==DYNAMITE)
Explode(x-i,y);
else if(Level.Level[x-i][y] == WALL)
{
if(Gas.Level[x+1][y]=='1' || Gas.Level[x+1][y]=='5')
Paint(x,y,hBBufDC, 1, 5);
else
Paint(x,y,hBBufDC, 2, 5);
Gas.Level[x][y]='1';
break;
}
}
}
}
// UP TO DOWN
if(Gas.Level[x][y] == '2' || Gas.Level[x][y] == '6')
{
if(Level.Level[x][y] == WALL)
Paint(x,y,hBBufDC, 2, 0);
else
{
for(int i=0; i<Gas.MapSizeY; i++)
{
if(CheckIfPushable(x, y-i, Level.Level))
{
if(i!=0)
Gas.Level[x][y]='6';
break;
}
else if(Level.Level[x][y-i]==DYNAMITE)
Explode(x,y-i);
else if(Level.Level[x][y-i] == WALL)
{
if(Gas.Level[x][y+1]=='2' || Gas.Level[x][y+1]=='6')
Paint(x,y,hBBufDC, 2, 1);
else
Paint(x,y,hBBufDC, 2, 2);
Gas.Level[x][y]='2';
break;
}
}
}
}
// RIGHT TO LEFT
if(Gas.Level[x][y] == '3' || Gas.Level[x][y] == '7')
{
if(Level.Level[x][y] == WALL)
Paint(x,y,hBBufDC, 2, 3);
else
{
for(int i=0; i<Gas.MapSizeX; i++)
{
if(CheckIfPushable(x+i, y, Level.Level))
{
if(i!=0)
Gas.Level[x][y]='7';
break;
}
else if(Level.Level[x+i][y]==DYNAMITE)
Explode(x+i,y);
else if(Level.Level[x+i][y] == WALL)
{
if(Gas.Level[x-1][y]=='3' || Gas.Level[x-1][y]=='7')
Paint(x,y,hBBufDC, 1, 3);
else
Paint(x,y,hBBufDC, 0, 3);
Gas.Level[x][y]='3';
break;
}
}
}
}
// DOWN TO UP
if(Gas.Level[x][y] == '4' || Gas.Level[x][y] == '8')
{
if(Level.Level[x][y] == WALL)
Paint(x,y,hBBufDC, 0, 2);
else
{
for(int i=0; i<Gas.MapSizeY; i++)
{
if(CheckIfPushable(x, y+i, Level.Level))
{
if(i!=0)
Gas.Level[x][y]='8';
break;
}
else if(Level.Level[x][y+i]==DYNAMITE)
Explode(x,y+i);
else if(Level.Level[x][y+i] == WALL)
{
if(Gas.Level[x][y-1]=='4' || Gas.Level[x][y-1]=='8')
Paint(x,y,hBBufDC, 0, 1);
else
Paint(x,y,hBBufDC, 0, 0);
Gas.Level[x][y]='4';
break;
}
}
}
}
}
}
}
/*******************************************************************\
* DRAWS THE SNAKE *
\*******************************************************************/
void Draw(HDC hBBufDC)
{
int xPos=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -