陆续改了半个月,不断更改游戏目的,结合了老鼠走迷宫和贪吃蛇游戏,终于在昨天凌晨2点写好了我的炸弹超人游戏。基于Windows图形界面,用C语言编写。虽然并不复杂,但收获颇多。
文件1:winapp.cpp,指导老师所给框架及自己写的主函数:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
#include "winapp.h"
#include "myfile.h"
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Windows 应用程序设计--The Maze") ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("Windows 应用程序设计--The Maze"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
1600, //CW_USEDEFAULT, // initial x size
900, //CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
drawRectangle(38*WIDTH,3*WIDTH,39*WIDTH,8*WIDTH,WEAKWALL); //上下
drawRectangle(36*WIDTH,5*WIDTH,41*WIDTH,6*WIDTH,WEAKWALL); //左右
drawRectangle(38*WIDTH,5*WIDTH,39*WIDTH,6*WIDTH,BOOM);
printText(800,400,"炸弹人!!!");
printText(800,450,"come on baby!!!");
printText(1100,355," へ /|");
printText(1100,375," / \ ∠_/");
printText(1100,395," / │ / /");
printText(1100,415," │ _,< / /`ヽ");
printText(1100,435," │ ヽ / 〉");
printText(1100,455," \\ ` / /");
printText(1100,475," |● ● |〈 /");
printText(1100,495," () へ | \〈");
printText(1100,505," > _ ィ │ //");
printText(1100,525," / へ / <| \\");
printText(1100,545," ヽ_| (_/ │//");
printText(1100,565," | |/");
printText(1100,585," >―- ̄ ̄//―_");
if(yes==1)
{
printText(800,300,"~O(∩_∩)O~");
printText(800,350,"你赢了 !!!");
return 0;
}
//char s[100];
switch (message)
{
case WM_KEYDOWN: //键盘消息
{
//键盘消息处理内容
switch ((int)wParam)
{
case VK_UP: //向上 ↑
up();
break;
case VK_DOWN: // 向下 ↓
down();
break;
case VK_LEFT: //向左 ←
left();
break;
case VK_RIGHT: // 向右 →
right();
break;
case VK_RETURN: //回车
initgame();
break;
case VK_SPACE: //空格放炸弹
backspace();
break;
case VK_ESCAPE: //esc
boomboom();
break;
}
}
break;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
文件2:myfile.h,自己编写的程序:
#include<time.h>
#include<stdlib.h>
#define ROAD RGB(200,200,200)//浅灰
#define WEAKWALL RGB(255,174,200)//假墙
#define MOUSE RGB(255,100,168)//pink
#define BOOM RGB(0,0,0)//炸弹
#define WALL RGB(50,50,50)//深灰
#define FIRE RGB(225,0,0)//red
#define yeah RGB(255,225,128)//yellow
#define FOOD RGB(50,50,225)//BLUE
#define UP 40
#define DOWN 41
#define LEFT 42
#define RIGHT 43
#define X0 0
#define Y0 0
#define WIDTH 30
int xnum=21;
int ynum=21;
int boomx1,boomy1,boomx2,boomy2;
int a[30][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if (a[x][y]==1)
drawRectangle(x*WIDTH,y*WIDTH,(x+1)*WIDTH,(y+1)*WIDTH,WALL);
else if(a[x][y]==0)
drawRectangle(x*WIDTH,y*WIDTH,(x+1)*WIDTH,(y+1)*WIDTH,ROAD);
else
drawRectangle(x*WIDTH,y*WIDTH,(x+1)*WIDTH,(y+1)*WIDTH,WEAKWALL);
}
mousex=1,mousey=1;
drawRectangle(X0+mousex*WIDTH,Y0+mousey*WIDTH,X0+(mousex+1)*WIDTH,Y0+(mousey+1)*WIDTH,MOUSE);
drawRectangle(19*WIDTH,19*WIDTH,20*WIDTH,20*WIDTH,yeah);
}
void right()
{
if(a[mousex+1][mousey]==0)
{
if(flag==1)
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,BOOM);
else
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,ROAD);
mousex++;
drawRectangle(X0+mousex*WIDTH,Y0+mousey*WIDTH,X0+(mousex+1)*WIDTH,Y0+(mousey+1)*WIDTH,MOUSE);
}
flag=0;
if(mousex==19&&mousey==19)
yes=1;
}
void left()
{
if(a[mousex-1][mousey]==0)
{
if(flag==1)
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,BOOM);
else
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,ROAD);
mousex--;
drawRectangle(X0+mousex*WIDTH,Y0+mousey*WIDTH,X0+(mousex+1)*WIDTH,Y0+(mousey+1)*WIDTH,MOUSE);
}
flag=0;
if(mousex==19&&mousey==19)
yes=1;
}
void up()
{
if(a[mousex][mousey-1]==0)
{
if(flag==1)
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,BOOM);
else
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,ROAD);
mousey--;
drawRectangle(X0+mousex*WIDTH,Y0+mousey*WIDTH,X0+(mousex+1)*WIDTH,Y0+(mousey+1)*WIDTH,MOUSE);
}
flag=0;
if(mousex==19&&mousey==19)
yes=1;
}
void down()
{
if(a[mousex][mousey+1]==0)
{
if(flag==1)
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,BOOM);
else
drawRectangle(mousex*WIDTH,mousey*WIDTH,(mousex+1)*WIDTH,(mousey+1)*WIDTH,ROAD);
mousey++;
drawRectangle(X0+mousex*WIDTH,Y0+mousey*WIDTH,X0+(mousex+1)*WIDTH,Y0+(mousey+1)*WIDTH,MOUSE);
}
flag=0;
if(mousex==19&&mousey==19)
yes=1;
}
void backspace()
{
boomx1=mousex*WIDTH;
boomy1=mousey*WIDTH;
boomx2=(mousex+1)*WIDTH;
boomy2=(mousey+1)*WIDTH;
q=mousex;w=mousey;e=mousex+1;r=mousey+1;
drawRectangle(boomx1,boomy1,boomx2,boomy2,BOOM);
flag=1;
}
void boomboom()
{
int i,j;
drawRectangle(boomx1,boomy1,boomx2,boomy2,ROAD);
drawRectangle(boomx1,boomy1-2*WIDTH,boomx2,boomy1,ROAD); //上
//if(boomy1+WIDTH<=19)
drawRectangle(boomx1,boomy1+WIDTH,boomx2,boomy2+WIDTH,ROAD);//下
if(boomy1+2*WIDTH<=20)
drawRectangle(boomx1,boomy1+2*WIDTH,boomx2,boomy2+2*WIDTH,ROAD);//下
drawRectangle(boomx1-2*WIDTH,boomy1,boomx1,boomy2,ROAD);//左
//if(boomx1+WIDTH<=19) //右
drawRectangle(boomx1+WIDTH,boomy1,boomx2+WIDTH,boomy2,ROAD);
if(boomx1+2*WIDTH<=20) //右
drawRectangle(boomx1+2*WIDTH,boomy1,boomx2+2*WIDTH,boomy2,ROAD);
//a[(boomx1)/WIDTH][boomy1/WIDTH]=a[boomx1/WIDTH][(boomy1-2*WIDTH)/WIDTH]=a[boomx1/WIDTH][(boomy1-WIDTH)/WIDTH]=a[boomx1/WIDTH][(boomy1+WIDTH)/WIDTH]=a[boomx1/WIDTH][(boomy1+2*WIDTH)/WIDTH]=a[(boomx1-WIDTH)/WIDTH][boomy1/WIDTH]=a[(boomx1-2*WIDTH)/WIDTH][boomy1/WIDTH]=a[(boomx1+WIDTH)/WIDTH][boomy1/WIDTH]=a[(boomx1+2*WIDTH)/WIDTH][boomy1/WIDTH]=0;
//for(i=mousex-2;i<=mousex+3;i++)
//for(j=mousey-2;j<=mousey+3;j++)
/*if((mousex >= 2) && a[mousex-2][mousey]!=1)
a[mousex-2][mousey]=0;
else
drawRectangle((mousex-2)*WIDTH,(mousey)*WIDTH,(mousex-2+1)*WIDTH,(mousey+1)*WIDTH,WALL);
if((mousex >= 1) && a[mousex-1][mousey]!=1)
a[mousex-1][mousey]=0;
else
drawRectangle((mousex-1)*WIDTH,(mousey)*WIDTH,(mousex-1+1)*WIDTH,(mousey+1)*WIDTH,WALL);
if((mousex+1<=20) && a[mousex+1][mousey]!=1)
a[mousex+1][mousey]=0;
else
drawRectangle((mousex+1)*WIDTH,(mousey)*WIDTH,(mousex+1+1)*WIDTH,(mousey+1)*WIDTH,WALL);
if((mousex+2<=20) &&a[mousex+2][mousey]!=1)
a[mousex+2][mousey]=0;
else
drawRectangle((mousex+2)*WIDTH,(mousey)*WIDTH,(mousex+2+1)*WIDTH,(mousey+1)*WIDTH,WALL);
if((mousey >= 2) && a[mousex][mousey-2]!=1)
a[mousex][mousey-2]=0;
else
drawRectangle((mousex)*WIDTH,(mousey-2)*WIDTH,(mousex+1)*WIDTH,(mousey-2+1)*WIDTH,WALL);
if((mousey >= 1) && a[mousex][mousey-1]!=1)
a[mousex][mousey-1]=0;
else
drawRectangle((mousex)*WIDTH,(mousey-1)*WIDTH,(mousex+1)*WIDTH,(mousey-1+1)*WIDTH,WALL);
if((mousey+1<=20) &&a[mousex][mousey+1]!=1)
a[mousex][mousey+1]=0;
else
drawRectangle((mousex)*WIDTH,(mousey+1)*WIDTH,(mousex+1)*WIDTH,(mousey+1+1)*WIDTH,WALL);
if((mousey+2<=20) &&a[mousex][mousey+2]!=1)
a[mousex][mousey+2]=0;
else
drawRectangle((mousex)*WIDTH,(mousey+2)*WIDTH,(mousex+1)*WIDTH,(mousey+2+1)*WIDTH,WALL);*/
if((q >= 2) && a[q-2][w]!=1)
a[q-2][w]=0;
else
drawRectangle((q-2)*WIDTH,(w)*WIDTH,(q-2+1)*WIDTH,(w+1)*WIDTH,WALL);
if((q >= 1) && a[q-1][w]!=1)
a[q-1][w]=0;
else
drawRectangle((q-1)*WIDTH,(w)*WIDTH,(q-1+1)*WIDTH,(w+1)*WIDTH,WALL);
if(a[q+1][w]!=1)
a[q+1][w]=0;
else
drawRectangle((q+1)*WIDTH,(w)*WIDTH,(q+1+1)*WIDTH,(w+1)*WIDTH,WALL);
if(a[q+2][w]!=1)
a[q+2][w]=0;
else
drawRectangle((q+2)*WIDTH,(w)*WIDTH,(q+2+1)*WIDTH,(w+1)*WIDTH,WALL);
if((w >= 2) && a[q][w-2]!=1)
a[q][w-2]=0;
else
drawRectangle((q)*WIDTH,(w-2)*WIDTH,(q+1)*WIDTH,(w-2+1)*WIDTH,WALL);
if((w >= 1) && a[q][w-1]!=1)
a[q][w-1]=0;
else
drawRectangle((q)*WIDTH,(w-1)*WIDTH,(q+1)*WIDTH,(w-1+1)*WIDTH,WALL);
if(a[q][w+1]!=1)
a[q][w+1]=0;
else
drawRectangle((q)*WIDTH,(w+1)*WIDTH,(q+1)*WIDTH,(w+1+1)*WIDTH,WALL);
if(a[q][w+2]!=1)
a[q][w+2]=0;
else
drawRectangle((q)*WIDTH,(w+2)*WIDTH,(q+1)*WIDTH,(w+2+1)*WIDTH,WALL);
//外墙,死墙,自己,食物
for(int x=0;x<xnum;x++)
for(int y=0;y<ynum;y++)
{
if (a[x][y]==1&&x!=mousex&&y!=mousey)
drawRectangle(x*WIDTH,y*WIDTH,(x+1)*WIDTH,(y+1)*WIDTH,WALL);
else if(a[x][y]==0&&x!=mousex&&y!=mousey)
drawRectangle(x*WIDTH,y*WIDTH,(x+1)*WIDTH,(y+1)*WIDTH,ROAD);
}
drawRectangle(X0+19*WIDTH,Y0+19*WIDTH,X0+(19+1)*WIDTH,Y0+(19+1)*WIDTH,yeah);
flag = 0;
}
文件3:一些Windows图形界面下的函数,(老师给的框架):
#include <stdio.h>
HWND hwnd;
//设置定时器间隔
//speed为毫秒
void setSpeed(int movespeed)
{
SetTimer(hwnd,1, movespeed, NULL);
}
//暂停定时器
void stopTimer()
{
KillTimer(hwnd,1);
}
//指定位置输出字符串
void printText(int x,int y,char s[])
{
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,x,y,s,strlen(s));
ReleaseDC (hwnd, hdc) ;
}
//指定位置输出整型数
void printNumber(int x,int y, int z)
{
char str[100];
sprintf(str,"%d",z);
printText(x,y,str);
}
//指定位置输出实型数
void printNumber(int x,int y, double z)
{
char str[100];
sprintf(str,"%f",z);
printText(x,y,str);
}
//输出分数和等级
void printTitle(int score,int level)
{
char str[100];
sprintf(str,"当前分数 %5d",score);
printText(200,20,str);
sprintf(str," 等级 %5d",level);
printText(200,50,str);
}
//画矩形,顶点为(x1,y1),(x2,y2),颜色为color
void drawRectangle(int x1,int y1,int x2,int y2,int color)
{
HDC hdc;
hdc=GetDC(hwnd);
HBRUSH hBrush ;
RECT rect ;
POINT a[4];
a[0].x=x1,a[0].y=y1;
a[1].x=x2,a[1].y=y1;
a[2].x=x2,a[2].y=y2;
a[3].x=x1,a[3].y=y2;
a[4].x=x1,a[4].y=y1;
SetRect (&rect,x1,y1,x2,y2);
hBrush = CreateSolidBrush (color) ;
FillRect (hdc, &rect, hBrush) ; //填充矩形
SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
Polyline (hdc, a, 5) ; //画边框
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
void DrawImage(int x0, int y0, int x1, int y1, char *szImageFile)
{
int wid, hei;
int i;
wid = x1 - x0 + 1;
hei = y1 - y0 + 1;
HDC hdc = ::GetDC(hwnd);
//for (i = 0; i < 5; i++)
{
HANDLE h = ::LoadImage(0, szImageFile, IMAGE_BITMAP, wid, hei, LR_SHARED | LR_LOADFROMFILE);
// SIZE size;
// ::GetBitmapDimensionEx((HBITMAP)h, &size); // 问题:原图size尚未成功读取
HDC hdcMem = ::CreateCompatibleDC(hdc);
::SelectObject(hdcMem, h);
::BitBlt(hdc, x0, y0, wid, hei, hdcMem, 0, 0, SRCCOPY);
wid /= 2;
hei /= 2;
x0 += wid * 2 + 10;
}
}