曾经用 HGE 做的一个五子棋游戏-。-今天偶然翻了出来, 给大家分享一下
//main.cpp
#include <hge.h>
#include <hgeSprite.h>
#include "menu.h"
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
init();
doit();
delit();
return 0;
}
//menu.h
HEFFECT papa, music;//下棋声音
HTEXTURE bg0, bg1, bg2, bg3, bg4;//背景更换
HTEXTURE bgbg, white, black, whitewin, blackwin;
HGE *hge = 0;
hgeSprite *s_bgbg, *s_bg, *s_white, *s_black, *s_whitewin, *s_blackwin;
bool mouse_lbtn = 0;//记录鼠标左键点击
bool isWhiteBlack = 0;//判断当前下子方
float wfx[205], wfy[205];//白子坐标
float bfx[205], bfy[205];//黑子坐标
int cntw = 0, cntb = 0;//白子数与黑子数
int qi[20][20];//棋盘
int over = 0;//是否结束
int bgrand;//背景随机数
int dir[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
void boom()//点击声音
{
hge->Effect_Play(papa);
}
int walk(int x, int y, int dx, int dy)
{
int s = 0;
int xx = x;
int yy = y;
while (1)
{
xx += dx;
yy += dy;
if (xx < 0 || yy < 0 || xx > 18 || yy > 18 || qi[xx][yy] != qi[x][y]) return s;
s++;
}
}
void judge(int x, int y)//判断是否五星连珠
{
for (int i = 0; i < 8; i += 2)
{
if (walk(x, y, dir[i][0], dir[i][1]) + (walk(x, y, dir[i + 1][0], dir[i + 1][1])) >= 4)
{
over = qi[x][y];
return;
}
}
}
void leftButtonLastState()
{
bool mouse_lbtn0 = hge->Input_GetKeyState(HGEK_LBUTTON);
if (mouse_lbtn == 1 && mouse_lbtn0 == 0)
{
if (over)
{
bgrand = rand() % 5;
switch (bgrand)
{
case 0: s_bg = new hgeSprite(bg0, 0, 0, 1024, 768);break;
case 1: s_bg = new hgeSprite(bg1, 0, 0, 1024, 768);break;
case 2: s_bg = new hgeSprite(bg2, 0, 0, 1024, 768);break;
case 3: s_bg = new hgeSprite(bg3, 0, 0, 1024, 768);break;
case 4: s_bg = new hgeSprite(bg4, 0, 0, 1024, 768);break;
}
memset(qi, 0, sizeof(qi));
cntw = 0;
cntb = 0;
over = 0;
isWhiteBlack = false;
mouse_lbtn = 0;
return;
}
float x1, y1;
hge->Input_GetMousePos(&x1, &y1);
if (x1 < 100 && y1 < 100)
{
if (isWhiteBlack == false)
{
if (cntb == 0) return;
cntb--;
int xx = (bfx[cntb] + 10 - 450) / 30;
int yy = (bfy[cntb] + 10 - 114) / 30;
qi[xx][yy] = 0;
isWhiteBlack = true;
}
else
{
if (cntw == 0) return;
cntw--;
int xx = (wfx[cntw] + 10 - 450) / 30;
int yy = (wfy[cntw] + 10 - 114) / 30;
qi[xx][yy] = 0;
isWhiteBlack = false;
}
}
if (isWhiteBlack == false)
{
if (x1 > 445 && x1 < 995 && y1 > 109 && y1 < 659)
{
x1 = (int)(x1 - 435) / 30 * 30 + 450;
y1 = (int)(y1 - 99) / 30 * 30 + 114;
int xx = (x1 - 450) / 30;
int yy = (y1 - 114) / 30;
if (qi[xx][yy] == 0)
{
wfx[cntw] = x1 - 10;
wfy[cntw] = y1 - 10;
qi[xx][yy] = 1;
cntw++;
isWhiteBlack = true;
boom();
judge(xx, yy);
}
}
}
else
{
if (x1 > 445 && x1 < 995 && y1 > 109 && y1 < 659)
{
x1 = (int)(x1 - 435) / 30 * 30 + 450;
y1 = (int)(y1 - 99) / 30 * 30 + 114;
int xx = (x1 - 450) / 30;
int yy = (y1 - 114) / 30;
if (qi[xx][yy] == 0)
{
bfx[cntb] = x1 - 10;
bfy[cntb] = y1 - 10;
qi[xx][yy] = 2;
cntb++;
isWhiteBlack = false;
boom();
judge(xx, yy);
}
}
}
mouse_lbtn = 0;
}
}
bool RenderFunc()
{
hge->Gfx_BeginScene();
s_bg->Render(0, 0);
//s_bgbg->Render(416, 80);
for (int i = 0; i < 19; i++)
{
hge->Gfx_RenderLine(450.0, 114.0 + i * 30, 990.0, 114.0 + i * 30, 0xff000000, 0.5);
}
for (int i = 0; i < 19; i++)
{
hge->Gfx_RenderLine(450.0 + i * 30, 114.0, 450.0 + i * 30, 654.0, 0xff000000, 0.5);
}
for (int i = 0; i < cntw; i++)
{
s_white->Render(wfx[i], wfy[i]);
}
for (int i = 0; i < cntb; i++)
{
s_black->Render(bfx[i], bfy[i]);
}
if (over == 1)
s_whitewin->Render(200, 200);
if (over == 2)
s_blackwin->Render(200, 200);
hge->Gfx_EndScene();
return false;
}
bool FrameFunc()
{
if (hge->Input_GetKeyState(HGEK_LBUTTON)) mouse_lbtn = 1;
if (mouse_lbtn == 1) leftButtonLastState();
if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
if (hge->Input_GetKeyState(HGEK_A)) hge->Effect_Play(music);
return false;
}
void init()
{
hge=hgeCreate(HGE_VERSION);
hge->System_SetState(HGE_SHOWSPLASH, false);
hge->System_SetState(HGE_SCREENWIDTH, 1024);
hge->System_SetState(HGE_SCREENHEIGHT, 768);
hge->System_SetState(HGE_SCREENBPP, 32);
hge->System_SetState(HGE_LOGFILE, "Gobang.log");
hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
hge->System_SetState(HGE_TITLE, "Gobang");
hge->System_SetState(HGE_WINDOWED, true);
hge->System_SetState(HGE_USESOUND, true);
hge->System_SetState(HGE_HIDEMOUSE, false);
}
void doit()
{
if(hge->System_Initiate())
{
memset(qi, 0, sizeof(qi));
// Load sounds and textures
papa = hge->Effect_Load("menu.wav");
music = hge->Effect_Load("music.mp3");
bgbg = hge->Texture_Load("bgbg.png");
bg0 = hge->Texture_Load("bg0.jpg");
bg1 = hge->Texture_Load("bg1.jpg");
bg2 = hge->Texture_Load("bg2.jpg");
bg3 = hge->Texture_Load("bg3.jpg");
bg4 = hge->Texture_Load("bg4.jpg");
white = hge->Texture_Load("white.png");
black = hge->Texture_Load("black.png");
whitewin = hge->Texture_Load("whitewin.png");
blackwin = hge->Texture_Load("blackwin.png");
if (papa && bgbg && bg0 && bg1 && bg2 && bg3 && bg4 && white && black && whitewin && blackwin)
{
bgrand = rand() % 5;
switch (bgrand)
{
case 0: s_bg = new hgeSprite(bg0, 0, 0, 1024, 768);
case 1: s_bg = new hgeSprite(bg1, 0, 0, 1024, 768);
case 2: s_bg = new hgeSprite(bg2, 0, 0, 1024, 768);
case 3: s_bg = new hgeSprite(bg3, 0, 0, 1024, 768);
case 4: s_bg = new hgeSprite(bg4, 0, 0, 1024, 768);
}
s_bgbg = new hgeSprite(bgbg, 0, 0, 608, 608);
s_white = new hgeSprite(white, 0, 0, 24, 24);
s_black = new hgeSprite(black, 0, 0, 24, 24);
s_whitewin = new hgeSprite(whitewin, 0, 0, 250, 200);
s_blackwin = new hgeSprite(blackwin, 0, 0, 250, 200);
hge->System_Start();
}
else
{
MessageBoxA(NULL, "Can't load some sound(s) or texture(s)", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
}
}
}
void delit()
{
hge->Texture_Free(bg0);
hge->Texture_Free(bg1);
hge->Texture_Free(bg2);
hge->Texture_Free(bg3);
hge->Texture_Free(bg4);
hge->Texture_Free(bgbg);
hge->Texture_Free(black);
hge->Texture_Free(white);
hge->Texture_Free(blackwin);
hge->Texture_Free(whitewin);
hge->Effect_Free(papa);
delete s_bg;
delete s_bgbg;
delete s_black;
delete s_white;
delete s_blackwin;
delete s_whitewin;
hge->System_Shutdown();
hge->Release();
}
当初代码写的比较烂,拿出来丢丢脸。
![可怜](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)