zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Maven 集成Tomcat插件
    dubbo 序列化 问题 属性值 丢失 ArrayList 解决
    docker 中安装 FastDFS 总结
    docker 从容器中拷文件到宿主机器中
    db2 相关命令
    Webphere WAS 启动
    CKEDITOR 4.6.X 版本 插件 弹出对话框 Dialog中 表格 Table 自定义样式Style 问题
    SpringMVC JSONP JSON支持
    CKEDITOR 3.4.2中 按钮事件中 动态改变图标和title 获取按钮
    git回退到远程某个版本
  • 原文地址:https://www.cnblogs.com/magicsoar/p/3589675.html
Copyright © 2011-2022 走看看