C++编写简单的打靶游戏

首次自己写程序,很不完善,还有许多问题需要解决。。。见谅见谅

#define GDIPVER 0x0110 
#define WIN32_LEAN_AND_MEAN 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "resource.h"
#include  
#pragma comment(lib, "winmm.lib")
#pragma comment(lib,"GdiPlus.lib")
using namespace Gdiplus;
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
static int cxball,cyball;
 
VOID OnPaint(HDC hDC,int x,int y)
{
  Graphics _g(hDC);
 
  //构造画笔
  Pen _p(
    Color::Red,     //颜色
    2.0F);       //笔宽(默认:1.0F)
  _g.DrawEllipse(&_p,x, y, 50, 50);
 
  //设置笔宽与颜色
  _p.SetColor(Color(255,111,222,55)); //设置颜色
  _p.SetWidth(3.0F);//设置笔宽
 
  //获得笔宽与颜色
  Color _c;_p.GetColor(&_c);//获取颜色
  REAL _r = _p.GetWidth();//获取笔宽
}
LRESULT CALLBACK WinProc(HWND hWnd, 
              UINT msg, 
              WPARAM wparam, 
              LPARAM lparam)
{
 
 
static PMSG pmsg;
 
switch(msg)
  {
    PAINTSTRUCT  ps ;
    HDC hDC;
    static int  cxClient, cyClient ;
    static int cxcreat,cycreat;
    static int times,score;
 
    TCHAR szText[256];
 
  case WM_CREATE: 
    {
    hDC = GetDC(hWnd);
     
    PlaySound(MAKEINTRESOURCE(IDR_WAVE1), hinstance_app, SND_RESOURCE | SND_ASYNC);
     
    times = 9;
    score = 0;
    ReleaseDC(hWnd,hDC);
    return(0);
    } break;
  case WM_SIZE:
    {
     cxClient = LOWORD (lparam) ;
     cyClient = HIWORD (lparam) ;
     return 0 ;
    }
    break;
 
  case WM_LBUTTONDOWN :
    {
      switch (wparam)
      {
      case MK_LBUTTON:
       
      hDC = GetDC(hWnd);
      sprintf(szText,"得分为 %d",score);
      TextOut(hDC,900,240,szText,10);
      times--;
      sprintf(szText,"次数为 %d",times);
      TextOut(hDC,900,280,szText,8);
      cxcreat = (int)LOWORD( lparam ) ;  //获取鼠标位置x坐标信息
      cycreat = (int)HIWORD( lparam ) ;  //获取鼠标位置y坐标信息
      SetBkMode(hDC, OPAQUE);
      if(cxcreat>cxball-50&&cxcreatcyball-50&&cycreat WINDOW_WIDTH - 50)
    {
      xv=-xv;
      cxball += xv;
    }
    else if (cyball < 0 || cyball > WINDOW_HEIGHT - 50)
    {
      yv=-yv;
      cyball += yv;
    }
    OnPaint(hdc,cxball,cyball);
    Sleep(10);
    InvalidateRect(hwnd,&rect,TRUE);
  }while(Msg.message != WM_QUIT);
   
  ReleaseDC(hwnd,hdc);
  UnregisterClass(
    _T("SimpleWindowClass"),
    WndClassEx.hInstance);
  GdiplusShutdown(GdiplusToken);
  return 0;
}

以上就是本文给大家分享的C++编写的打靶小游戏的代码了,希望大家能够喜欢。

你可能感兴趣的:(C++编写简单的打靶游戏)