共有回帖数 0 个
-
- -!,好像违背了我的诺言,算了,有代码还是要和大家分享.下面是游戏的代码,里面还有一些图片,
#define INITGUID
#pragma comment(lib, "ddraw.lib" )
#include windows.h
#include ddraw.h
#include cmath
#include cstdlib
#include ctime
#include io.h
#include stdio.h
HINSTANCE hInst;
HWND hWnd;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
typedef struct
{
BITMAPFILEHEADER stBmpHeader;
BITMAPINFOHEADER stBmpInfo;
PALETTEENTRY stPalette[256];
UCHAR* lpBuffer;
}BITMAPFILE,*BITMAPPTR;
typedef unsigned char UCHAR;
typedef unsigned long ULONG;
typedef unsigned short USHORT;
typedef LPDIRECTDRAWSURFACE7 LPDDSF7;
LPDIRECTDRAW7 lpDD = NULL;
LPDIRECTDRAWPALETTE lpDDPal = NULL;
LPDDSF7 lpDDPrimary= NULL;
LPDDSF7 lpDDBackSurface = NULL;
PALETTEENTRY palette[256] = {0};
DDSURFACEDESC2 stDdsd;
int nMempitch = 0;
bool bIsExit = 0;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 8;
const int nManBmpHeight = 32;
const int nManBmpWidth = 31;
const int nBgWidth = 640;
const int nBgHeight = 480;
const int nWallNum = 20;
const int nWallWidth = 94;
const int nWallHeight = 14;
const int nBottom = 480;
bool bIsCollision = true;
int nWallRiseSpeed = 1;
BITMAPFILE stManBmp, stBg;
LPDDSF7 lpBg;
struct stMan
{
LPDDSF7 lpFrame[5][4];
RECT stRect;
int xv;
int yv;
int nHP;
int nFrameX;
int nFrameY;
int nDrt;
ULONG nLayer;
}stMan;
struct stWall
{
LPDDSF7 lpFrame;
RECT stRect;
int vx;
int vy;
int nType;
int bExist;
bool bIsColiision;
}stWall[nWallNum];
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define RGB55(r,g,b) ((b & 31) + ((g & 31) 5) + ((r & 31) 10))
#define RGB565(r,g,b) ((b & 31) + ((g & 63) 5) + ((r & 31) 11))
#define RGB32(a,r,g,b) ((b) + ((g) 8) + ((r) 16) + ((a) 24))
#define INITSTRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
inline void MemCpy(void* lpDes, void* lpSrc, long nSize)
{
_asm
{
cld
mov esi, lpSrc
mov edi, lpDes
mov ecx, nSize
rep movsb
}
}
inline void MemZero(void *lpDes, long nSize)
{
_asm
{
mov edi, lpDes
mov ecx, nSize
xor eax, eax
repnz stosb
}
}
inline void MemSet(void *lpDes, char ch1, long nSize)
{
_asm
{
mov edi, lpDes
mov ecx, nSize
mov al, ch1
repnz stosb
}
}
LPDDSF7 CreateOffscreenSurface(int w, int h)
{
LPDDSF7 lpDDSF;
DDSURFACEDESC2 ddsd;
INITSTRUCT( ddsd );
ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
ddsd.dwHeight = h;
ddsd.dwWidth = w;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY ;
lpDD-CreateSurface( &ddsd, &lpDDSF, NULL);
DDCOLORKEY stColKey;
stColKey.dwColorSpaceLowValue = 0;
stColKey.dwColorSpaceHighValue = 0;
lpDDSF-SetColorKey(DDCKEY_SRCBLT, &stColKey);
return lpDDSF;
}
bool RectInRect(RECT* lpRect1, RECT* lpRect2)
{
POINT pt;
pt.x = lpRect1-left;
pt.y = lpRect1-top;
if( PtInRect( lpRect2, pt) )
return true;
pt.x = lpRect1-left;
pt.y = lpRect1-bottom;
if( PtInRect( lpRect2, pt))
return true;
pt.x = lpRect1-right;
pt.y = lpRect1-top;
if( PtInRect( lpRect2, pt))
return true;
pt.x = lpRect1-right;
pt.y = lpRect1-bottom;
if( PtInRect( lpRect2, pt))
return true;
pt.x = lpRect2-left;
pt.y = lpRect2-top;
if( PtInRect( lpRect1, pt))
return true;
pt.x = lpRect2-left;
pt.y = lpRect2-bottom;
if( PtInRect( lpRect1, pt))
return true;
pt.x = lpRect2-right;
pt.y = lpRect2-top;
if( PtInRect( lpRect1, pt))
return true;
pt.x = lpRect2-right;
pt.y = lpRect2-bottom;
if( PtInRect( lpRect1, pt))
return true;
return false;
}
void HoldBmpToSurface(LPDDSF7 lpdds, BITMAPPTR bitmap , int cx, int cy)
{
UCHAR *source_ptr,
*dest_ptr;
DDSURFACEDESC2 ddsd;
ddsd.dwSize = sizeof(ddsd);
lpdds-Lock(NULL,
&ddsd,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
cx = cx*(ddsd.dwWidth+1) + 1;
cy = cy*(ddsd.dwHeight+1) + 1;
source_ptr = bitmap-lpBuffer + cy*bitmap-stBmpInfo.biWidth+cx;
dest_ptr = (UCHAR *)ddsd.lpSurface;
for (int index_y=0; index_y (int)ddsd.dwHeight; index_y++)
{
memcpy(dest_ptr, source_ptr, ddsd.dwWidth);
dest_ptr += (ddsd.lPitch);
source_ptr += bitmap-stBmpInfo.biWidth;
}
lpdds-Unlock(NULL);
}
int DrawTextGDI(char *text, int x,int y,COLORREF color, LPDIRECTDRAWSURFACE7 lpdds)
{
// this function draws the sent text on the sent surface
// using color index as the color in the palette
HDC xdc; // the working dc
// get the dc from surface
if (FAILED(lpdds-GetDC(&xdc)))
return(0);
// set the colors for the text up
SetTextColor(xdc,color);
// set background mode to transparent so black isn't copied
SetBkMode(xdc, TRANSPARENT);
// draw the text a
TextOut(xdc,x,y,text,strlen(text));
// release the dc
lpdds-ReleaseDC(xdc);
// return success
return(1);
} // end Draw_Text_GDI
void FlipBitmap(UCHAR *lpImage, int nWidth, int nHeight)
{
UCHAR *pBuffer = (UCHAR*)new char[nWidth * nHeight];
memcpy(pBuffer, lpImage, nWidth * nHeight );
for( int i = 0; i nHeight; i++)
{
memcpy( &lpImage[(nHeight-1-i) * nWidth], &pBuffer[ i* nWidth], nWidth);
}
delete[] pBuffer;
}
void LoadBitmapFile(char* lpszFileName, BITMAPPTR pBmpPtr)
{
OFSTRUCT stOf;
HFILE hFile = OpenFile( lpszFileName, &stOf, OF_READ);
_lread( hFile, (void*)&pBmpPtr-stBmpHeader, sizeof( BITMAPFILEHEADER ));
_lread( hFile, (void*)&pBmpPtr-stBmpInfo, sizeof( BITMAPINFOHEADER ));
_lread( hFile, (void*)pBmpPtr-stPalette, sizeof( PALETTEENTRY )*256);
if( pBmpPtr-stBmpInfo.biBitCount == 8 )
{
for( int i = 0; i 256; i++)
{
int col = pBmpPtr-stPalette.peRed;
pBmpPtr-stPalette.peRed = pBmpPtr-stPalette.peBlue;
pBmpPtr-stPalette.peBlue = col;
pBmpPtr-stPalette.peFlags = PC_NOCOLLAPSE;
}
}
_lseek( hFile, -(int)pBmpPtr-stBmpInfo.biSizeImage, SEEK_END);
pBmpPtr-lpBuffer = (UCHAR*)new char[pBmpPtr-stBmpInfo.biSizeImage];
_lread( hFile, pBmpPtr-lpBuffer, pBmpPtr-stBmpInfo.biSizeImage );
char *p = (char*)pBmpPtr-lpBuffer;
FlipBitmap( pBmpPtr-lpBuffer, pBmpPtr-stBmpInfo.biWidth * pBmpPtr-stBmpInfo.biBitCount/8,
pBmpPtr-stBmpInfo.biHeight);
_lclose( hFile );
}
const int nWallMaxNum = 8;
int nGetWallTime = 50 ;
void GetWall()
{
static int t = 0;
t ++;
if( t == nGetWallTime )
{
t = 0;
}
int nExistWallNum = 0;
for( int i = 0; i nWallNum; i++)
{
if( stWall.bExist )
{
nExistWallNum ++;
}
}
int n = 0;
BITMAPFILE stWallBmp[5];
LoadBitmapFile("3.bmp", &stWallBmp[0] );
int h = 20;
if( nExistWallNum nWallMaxNum )
{
for( int i = 0; i nWallNum; i++)
{
if( !stWall.bExist )
{
if( t == nGetWallTime - 1 )
{
stWall.stRect.top = nBottom ;
stWall.stRect.left = rand()%100 + rand()%50 + rand()% 200;
stWall.stRect.right = stWall.stRect.left + nWallWidth ;
stWall.stRect.bottom = stWall.stRect.top + nWallHeight ;
stWall.bExist = true;
h += 50;
stWall.bIsColiision = false;
stWall.lpFrame= CreateOffscreenSurface( nWallWidth, nWallHeight );
HoldBmpToSurface( stWall.lpFrame, &stWallBmp[0], 0, 0 );
n ++;
t = 0;
}
return ;
}
}
}
}
void MoveWall()
{
for( int i = 0; i nWallNum; i++)
{
if( stWall.bExist )
{
stWall.stRect.top -= nWallRiseSpeed;
stWall.stRect.bottom = stWall.stRect.top + nWallHeight;
if( stWall.stRect.top 100 )
{
stWall.bExist = false;
}
}
}
}
void InitMan()
{
stMan.nDrt = VK_LEFT;
stMan.nFrameX = 0;
stMan.nFrameY = 0;
stMan.nHP = 10;
stMan.xv = 3;
stMan.yv = 3;
stMan.stRect.top = 100;
stMan.stRect.left = 100;
stMan.stRect.right = stMan.stRect.left + nManBmpHeight;
stMan.stRect.bottom = stMan.stRect.top + nManBmpWidth;
stMan.nLayer = 0;
}
void GameInit()
{
//////////////////////////////////// 初始化 //////////////////////////////////////
if( DirectDrawCreateEx( NULL, (void**)&lpDD, IID_IDirectDraw7, NULL) != DD_OK )
{
exit( 0 );
}
lpDD-SetCooperativeLevel( hWnd, DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT );
lpDD-SetDisplayMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 0, 0);
///////////////////////// 创建调试板 ///////////////////////////////
for( int i = 1; i 255; i ++)
{
palette.peRed = rand()% 255;
palette.peGreen = rand()% 255;
palette.peBlue = rand()% 255;
palette.peFlags = PC_NOCOLLAPSE;
}
palette[0].peRed = 0;
palette[0].peGreen = 0;
palette[0].peBlue = 0;
palette[0].peFlags = 0;
palette[255].peRed = 1;
palette[255].peGreen = 1;
palette[255].peBlue = 1;
palette[255].peFlags = 1;
lpDD-CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE, palette,
&lpDDPal, NULL );
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////// 创建 显示表面 //////////////////////////////////////////////
memset( &stDdsd, 0, sizeof( DDSURFACEDESC2 ) );
stDdsd.dwSize = sizeof( DDSURFACEDESC2 ) ;
stDdsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
stDdsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP ;
stDdsd.dwBackBufferCount = 1;
lpDD-CreateSurface( &stDdsd, &lpDDPrimary, NULL );
stDdsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER ;
lpDDPrimary-GetAttachedSurface( &stDdsd.ddsCaps, &lpDDBackSurface);
lpDDPrimary-SetPalette( lpDDPal );
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// 上锁显示表面的内存 //////////////////////////////////////////
DDSURFACEDESC2 stDdsd;
memset( &stDdsd, 0, sizeof( DDSURFACEDESC2 ) );
stDdsd.dwSize = sizeof( DDSURFACEDESC2 );
lpDDPrimary-Lock( NULL, &stDdsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL );
nMempitch = stDdsd.lPitch;
lpDDPrimary-Unlock( NULL );
/////////////////////////////////////////////////////////////////////////////////////
LoadBitmapFile( "2.bmp", &stBg );
LoadBitmapFile( "1.bmp", &stManBmp );
lpDDPal-SetEntries( 0,0, 256, stManBmp.stPalette );
for( int x = 0; x 4; x++)
{
stMan.lpFrame[0][x] = CreateOffscreenSurface( nManBmpWidth, nManBmpHeight );
HoldBmpToSurface( stMan.lpFrame[0][x], &stManBmp, x, 0 );
}
for( x = 0; x 4; x++)
{
stMan.lpFrame[1][x] = CreateOffscreenSurface( nManBmpWidth, nManBmpHeight );
HoldBmpToSurface( stMan.lpFrame[1][x], &stManBmp, x, 1 );
}
for( x = 0; x 4; x++)
{
stMan.lpFrame[2][x] = CreateOffscreenSurface( nManBmpWidth, nManBmpHeight);
HoldBmpToSurface( stMan.lpFrame[2][x], &stManBmp, x, 2 );
}
for( x = 0; x 4; x++)
{
stMan.lpFrame[3][x] = CreateOffscreenSurface( nManBmpWidth, nManBmpHeight - 1);
HoldBmpToSurface( stMan.lpFrame[3][x], &stManBmp, x, 3 );
}
for( x = 0; x 4; x++)
{
stMan.lpFrame[4][x] = CreateOffscreenSurface( nManBmpWidth, nManBmpHeight );
HoldBmpToSurface( stMan.lpFrame[4][x], &stManBmp, x, 4 );
}
InitMan();
lpBg = CreateOffscreenSurface( nBgWidth, nBgHeight );
HoldBmpToSurface( lpBg, &stBg, 0, 0 );
/*
BITMAPFILE stWallBmp[5];
LoadBitmapFile("3.bmp", &stWallBmp[0] );
stWall[0].stRect.top = 300;
stWall[0].stRect.left = 10;
stWall[0].stRect.right = stWall[0].stRect.left+ nWallWidth ;
stWall[0].stRect.bottom = stWall[0].stRect.top + nWallHeight ;
stWall[0].bExist = true;
stWall[0].lpFrame= CreateOffscreenSurface( nWallWidth, nWallHeight );
HoldBmpToSurface( stWall[0].lpFrame, &stWallBmp[0], 0, 0 );
stWall[1].stRect.top = 200;
stWall[1].stRect.left = 30;
stWall[1].stRect.right = stWall[1].stRect.left+ nWallWidth ;
stWall[1].stRect.bottom = stWall[1].stRect.top + nWallHeight ;
stWall[1].bExist = true;
stWall[1].lpFrame= CreateOffscreenSurface( nWallWidth, nWallHeight );
HoldBmpToSurface( stWall[1].lpFrame, &stWallBmp[0], 0, 0 );*/
srand(time(0));
}
inline void DrawPoint(int x, int y, int ucColor, UCHAR *lpusMem)
{
lpusMem[x + y*nMempitch] = ucColor;
}
void GameMain()
{
static int d = 0;
MoveWall();
GetWall();
d ++;
if( d 300 )
{
d = 0;
stMan.nLayer ++;
nWallRiseSpeed =2; //stMan.nLayer / 5 + 1;
}
if( bIsExit )
return;
if( KEYDOWN(VK_ESCAPE) )
{
bIsExit = 1;
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
//////////////////// 渲染背景 ///////////////////////////////
lpDDBackSurface-Blt( NULL, lpBg, NULL, NULL, NULL );
//////////////////////////////////////////////////////////
////////////////// 渲染主角 ///////////////////////////////
if( stMan.nFrameX 2)
{
stMan.nFrameX = 0;
}
bIsCollision = false;
RECT r;
for(int i = 0; i nWallNum; i++)
{
if( stWall.bExist )
{
r = stMan.stRect;
r.top = r.bottom + nWallHeight - 13;
r.bottom = r.top + 1;
if( RectInRect( &stWall.stRect, &r ) )
{
stMan.stRect.top -= nWallRiseSpeed;
stMan.stRect.bottom -= nWallRiseSpeed ;
bIsCollision = true;
if( !stWall.bIsColiision)
{
PlaySound (TEXT ("DowmSound.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
stMan.nDrt = VK_LEFT;
}
stWall.bIsColiision = true;
break;
}
}
}
if (stMan.stRect.bottom nBottom && !bIsCollision )
{
stMan.stRect.top += stMan.yv;
stMan.stRect.bottom = stMan.stRect.top + nManBmpHeight;
stMan.nFrameX ++;
stMan.nFrameY = 3;
if( stMan.stRect.bottom 470 )
{
PlaySound (TEXT ("DieSound.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
InitMan();
Sleep( 1000 );
}
}
lpDDBackSurface-Blt( &stMan.stRect, stMan.lpFrame[stMan.nFrameY][stMan.nFrameX], NULL, DDBLT_KEYSRC , NULL );
////////////////// 渲染主角 ///////////////////////////////
////////////////// 渲染墙壁 //////////////////////////////
for(i = 0; i nWallNum; i++)
{
if( stWall.bExist )
lpDDBackSurface-Blt( &stWall.stRect, stWall.lpFrame, NULL, NULL , NULL );
}
char szText[100];
sprintf( szText, "第%d层", stMan.nLayer );
DrawTextGDI( szText, 400, 80, 180, lpDDBackSurface);
DrawTextGDI( "是男人就下100层 CIW_BLUE制作 QQ:281011131", 100, 50, 200, lpDDBackSurface);
///////////////////////////////////////////////////////////
////////////////// 按下了按键 ////////////////////////////
if( KEYDOWN( VK_LEFT ) )
{
stMan.nDrt = VK_LEFT;
stMan.nFrameY = 0;
stMan.nFrameX ++;
RECT r1 = stMan.stRect;
r1.left -= stMan.xv;
stMan.stRect.left -= stMan.xv;
stMan.stRect.right = stMan.stRect.left + nManBmpWidth;
}
else if( KEYDOWN( VK_RIGHT ))
{
stMan.nDrt = VK_RIGHT;
stMan.nFrameY = 1;
stMan.nFrameX ++;
RECT r1 = stMan.stRect;
r1.left += stMan.xv;
stMan.stRect.left += stMan.xv;
stMan.stRect.right = stMan.stRect.left + nManBmpWidth;
}
else if( KEYDOWN( VK_LEFT ))
{
}
else if( KEYDOWN( VK_LEFT ))
{
}
///////////////////////////////////////////////////////////
Sleep( 33 );
while( FAILED(lpDDPrimary-Flip( NULL, DDFLIP_WAIT )));
}
void GameExit()
{
if( lpDDPal )
lpDDPal-Release();
if( lpDDBackSurface )
lpDDBackSurface-Release();
if( lpDDPrimary )
lpDDPrimary-Release();
if( lpDD )
lpDD-Release();
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
GameInit();
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GameMain();
}
GameExit();
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "CIW";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hWnd = CreateWindowEx(NULL,
"CIW",
"My First DX Window",
WS_POPUP | WS_VISIBLE,
0,0,
400,300,
NULL,
NULL,
hInst,
NULL);
if (!hWnd)
{
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
break;
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
楼主 2016-01-15 16:58 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知