www.pudn.com > Billy_The_1798759292004.zip > Test.cpp
#include "stdafx.h" #include#include #include #include #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 0) BitBlt(hBBufDC, 6*32, 16.5*32, 32, 32, Graphics.DCGraphics, 7*32, 3*32, SRCCOPY); if(Player.Oxygen>0) BitBlt(hBBufDC, 7*32, 16.5*32, 32, 32, Graphics.DCGraphics, 8*32, 3*32, SRCCOPY); /* THE HEAD */ BitBlt(hBBufDC, 20*32, 16*32, 64, 64, Graphics.DCGraphics, 5*32, 6*32, SRCCOPY); /* OXYGEN */ for(int r=0; r<4; r++) { if(Player.Oxygen>r) BitBlt(hBBufDC, 64+(r*8), (16*32)+21, 8, 23, Graphics.DCGraphics, (7*32)+8, 6*32, SRCCOPY); else BitBlt(hBBufDC, 64+(r*8), (16*32)+21, 8, 23, Graphics.DCGraphics, 7*32, 6*32, SRCCOPY); } /**********************/ } /*******************************************************************\ * MOVES THE PLAYER ONE STEP * \*******************************************************************/ void Move() { /*** CHECKS IF YOU DIED BY THE GAS ***/ if(GasON) { int Pos=Gas.Level[Player.x][Player.y]; if(Pos=='1' || Pos=='2' || Pos=='3' || Pos=='4') Dead(); } /** CHECKS IF YOU DIED BY THE FLAME **/ if(Level.Level[Player.x][Player.y]==FLAME) Dead(); /*************************************/ int TempY =Player.y; int TempX =Player.x; if(Boulder.Moves) { Boulder.Moves=MoveBoulder(Level.Level,Boulder); if(!Boulder.Moves) PlaySound("BoulderStop",SND_ASYNC); } else { /********** Up **********/ if(Player.Direction=="UP") TempY--; /********* DOWN *********/ if(Player.Direction=="DOWN") TempY++; /********* LEFT *********/ if(Player.Direction=="LEFT") TempX--; /********* Right ********/ if(Player.Direction=="RIGHT") TempX++; if(TempX>21) TempX=0; if(TempX<0) TempX=21; if(TempY>15) TempY=0; if(TempY<0) TempY=15; if(Level.Level[TempX][TempY]==SHIFTKEY) { Level.Level[TempX][TempY]=' '; Player.HasShiftKey=true; } if(Level.Level[TempX][TempY]==CLOGGER) { Level.Level[TempX][TempY]=' '; Player.HasClogger=true; } if(Level.Level[TempX][TempY]==BRIDGEPLATE) { Level.Level[TempX][TempY]=' '; Player.NumBridges++; } if(Level.Level[TempX][TempY]==SCUBBAGEAR) { Level.Level[TempX][TempY]=' '; Player.Oxygen=4; } if(Level.Level[TempX][TempY]==TOILETT) { if(TempX!=Player.x || TempY!=Player.y) { for(int i=0; i 0) { Level.Level[TempX][TempY]=BRIDGE; Player.NumBridges--; TempX =Player.x; TempY =Player.y; } } if(Level.Level[TempX][TempY]==BUTTON) { if(Player.HasShiftKey) { if(GasON) GasON =false; else GasON =true; PlaySound("Button.wav", SND_NOSTOP | SND_ASYNC); } } if(Level.Level[TempX][TempY]==CRATE) { MoveCrate(TempX, TempY, Player.Direction, Level.Level, CRATE); TempX =Player.x; TempY =Player.y; } if(Level.Level[TempX][TempY]==DYNAMITE) MoveCrate(TempX, TempY, Player.Direction, Level.Level, DYNAMITE); if(Level.Level[TempX][TempY]==BOULDER) { Boulder.Dir =Player.Direction; Boulder.x =TempX; Boulder.y =TempY; Boulder.Moves =MoveBoulder(Level.Level, Boulder); if(Boulder.Moves) PlaySound("Boulder.wav", SND_NOSTOP | SND_ASYNC); } if(Level.Level[TempX][TempY]==END) { PlaySound("End.wav", SND_ASYNC); KillTimer(hWnd,uTimer); uTimer =0; char Puzzel[50]="Congratulazions you have completed puzzel "; int len=strlen(Puzzel); for(int i=0; i 0) { TempInt--; Level.Level[TempX][TempY]=TempInt+48; Player.x =TempX; Player.y =TempY; PlaySound("Plate", SND_NOSTOP | SND_ASYNC); } } } if((TempX!=Player.x || TempY!=Player.y)) { if(Walkable(Level.Level[TempX][TempY]) && !Boulder.Moves) { // Prevents you from going into the gas if(!GasON || ((Gas.Level[TempX][TempY]>='5' && Gas.Level[TempX][TempY]<='8') || Gas.Level[TempX][TempY]==' ')) { Player.x =TempX; Player.y =TempY; } else { TempX =Player.x; TempY =Player.y; } } else if(Level.Level[TempX][TempY]==WATER && Player.Oxygen>0) { Player.x =TempX; Player.y =TempY; Player.Oxygen--; } } if(Level.Level[Player.x][Player.y]==WATER) Player.Doing=SWIMMING; else if(CheckIfPusching(Player.x, Player.y, Player.Direction, Level.Level)) Player.Doing=PUSCHING; else if(CheckIfPusching(TempX, TempY, Player.Pointing, Level.Level)) Player.Doing=PUSCHING; else Player.Doing=NORMAL; SendMessage(hWndMe, WM_PAINT, 0, NULL); } /*******************************************************************\ * WHEN YOU ARE DEAD * \*******************************************************************/ void Dead() { GasON =true; Player.HasClogger =false; Player.HasShiftKey =false; Player.NumBridges =0; Player.Oxygen =0; LoadLevel(Level.CurrentMap); } /*******************************************************************\ * LOADS MAP * \*******************************************************************/ void LoadLevel(char *Map) { GasON=true; Player.HasClogger =false; Player.HasShiftKey =false; Player.NumBridges =0; Player.Oxygen =0; Level.CurrentMap=Map; char MapPath[30]="Levels\\"; char GasPath[30]="Levels\\"; for(int u=0; u