#include<windows.h>
#include "resource.h"
//自己做的一个很简单的小游戏,用win 32 sdk实现的,游戏中或许还有些bug,先传到网上去,
//就当是抛砖引玉吧,希望能认识更多有这方面兴趣的朋友,交流、学习,共同提高! QQ 568571516
//希望有这方面兴趣的人不吝赐教,本人一直想做一个很好的,属于我们自己的RPG游戏,希望有人能愿意一起做好它!
#define n_time1 100
#define n_time2 200
HWND hWND; //接受应用程序的句柄
HINSTANCE hThistance;
RECT rt; //接受应用程序的窗口大小
POINT ball; //接受飞船的坐标
int score=0; //游戏中所得成绩
int x=-1; //关于子弹的附加变量
struct Mons
{
BOOL m_exit;
POINT pt;
}monst[10]; //关于射击目标体的结构体
struct position
{
BOOL jd;
POINT pt;
}m_ball[20]; //子弹的结构体
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
void SetBackgrand(); //绘制游戏背景
void SetBoat(); //绘制非床
void SetBoot(int i); //绘制子弹
void SetMonster(int i); //绘制目标体(敌人)
BOOL IsHead(int x,int y); //判断飞船是否越界
void Finish(); //当目标体飞离游戏窗口的时候,还原目标体的坐标,使其继续出现
void Disappear(); //当子弹到达0的时候把该坐标设为0
void Destroy(); //当击中目标体的时候,让目标体消失
void OnTimer(UINT n_timer); //时间函数
void OutPut(); //再屏幕上显示成绩
void Victory(); //当成绩达到500时,显示胜利!
void Fail(); //当飞船与目标体象遇的时候,飞船撞沉,游戏结束
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
HWND hWnd;
MSG msg;
int screenx,screeny;
WNDCLASS wnd;
wnd.style=CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc=WndProc;
wnd.cbClsExtra=0;
wnd.cbWndExtra=0;
wnd.hInstance=hInstance;
wnd.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_UFO));
wnd.hCursor=LoadCursor(NULL,IDC_ARROW);
wnd.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
wnd.lpszMenuName=NULL;
wnd.lpszClassName="宇宙飞船";
if(!RegisterClass(&wnd))
{
MessageBox(NULL,"警告","ok",MB_HELP);
return 0;
}
screenx=GetSystemMetrics(SM_CXSCREEN)/3-150;
screeny=GetSystemMetrics(SM_CYSCREEN)/3-150;
hWnd=CreateWindow("宇宙飞船","飞翔",WS_OVERLAPPEDWINDOW &~WS_MAXIMIZEBOX,
screenx,screeny,530,540,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
MessageBox(NULL,"警告asdfasdfadsf","ok",MB_HELP);
return 0;
}
hWND=hWnd;
hThistance=hInstance;
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
GetClientRect(hWnd,&rt);
ball.x=rt.right/2-20; //设置飞船的初始坐标
ball.y=rt.bottom/2-15;
for(int i=0;i<20;i++) //设置飞船、目标体的初始值
{
m_ball[i].jd=FALSE;
m_ball[i].pt.x=0;
m_ball[i].pt.y=0;
}
for(i=0;i<10;i++)
{
monst[i].m_exit=FALSE;
monst[i].pt.x=0;
monst[i].pt.y=0;
}
SetTimer(hWnd,n_time1,100,NULL); //计时器
SetTimer(hWnd,n_time2,1000,NULL);
SetBackgrand();
SetBoat();
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
BeginPaint(hWND,&ps);
SetBackgrand();
SetBoat();
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
ball.y-=20;
if(IsHead(ball.x,ball.y))
ball.y+=20;
break;
case VK_DOWN:
ball.y+=20;
if(IsHead(ball.x,ball.y))
ball.y-=20;
break;
case VK_LEFT:
ball.x-=20;
if(IsHead(ball.x,ball.y))
ball.x+=20;
break;
case VK_RIGHT:
ball.x+=20;
if(IsHead(ball.x,ball.y))
ball.x-=20;
break;
case VK_SPACE:
x=++x;
x=x%20;
m_ball[x].pt.x=ball.x+17;
m_ball[x].pt.y=ball.y-5;
m_ball[x].jd=TRUE;
break;
}
break;
case WM_TIMER:
OnTimer(wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
void SetBackgrand()
{
HDC dc=GetDC(hWND);
HDC memDC=CreateCompatibleDC(dc);
HBITMAP m_bitmap=(HBITMAP)LoadImage(NULL,"Background.bmp",IMAGE_BITMAP,500,400,LR_LOADFROMFILE);
SelectObject(memDC,m_bitmap);
StretchBlt(dc,rt.left,rt.top,rt.right,rt.bottom,memDC,0,0,500,400,SRCCOPY);
DeleteDC(memDC);
ReleaseDC(hWND,dc);
DeleteObject(m_bitmap);
}
void SetBoat()
{
HDC dc=GetDC(hWND);
HDC cdc=CreateCompatibleDC(dc);
HBITMAP m_boat=(HBITMAP)LoadImage(NULL,"Boat.bmp",IMAGE_BITMAP,40,30,LR_LOADFROMFILE);
SelectObject(cdc,m_boat);
TransparentBlt(dc,ball.x,ball.y,40,30,cdc,0,0,40,30,RGB(255,0,255));
ReleaseDC(hWND,dc);
DeleteDC(cdc);
DeleteObject(m_boat);
}
BOOL IsHead(int x,int y)
{
if(ball.x<0 || ball.x>rt.right-30 || ball.y<0 || ball.y>rt.bottom-20)
return TRUE;
return FALSE;
}
void SetBoot(int i)
{
HDC dc=GetDC(hWND);
HDC cdc=CreateCompatibleDC(dc);
HBITMAP boot=(HBITMAP)LoadImage(NULL,"m_boot.bmp",IMAGE_BITMAP,5,5,LR_LOADFROMFILE);
SelectObject(cdc,boot);
TransparentBlt(dc,m_ball[i].pt.x,m_ball[i].pt.y,5,5,cdc,0,0,5,5,RGB(255,255,255));
ReleaseDC(hWND,dc);
DeleteDC(cdc);
DeleteObject(boot);
}
void SetMonster(int i)
{
HDC dc=GetDC(hWND);
HDC cdc=CreateCompatibleDC(dc);
HBITMAP m_monster=(HBITMAP)LoadImage(NULL,"1.bmp",IMAGE_BITMAP,20,20,LR_LOADFROMFILE);
SelectObject(cdc,m_monster);
TransparentBlt(dc,monst[i].pt.x,monst[i].pt.y,20,20,cdc,0,0,20,20,RGB(255,255,255));
ReleaseDC(hWND,dc);
DeleteDC(cdc);
DeleteObject(m_monster);
}
void OnTimer(UINT n_timer)
{
int i=0;
//当n_timer==n_time1时,设置飞船,背景,计算目标体、子弹的坐标
if(n_timer==n_time1) //否则设置目标体的显示位置
{
SetBackgrand();
SetBoat();
for(;i<10;i++)
{
if(monst[i].m_exit==TRUE)
{
monst[i].pt.y+=10;
SetMonster(i);
}
}
for(i=0;i<20;i++)
{
if(m_ball[i].jd==TRUE)
{
m_ball[i].pt.y-=5;
SetBoot(i);
}
}
OutPut();
Finish();
Disappear();
Destroy();
Victory();
Fail();
}
else
{
while(i<10 && monst[i].m_exit==FALSE)
{
monst[i].pt.x=50*i+rand()%4*25;
monst[i].pt.y=0;
monst[i].m_exit=TRUE;
i++;
}
}
}
void Finish()
{
for(int i=0;i<10;i++)
{
if(monst[i].pt.y==500)
{
monst[i].m_exit=FALSE;
}
}
}
void Disappear()
{
for(int i=0;i<20;i++)
{
if(m_ball[i].pt.y<0)
{
m_ball[i].jd=FALSE;
}
}
}
void Destroy()
{
for(int i=0;i<20;i++)
{
if(m_ball[i].jd==FALSE)
continue;
for(int j=0;j<10;j++)
{
if(monst[j].m_exit==FALSE)
continue;
if(m_ball[i].pt.x+5>monst[j].pt.x && m_ball[i].pt.x+5<monst[j].pt.x+25
&& m_ball[i].pt.y+5>monst[j].pt.y && m_ball[i].pt.y+5<monst[j].pt.y+25)
{
m_ball[i].jd=FALSE;
monst[j].m_exit=FALSE;
score+=10;
MessageBeep(0);
}
}
}
}
void OutPut()
{
HDC hdc=GetDC(hWND);
char str[10]="成绩:";
char temp[30];
SetTextColor(hdc,RGB(255,0,0));
SetBkMode(hdc,TRANSPARENT);
SetBkColor(hdc,RGB(0,0,0));
TextOut(hdc,250,480,str,strlen(str));
itoa(score,temp,10);
TextOut(hdc,290,480,temp,strlen(temp));
ReleaseDC(hWND,hdc);
}
void Victory()
{
if(score==500)
{
KillTimer(hWND,n_time1);
KillTimer(hWND,n_time2);
int i=MessageBox(NULL,"恭喜你,你已经通关了!是否继续?","恭喜",MB_OKCANCEL);
if(i==IDOK)
{
SetTimer(hWND,n_time1,100,NULL);
SetTimer(hWND,n_time2,1000,NULL);
score=0;
}
else
PostQuitMessage(0);
}
}
void Fail()
{
int i=0;
while(i<10)
{
if(ball.x+30>monst[i].pt.x && ball.x+30<monst[i].pt.x+50
&& ball.y+40>monst[i].pt.y && ball.y+40<monst[i].pt.y+60)
break;
i++;
}
if(i<10)
{
KillTimer(hWND,n_time1);
KillTimer(hWND,n_time2);
int j=MessageBox(NULL,"游戏失败,是否继续?","失败",MB_OKCANCEL);
if(j==IDOK)
{
SetTimer(hWND,n_time1,100,NULL);
SetTimer(hWND,n_time2,1000,NULL);
for(int n=0;n<8;n++)
{
monst[n].m_exit=FALSE;
monst[n].pt.y=0;
}
}
els