专栏首页magicsoarC++ 游戏之点点水果

C++ 游戏之点点水果

大二时利用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=0;
char cScore[20];
  
int numberOfBomb=0;
int const fruitLength=11;
bool gameOver=false;
bool gameStart=true;
Fruit* (myFruit[])={ new Fruit("apple.png",15,15,-9,5),
                    new Fruit("apple.png",150,15,7,5),
                    new Fruit("apple.png",40,315,1,5),
                    new Fruit("apple.png",600,215,9,-5),
                    new  Fruit("banana.png",100,125,10,6),
                    new  Fruit("banana.png",320,315,-7,7),
                    new  Fruit("banana.png",320,315,7,-7),
                    new Fruit("strawberry.png",400,415,6,8),
                    new  Fruit("pineapple.png",500,215,-5,9),
                     new  Fruit("pineapple.png",550,115,5,-9),
                     new  Fruit("watermelon.png",650,15,5,-9)};

Fruit* (Bomb[25])={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, 0,0 ,PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        else
        {
            
            //if(gameStart==false&&gameOver==false)
            {
                tNow = GetTickCount();
                
                if(tNow-tPre >= 40)
                    MyPaint(hdc);
                if(gameStart==false&&gameOver==false)
                {
                    tNow2=GetTickCount();
                if(tNow-tCheck>=8000)
                    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        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = NULL;
    wcex.hCursor        = NULL;
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    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, 800, 600, 0, 0, hInstance, 0);
    if (!hWnd)
    {
        return FALSE;
    }
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    hdc = GetDC(hWnd);
    mdc = CreateCompatibleDC(hdc);


    bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,800,600,LR_LOADFROMFILE);
    font=(HBITMAP)LoadImage(NULL,"bgOver.bmp",IMAGE_BITMAP,800,600,LR_LOADFROMFILE);
    start=(HBITMAP)LoadImage(NULL,"bgStart.bmp",IMAGE_BITMAP,800,600,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[80]="Score:";
    SelectObject(mdc,bg);
    BitBlt(hdc,0,0,800,600,mdc,0,0,SRCCOPY);
    sprintf(cScore, "%d", score);
    old_fcolor=SetTextColor(hdc,RGB(0,255,0));
    old_bcolor=SetBkColor(hdc,RGB(0,0,0));
    old_tmode=SetBkMode(hdc,TRANSPARENT);
    strcat(ss,cScore);
    TextOut(hdc,10,10,ss,strlen(ss));
    SetTextColor(hdc,old_fcolor);
    SetBkColor(hdc,old_bcolor);
    SetBkMode(hdc,old_tmode);

    for(int i=0;i<fruitLength;i++)
    {
        myFruit[i]->Draw(hdc);
        myFruit[i]->Move(rect);
    }
    for(int i=0;i<numberOfBomb;i++)
    {
        Bomb[i]->Draw(hdc);
        Bomb[i]->Move(rect);
    }
    

    tPre = GetTickCount();  
    }
    if(gameOver==true)
    {
        char ss[80]="Your Score is ";
        SelectObject(mdc,font);
        BitBlt(hdc,0,0,800,600,mdc,0,0,SRCCOPY);
        sprintf(cScore, "%d", score);
        old_fcolor=SetTextColor(hdc,RGB(255,0,0));
        old_bcolor=SetBkColor(hdc,RGB(0,0,0));
        old_tmode=SetBkMode(hdc,TRANSPARENT);
        strcat(ss,cScore);
        TextOut(hdc,10,10,ss,strlen(ss));
        SetTextColor(hdc,old_fcolor);
        SetBkColor(hdc,old_bcolor);
        SetBkMode(hdc,old_tmode);
    }
    if(gameStart==true)
    {
        SelectObject(mdc,start);
        BitBlt(hdc,0,0,800,600,mdc,0,0,SRCCOPY);
    }
}

void productBomb()
{
    if(numberOfBomb<20)
    {
        Bomb[numberOfBomb]->changePosition(rect);
        numberOfBomb++;
    }
    tCheck = GetTickCount();  
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    WORD mouseX=0;
    WORD mouseY=0;  
    
    switch (message)
    {
        case WM_KEYDOWN:                    
            if(wParam==VK_ESCAPE)        
                PostQuitMessage(0);
            if(gameOver==true)
            {
                if(wParam==82||wParam==114)
                {
                    numberOfBomb=0;
                    gameOver=false;
                    gameStart=true;
                }
            }
            if(gameStart==true)
            {
                if(wParam==32)
                {
                    gameStart=false;
                    score=0;
                }
            }
            break;

        case WM_DESTROY:                    
            DeleteDC(mdc);
            DeleteObject(bg);
            DeleteObject(font);
            for(int i=0;i<fruitLength;i++)
            {
                myFruit[i]->Destroy();
            }
            for(int i=0;i<20;i++)
            {
                Bomb[i]->Destroy();
            }
            ReleaseDC(hWnd,hdc);
            PostQuitMessage(0);
            break;
        case WM_LBUTTONDOWN:
            mouseX=LOWORD(lParam);
            mouseY=HIWORD(lParam);
            for(int i=0;i<fruitLength;i++)
            {
                if(myFruit[i]->checkClick(mouseX,mouseY))
                {
                    myFruit[i]->changePosition(rect);
                    score+=AddScore(myFruit[i]->GetName());
                }
            }
            for(int i=0;i<numberOfBomb;i++)
            {
                if(Bomb[i]->checkClick(mouseX,mouseY))
                {
                    gameOver=true;
                }
            }
        default:                            
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
int  AddScore(std::string name)
{
    if(name=="apple.png")
    {
        return 100;
    }
    if(name=="banana.png")
    {
        return 50;
    }
    if(name=="strawberry.png")
    {
        return 50;
    }
     if(name=="pineapple.png")
    {
        return 25;
    }

     if(name=="watermelon.png")
    {
        return 10;
    }
     return 0;
    
}

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(28)-14;
        int i=rand()/2;
        int j=sqrt((double)(200-speedX*speedX));
        speedY=(i==1)?j:-j;
    }
void Fruit:: Draw(HDC hdc)
    {
        image.TransparentBlt (hdc,positionX,positionY,image.GetWidth(),image.GetHeight(),RGB(255,255,255));
    }
void Fruit::Move(RECT rect)
    {
        positionX+=speedX;
        positionY+=speedY;
        if(positionX <= 0)
        {
            positionX = 0;
            speedX = -speedX;
        }
        else if(positionX >= rect.right-image.GetWidth())
        {
            positionX = rect.right - image.GetWidth();
            speedX = -speedX;
        }
        if(positionY<=0)
        {
            positionY = 0;
            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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++ socket网络爬虫(1)

    C++写的socket网络爬虫,代码会在最后一次讲解中提供给大家,同时我也会在写的同时不断的对代码进行完善与修改 我首先向大家讲解如何将网页中的内容,文本,图片...

    magicsoar
  • C++内存布局(1)-让new出的两个变量在堆上的地址连续

    大家都知道栈的地址按照从高到低的顺序增长的, 而堆的地址是按照从底到高的顺序增长的。 int *n1 = new int(1); int *n2 = new i...

    magicsoar
  • windows下的C++ socket服务器(3)

    int make_server_socket(int port) { WSADATA inet_WsaData;//1 WSAStar...

    magicsoar
  • java 五子棋

    week
  • 【死磕Java并发】—–J.U.C之并发工具类:CountDownLatch

    在上篇博客中介绍了Java四大并发工具一直的CyclicBarrier,今天要介绍的CountDownLatch与CyclicBarrier有点儿相似。 Cyc...

    用户1655470
  • 带货直播系统源码中,商品详情页是如何搭建起来的

    市面上大多数带货直播系统源码的商品介绍详情页,都是仿照淘宝的商品详情页的制作的,那么这个模块是如何通过代码建立起来的呢?下面小编将会通过代码来描述其实现过程。

    万岳教育系统
  • AtCoder Beginner Contest 170

    C.题意:就是让你找到不在数组中的距离X的绝对值最近的那个值,值得注意的是你得遍历到比数据范围大一的那个位置。

    用户7727433
  • ImageLoader

    这次做一个图片加载器,里面涉及到线程池,bitmap的高效加载,LruCache,DiskLruCache。接下来我先介绍这四个知识点

    提莫队长
  • Android实现仿支付宝流水

    今天给大家讲的是如何自定义下拉的ListView实现支付宝账单的效果,月份是需要悬浮的,然后没一个月归为一类,先看一个效果图吧。 ? 场景:后台下发的数据就是一...

    xiangzhihong
  • mysql数据库基础语法

    说到数据库语法,需要先提一下数据库表,数据库语法操作的就是数据库的表。一个数据库通常包含一个或多个表。每个表由一个名字标识(例如“客户”或者“订单”)。表包含带...

    无邪Z

扫码关注云+社区

领取腾讯云代金券