C + + Write a simple shooting game _c language

Source: Internet
Author: User
Tags rand sprintf textout

First write your own program, very imperfect, there are many problems to solve ... Forgive forgive me

#define GDIPVER 0x0110 #define Win32_lean_and_mean #include <windows.h> #include <ObjIdl.h> #include <gd iplus.h> #include <windowsx.h> #include <tchar.h> #include <mmsystem.h> #include <stdio.h> # Include <stdlib.h> #include <math.h> #include <time.h> #include "resource.h" #include <mmsystem.h& 
Gt
#pragma comment (lib, "Winmm.lib") #pragma comment (lib, "GdiPlus.lib") using namespace Gdiplus;
 
#define WINDOW_WIDTH #define Window_height-static int cxball,cyball;
 
  VOID OnPaint (HDC hdc,int x,int y) {Graphics _g (HDC);       Construction Brush Pen _p (color::red,//color 2.0F);
 
  Pen width (default: 1.0F) _g.drawellipse (&_p,x, Y, 50, 50); Set pen width and color _p.setcolor (255,111,222,55);
Set the color _p.setwidth (3.0F);//Set pen width to get pen width and color _c;_p.getcolor (&_c);//Get color real _r = _p.getwidth ();//Get pen width} 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, "score%d", score);
      TextOut (hdc,900,240,sztext,10);
      times--;
      sprintf (Sztext, "Number of%d", times);
      TextOut (hdc,900,280,sztext,8);  Cxcreat = (int) loword (lparam);  Gets the mouse position x coordinate information cycreat = (int) hiword (lparam);
      Gets the mouse position y-coordinate information setbkmode (HDC, OPAQUE); if (cxcreat> cxball-50&&cxcreat<cxball+50) {if (cycreat>cyball-50&&cycreat<cyball+50) {
           
        score+=100;
        } if (Times <= 0) {score = 0;
        Times = 0;
      MessageBox (Hwnd,text ("more times exceeded"), TEXT ("error"), Mb_iconerror);
      } ReleaseDC (HWND,HDC);
      Break
    return 0;
  } break;
    Case WM_PAINT: {HDC = BeginPaint (HWND,&AMP;PS);
    Movetoex (Hdc,800,0,null);
    LineTo (hdc,800,600);
    Movetoex (Hdc,0,600,null);
    LineTo (hdc,800,600);
    EndPaint (HWND,&AMP;PS);
    return (0);
 
  } break;
    Case Wm_destroy: {PlaySound (NULL, Hinstance_app, Snd_purge);
    PostQuitMessage (0);
    return (0);
 
  } break;
 
  Default:break;
 
Return (DefWindowProc (hWnd, MSG, wparam, lparam));
  int WINAPI WinMain (hinstance hinst, hinstance, LPSTR, INT) {HWND hwnd;
  ULONG_PTR Gdiplustoken;
  Gdiplusstartupinput Gdiplusstartupinput; Status Sresult = GdiplusStartup (&gdiplustoken, &gdiplusstartupinput, NULL);
   
  if (Sresult!= Ok) return 0; Wndclassex wndclassex = {sizeof (wndclassex), Cs_hredraw | Cs_vredraw, WinProc, 0L, 0L, GetModuleHandle (NULL), LoadIcon (Hinst, Makeintresource (Idi_icon1)), L Oadcursor (Hinst, Makeintresource (Idc_cursor1)), (Hbrush) Getstockobject (White_brush), NULL, _t ("Simplewindowcla
  SS "), LoadIcon (Hinst, Makeintresource (Idi_icon1))};
 
     RegisterClassEx (&wndclassex); hwnd = CreateWindow (_t ("Simplewindowclass"), _t ("Pan ' game ~ ~"), ws_overlappedwindow|
    Ws_vscroll, Cw_usedefault, Cw_usedefault, Cw_usedefault, Cw_usedefault, GetDesktopWindow (), NULL,
 
  Wndclassex.hinstance, NULL);
  ShowWindow (hwnd, Sw_showdefault);
  UpdateWindow (HWND);
  HDC HDC;
  HDC = GetDC (hwnd);
  Srand (GetTickCount ());
  Cxball = WINDOW_WIDTH/2;
  Cyball = WINDOW_HEIGHT/2;
  RECT RECT;
  rect.left=0;
  rect.bottom=600; Rect.right=800;
  Rect.top=0;
  int XV = -4+rand ()%8;
   
  int yv = -4+rand ()%8;
  MSG msg;
    do {GetMessage (&msg, NULL, 0U, 0U);
    TranslateMessage (&AMP;MSG);
    DispatchMessage (&AMP;MSG);
    OnPaint (Hdc,cxball,cyball);
    Cxball + = XV;
    Cyball + = YV;
      if (Cxball < 0 | | | cxball > 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;
 }

Above is the article to share the C + + written shooting small game code, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.