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

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

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

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

游戏的开始界面

游戏中界面

游戏的结束界面

游戏的代码

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;
}

C++ 游戏之点点水果的更多相关文章

  1. java游戏制作之水果忍者

    水果忍者的原理很简单,主要就是采用随机的方式是画面上面出现水果. package Fruitninja; import java.awt.Dimension; import java.awt.Grap ...

  2. Python开发接水果小游戏

    我研发的Python游戏引擎Pylash已经更新到1.4了.如今我们就来使用它完毕一个极其简单的小游戏:接水果. 下面是游戏截图: 游戏操作说明:点击屏幕左右两边或者使用键盘方向键控制人物移动.使人物 ...

  3. 练手WPF(四)——贪吃蛇小游戏的简易实现(下)

    八.生成新的单节蛇身我们这里先说说游戏小原理好了,游戏运行后,通过计时器事件不断生成新的单节蛇身类SnakeNode,添加到List中的0位置,原来的蛇头变成了第二节.该节新蛇头的坐标通过蛇头前进方向 ...

  4. 纯css3手机页面图标样式代码

    全部图标:http://hovertree.com/texiao/css/19/ 先看效果: 或者点这里:http://hovertree.com/texiao/css/19/hoverkico.ht ...

  5. 使用Visual Studio进行 Android开发的十大理由

    [原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...

  6. 设计模式之十八:桥接模式(Bridge)

    桥接模式: 将抽象部分和它的实现部分相分离开来,以使它们能够单独地变化. UML图: 主要包含: Abstraction:定义了抽象部分的接口.操作一个实现部分对象的引用. RefinedAbstra ...

  7. 少儿编程|Scratch编程教程系列合集,总有一款适合你

    如果觉得资源不错,友情转发,贵在分享!!! 少儿编程Scratch: 少儿编程Scratch第一讲:Scratch完美的初体验少儿编程Scratch第二讲:奇妙的接球小游戏少儿编程Scratch第三讲 ...

  8. 基于HTML5和JS实现的切水果游戏

    切水果游戏曾经是一款风靡手机的休闲游戏,今天要介绍的就是一款网页版的切水果游戏, 由JavaSript和HTML5实现,虽然功能和原版的相差太大,但是基本的功能还是具备了,还是模仿的挺逼真,有一定的J ...

  9. ios水果风暴游戏源码下载

    游戏源码是从那个IOS教程网IOS.662p.com分享给大家的. 这是一款ios水果风暴游戏源码下载,介绍给大家一下,喜欢的朋友可以下载学习一下吧.应用介绍:这是一个以获得高分和挑战更高难度为目的的 ...

随机推荐

  1. Matlab中的“prod”函数

    B = prod(A)将A矩阵不同维的元素的乘积返回到矩阵B. 如果A是向量,prod(A)返回A向量的乘积.如果A是矩阵,prod(A)将A看作列向量,返回每一列元素的乘积并组成一个行向量B.如果A ...

  2. RMAN 与control文件的关系

    试验: SQL> oradebug setmypid;Statement processed.SQL> 接着,进行backup: RMAN> list backup; using t ...

  3. PostgreSQL调整内存与IO的参数说明

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 [作者:高健@博客园 luckyjackgao ...

  4. mfc 类对象的引用

    类对象引用 自写复制构造函数 一. 类对象引用 在第4课的时候,我们已经讨论过C++引用特性.类变量的引用呢,实际上也是类似的. Tdate d1; Tdate &d2=d1; 二.自写复制构 ...

  5. opencv-Getting Started with Images

    1.opencv库简单的操作图片 # coding = utf-8 # 书籍:<<学习opencv>> import cv2 from matplotlib import py ...

  6. 异步任务(Celery)详解

    一.背景 在开发中,我们可能经常会遇到一些需要执行时间很长的任务,如果放在前端,会让用户一直卡在那儿等待或者一直转圈圈,体验非常不好.为了改善这种体验,我赶紧上网搜索,果然,前人早已有解决办法了.那就 ...

  7. Float浮点数的使用和条件

    在这里简单的说一下,我对浮点数的理解,可能说的比较浅,老师也没有说,只是略微的提了一下,完全是我自己个人的理解. 我觉得float浮点数的用法和int的用法有些雷同,浮点数用于计算小数点单位,我们先可 ...

  8. Mysql Mariadb 密码问题

    mysql密码遗忘和登陆报错问题   mysql登录密码忘记,其实解决办法很简单,只需要在mysql的主配置文件my.cnf里添加一行“跳过授权表”的参数选择即可! 在my.cnf中添加下面一行:[r ...

  9. 【Unity Shader】渲染管线

    流程概述 应用程序阶段 应用程序阶段,使用高级编程语言(C.C++.JAVA 等)进行开发,主要和CPU.内存打交道,诸如碰撞检测.场景图建立.空间八叉树更新.视锥裁剪等经典算法都在此阶段执行.在该阶 ...

  10. python打印对象的所有可操作内容

    print('\n'.join(['%s:%s' % item for item in 对象.__dict__.items()]))