C++ 游戏之点点水果

时间:2023-03-09 23:21:01
C++ 游戏之点点水果

大二时利用C++编写的点水果小游戏

程序代码总共3个文件,main.cpp Fruit.h Fruit.cpp  代码将在图片下面给出

至于讲解,由于过了一年多的时间,有点忘记了,但我会努力回忆并即时写出来的。

程序的下载地址http://files.cnblogs.com/magicsoar/clickfruit.rar包括了需要的图片素材

游戏的开始界面

C++ 游戏之点点水果

游戏中界面

C++ 游戏之点点水果

游戏的结束界面
C++ 游戏之点点水果

游戏的代码

main.cpp

 
#include <stdio.h>
#include <string>
#include <Windows.h>
#include "atlimage.h"
#include "Fruit.h" #include <vector>
using namespace std;
#define random(x) (rand()%x)
HINSTANCE hInst;
HBITMAP bg,font,start;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow,tNow2,tCheck;
RECT rect;
int score=;
char cScore[]; int numberOfBomb=;
int const fruitLength=;
bool gameOver=false;
bool gameStart=true;
Fruit* (myFruit[])={ new Fruit("apple.png",,,-,),
new Fruit("apple.png",,,,),
new Fruit("apple.png",,,,),
new Fruit("apple.png",,,,-),
new Fruit("banana.png",,,,),
new Fruit("banana.png",,,-,),
new Fruit("banana.png",,,,-),
new Fruit("strawberry.png",,,,),
new Fruit("pineapple.png",,,-,),
new Fruit("pineapple.png",,,,-),
new Fruit("watermelon.png",,,,-)}; Fruit* (Bomb[])={new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),
new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),
new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),
new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),
new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png"),new Fruit("bomb.png")}; ; int AddScore(std::string name);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
void productBomb();
void initFruit();
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg; MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} GetMessage(&msg,NULL,NULL,NULL);
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, , ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{ //if(gameStart==false&&gameOver==false)
{
tNow = GetTickCount(); if(tNow-tPre >= )
MyPaint(hdc);
if(gameStart==false&&gameOver==false)
{
tNow2=GetTickCount();
if(tNow-tCheck>=)
productBomb();
} }
}
} 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 = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL; return RegisterClassEx(&wcex);
} BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
//HBITMAP bmp;
hInst = hInstance; initFruit(); hWnd = CreateWindow("canvas", "Soar" , WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, , , , , hInstance, );
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd); hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc); bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
font=(HBITMAP)LoadImage(NULL,"bgOver.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
start=(HBITMAP)LoadImage(NULL,"bgStart.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
GetClientRect(hWnd,&rect);
MyPaint(hdc); return TRUE;
} void initFruit()
{ }
void MyPaint(HDC hdc)
{
COLORREF old_fcolor,old_bcolor;
int old_tmode; if(gameOver==false&&gameStart==false)
{
char ss[]="Score:";
SelectObject(mdc,bg);
BitBlt(hdc,,,,,mdc,,,SRCCOPY);
sprintf(cScore, "%d", score);
old_fcolor=SetTextColor(hdc,RGB(,,));
old_bcolor=SetBkColor(hdc,RGB(,,));
old_tmode=SetBkMode(hdc,TRANSPARENT);
strcat(ss,cScore);
TextOut(hdc,,,ss,strlen(ss));
SetTextColor(hdc,old_fcolor);
SetBkColor(hdc,old_bcolor);
SetBkMode(hdc,old_tmode); for(int i=;i<fruitLength;i++)
{
myFruit[i]->Draw(hdc);
myFruit[i]->Move(rect);
}
for(int i=;i<numberOfBomb;i++)
{
Bomb[i]->Draw(hdc);
Bomb[i]->Move(rect);
} tPre = GetTickCount();
}
if(gameOver==true)
{
char ss[]="Your Score is ";
SelectObject(mdc,font);
BitBlt(hdc,,,,,mdc,,,SRCCOPY);
sprintf(cScore, "%d", score);
old_fcolor=SetTextColor(hdc,RGB(,,));
old_bcolor=SetBkColor(hdc,RGB(,,));
old_tmode=SetBkMode(hdc,TRANSPARENT);
strcat(ss,cScore);
TextOut(hdc,,,ss,strlen(ss));
SetTextColor(hdc,old_fcolor);
SetBkColor(hdc,old_bcolor);
SetBkMode(hdc,old_tmode);
}
if(gameStart==true)
{
SelectObject(mdc,start);
BitBlt(hdc,,,,,mdc,,,SRCCOPY);
}
} void productBomb()
{
if(numberOfBomb<)
{
Bomb[numberOfBomb]->changePosition(rect);
numberOfBomb++;
}
tCheck = GetTickCount();
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WORD mouseX=;
WORD mouseY=; switch (message)
{
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
PostQuitMessage();
if(gameOver==true)
{
if(wParam==||wParam==)
{
numberOfBomb=;
gameOver=false;
gameStart=true;
}
}
if(gameStart==true)
{
if(wParam==)
{
gameStart=false;
score=;
}
}
break; case WM_DESTROY:
DeleteDC(mdc);
DeleteObject(bg);
DeleteObject(font);
for(int i=;i<fruitLength;i++)
{
myFruit[i]->Destroy();
}
for(int i=;i<;i++)
{
Bomb[i]->Destroy();
}
ReleaseDC(hWnd,hdc);
PostQuitMessage();
break;
case WM_LBUTTONDOWN:
mouseX=LOWORD(lParam);
mouseY=HIWORD(lParam);
for(int i=;i<fruitLength;i++)
{
if(myFruit[i]->checkClick(mouseX,mouseY))
{
myFruit[i]->changePosition(rect);
score+=AddScore(myFruit[i]->GetName());
}
}
for(int i=;i<numberOfBomb;i++)
{
if(Bomb[i]->checkClick(mouseX,mouseY))
{
gameOver=true;
}
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
}
int AddScore(std::string name)
{
if(name=="apple.png")
{
return ;
}
if(name=="banana.png")
{
return ;
}
if(name=="strawberry.png")
{
return ;
}
if(name=="pineapple.png")
{
return ;
} if(name=="watermelon.png")
{
return ;
}
return ; }

Fruit.h

#pragma once
#include <string>
#include "atlimage.h"
using namespace std;
class Fruit
{
private:
std::string name;
int positionX;
int positionY;
int speedX;
int speedY;
CImage image;
public:
Fruit(void);
~Fruit(void);
Fruit(CString);
Fruit(CString name,int positionX,int positionY,int speedX,int speedY);
void changePosition(RECT rect);
void Draw(HDC);
int GetX();
int GetY();
string GetName();
void Move(RECT rect);
bool checkClick(WORD mouseX,WORD mouseY);
void Destroy();
};

Fruit.cpp

#include "Fruit.h"
#include <string>
using namespace std;
#define random(x) (rand()%x)
Fruit::Fruit(void)
{
}
Fruit::Fruit(CString name,int positionX,int positionY,int speedX,int speedY)
{
this->name=name;
image.Load(name);
this->positionX=positionX;
this->positionY=positionY;
this->speedX=speedX;
this->speedY=speedY;
}
Fruit::Fruit(CString name)
{
this->name=name;
image.Load(name);
} Fruit::~Fruit(void)
{
}
void Fruit::changePosition(RECT rect)
{
positionX=random(rect.right-image.GetWidth());
positionY=random(rect.bottom-image.GetHeight());
speedX=random()-;
int i=rand()/;
int j=sqrt((double)(-speedX*speedX));
speedY=(i==)?j:-j;
}
void Fruit:: Draw(HDC hdc)
{
image.TransparentBlt (hdc,positionX,positionY,image.GetWidth(),image.GetHeight(),RGB(,,));
}
void Fruit::Move(RECT rect)
{
positionX+=speedX;
positionY+=speedY;
if(positionX <= )
{
positionX = ;
speedX = -speedX;
}
else if(positionX >= rect.right-image.GetWidth())
{
positionX = rect.right - image.GetWidth();
speedX = -speedX;
}
if(positionY<=)
{
positionY = ;
speedY = -speedY;
}
else if(positionY>= rect.bottom-image.GetHeight())
{
positionY = rect.bottom - image.GetHeight();
speedY = -speedY;
}
}
void Fruit::Destroy()
{
image.Destroy();
}
string Fruit::GetName()
{
return name;
}
bool Fruit::checkClick(WORD mouseX,WORD mouseY)
{ if(mouseX>positionX&&mouseX<positionX+image.GetWidth())
{
if(mouseY>positionY&&mouseY<positionY+image.GetHeight())
return true;
}
return false;
}
int Fruit::GetX()
{
return positionX;
}
int Fruit::GetY()
{
return positionY;
}