The---of Win32 game production Swordsman

Source: Internet
Author: User
Tags set background textout dota

Preface  

Today is the final of the NBA Finals, or Rob Seven wars, the great James finally realized, the game suddenly found that James Beard is a little white, originally those who accompany their own many basketball stars have been old, Kobe also retired, they each have a dream and struggle for it, James for his dream to struggle for so many years to realize the dream, then he should also strive to achieve their dreams, of course, just want to achieve their own heart of a small dream, not everyone can say for their hometown to bring the champion of the same rhetoric.


--- this to the great James, and to myself, and to all who go on the path of the dream

Some time ago because of busy preparing for school exams, no time to do what they want to do, plus this morning there are NBA Finals, so long no blog , after exams, I began to do a little game---swordsman, of course, this is just I give a name, is not that swordsman, and the real swordsman game still has the very big disparity.

The scene of the game in the picture is I use Photoshop mosaic, I also feel bad, we look good. The game of birds and boss dragons are also made with Photoshop, the following look at the actual:

Game Initial state:


Game Skill Release:

Every time you hit a bird with a skill, you get points, and when the score reaches 100, the boss appears,


Of course the boss will release the skills and take a look:


The boss skill is a set of dynamic graphs, the range is the center of the 150 pixels, of course, the boss will first determine the position of the player, and then release skills, but here is only a preliminary judgment, AI is a big topic. Now finally understand why AI is difficult to design, and always hated AI, such as when I play Kingdoms series games always think AI is not intelligent, now finally understand the difficulty of AI. When you play DotA or LOL, you may feel the computer is weak (but I never feel the AI in the DotA is weak, I mean I often hit the computer O (╯-╰) o).

In fact, I feel the most difficult or collision detection , I am in the code is also done some relatively simple detection, in fact, this piece really is very difficult, first you have to think of all kinds of situations, even if the corner angle encountered, may also be an effective collision. Such collision detection is particularly important in FPS-type games, such as whether bullets hit people and what parts of the person are hit. These aspects, the foreign do very well, especially some 3 A-level masterpiece. In their game engine there is a special detection mechanism.

Here's a look at the code, in order to save space, I give the important code, the code to generate the window is omitted.

Header file #pragma once#include "GameEngine.h" #include <Windows.h> #include <tchar.h>/Use swprintf The header files required for the _s function include "resource.h" #define Key_down (Vk_code) (Getasynckeystate (Vk_code) &0x8000) 1:0) #define Key_up (Vk_code) ((Getasynckeystate (Vk_code) &0x8000) 0:1) struct swordbullets//on behalf of the protagonist                       The sword Gas {int x, y;                       Character Sword gas coordinates bool isexist;                       Whether the sword gas exists};struct birdmaster{int x, y;                       Represents the coordinates of the bird passing through the bool isexist;                       Indicates whether the bird exists int picnum;                       The marking of the figures (altogether 5) Hbitmap Hbird;  Bitmap handle of the bird};                      struct bossmaster{int x, y;                      Represents the boss of the mapping coordinate int picnum;                      The label of the boss graph (altogether 6) int ipicnum;                      Boss Skill map is labeled Hbitmap Hboss;                      Boss's bitmap handle int boss_blood;                     Boss's blood volume hbitmap hbossskill; Boss Skill handle int skill_x, skill_y;                      Boss's skill coordinates int boss_x, boss_y;            Boss's Center coordinate};int g_ix = 0, G_iy = 0; Mouse position <pre name= "code" class= "CPP" >//Global device handle, global memory DC handle, and buffer dchdc G_HDC = null, G_MDC = NULL, G_BUFDC = NULL; Defines five bitmap handles for storing backgrounds, characters, sword gas, failure, victory hbitmap G_hbackground = NULL , G_hswordman = null, G_hswordblade = NULL, G_hgameover = NULL, g_hvictory = NULL; int G_ixswordman = 0, G_iyswordman = 0; The position of the character records the coordinates of the current person, that is, the mapping coordinate int g_ibgoffset = 0, G_ibulletnum = 0, g_ibirdnum = 0;                            G_ibgoffset is the width of the area to be cropped for the scrolling background, g_ibulletnum the number of sword gas swordbullets bullet[30];                                Birdmaster BIRD[10];                                     Bossmaster Boss;                                    int g_score = 0;                                     Player scored int g_life = 0;                              Player Blood volume bool G_isboss = FALSE; Whether the boss exists//Time count DWORD g_tPre = 0, G_tnow = 0; GameEngine * G_pgame = null;//in-game detection function (it seems unnecessary, mainly for the sake of future code maintenance) bool Is_in_randge (Birdmaster &bir); bool Is_hit_ Bird (Birdmaster &bir, swordbullets &bullet); void Bird_hit_man (Birdmaster &bir); void Boss_appear (); void Boss_initalize (); void Is_hit_boss (Bossmaster &bos, swordbullets &bullet); bool Boss_hit_man (BossMaster & BOS); #pragma

#include "Swordsman.h" BOOL gameinitalize (hinstance hinstance)//game initialization function {PlaySound (L "Paladin three • Original theme song", NULL, Snd_filename | Snd_async | Snd_loop) Try{g_pgame = new GameEngine (HINSTANCE, L "Swordsman", L "Swordman", Idi_swordman, IDI_SWORDMAN_SM);} catch (...) {return FALSE;} G_pgame->setframedelay (g_life = 100;g_score = 0;                                Boss_initalize (); Initialize some information of the boss, meet certain conditions to appear return TRUE; void Gamestart (HWND Hwindow)//game start function {hbitmap bmp;g_hdc = GetDC (Hwindow); G_MDC = CreateCompatibleDC (G_HDC); G_BUFDC = CreateCompatibleDC (G_HDC); bmp = CreateCompatibleBitmap (G_HDC, Window_width, window_height); G_iX = 300;g_iy = 100;g_ixswordman = 300;g_iyswordman = 100;                     SelectObject (G_MDC, BMP); Select empty map//load various bitmaps G_hswordman = (hbitmap) loadimage (NULL, L "Gamemedia\\swordman.bmp", Image_bitmap, 317, 283, lr_ G_hswordblade = (hbitmap) loadimage (NULL, L "Gamemedia\\swordblade.bmp", Image_bitmap, LoadFromFile, LR_ LoadFromFile); G_hbackground = (HBITMAP) LoadImage (NULL, L "Gamemedia\\bg.bmp", Image_bitmap, Window_width, Window_height, lr_loadfromfile); g_ Hgameover = (hbitmap) loadimage (NULL, L "Gamemedia\\gameover.bmp", Image_bitmap, 1086, 396, lr_loadfromfile); g_hvictory = (HBITMAP) loadimage (NULL, L "Gamemedia\\victory.bmp", Image_bitmap, N, +, lr_loadfromfile); Point pt, LT, RB; RECT rect;//Sets the position of the cursor pt.x = 300;PT.Y = 100; ClientToScreen (Hwindow, &pt); Setcursorpos (Pt.x, PT.Y);                             ShowCursor (FALSE); Hide Mouse GetClientRect (Hwindow, &rect); lt.x = Rect.left;lt.y = rect.top;rb.x = Rect.right;rb.y = Rect.bottom; ClientToScreen (Hwindow, <); ClientToScreen (Hwindow, &AMP;RB); rect.left = Lt.x;rect.top = Lt.y;rect.right = Rb.x;rect.bottom = rb.y;// Limits the mouse's moving area clipcursor (&rect); Gamepaint (Hwindow);} void GamePlay () {Gamepaint (G_pgame->getwindow ()); if (G_ibirdnum <=) {g_tpre = GetTickCount (); if (G_tpre-g_tnow >) {for (int j = 0; J < Ten; J + +) {if (! bird[j].isexist) {bird[j].x = 0; BIRD[J].Y = rand ()%window_height; Bird[j].isexist = true; Bird[j].picnum = 0; Bird[j].hbird = (hbitmap) loadimage (NULL, L "Gamemedia\\bird.bmp", Image_bitmap, 1250, and lr_loadfromfile); g_ibirdNum ++;g_tnow = G_tpre;break;}}}} void Gamepaint (HWND Hwindow)//game Draw function {//First paste the background image in the memory DC SelectObject (G_BUFDC, G_hbackgrou nd); BitBlt (G_MDC, 0, 0, G_ibgoffset, window_height, G_BUFDC, Window_width-g_ibgoffset, 0, srccopy); BitBlt (G_MDC, G_ibgoffset, 0, Window_width-g_ibgoffset, window_height, G_BUFDC, 0, 0, srccopy); TCHAR str[20] = {};//calculates the map coordinates of the swordsman, and when the map is set, the swordsman will slowly move towards the target in 10 pixels (G_ix > G_ixswordman)//If the current mouse x sits The coordinates are larger than the characters, then go forward (left) {G_ixswordman + = 10;if (G_ixswordman > G_ix) {g_ixswordman = G_ix;}} else//If the current mouse x coordinate is less than the coordinates of the person, then back (right) {G_ixswordman-= 10;if (G_ixswordman < G_ix) {G_ixswordman = G_ix;}} if (G_iy > G_iyswordman) {g_iyswordman + = 10;if (G_iyswordman > G_iy) {g_iyswordman = G_iy;}} Else{g_iyswordman-= 10;if(G_iyswordman < 0) {G_iyswordman = 0;}} Paste the Swordsman Figure SelectObject (G_BUFDC, G_hswordman); TransparentBlt (G_MDC, G_ixswordman, G_iyswordman, 317, 283, G_BUFDC, 0, 0, 317, 283, RGB (0, 0, 0));//Sword gas (bullet) map, first determine the number of sword gas (bullets) Whether the value of "G_ibulletnum" is "0". If it is not 0, the SelectObject (G_BUFDC, G_hswordblade) of the Sword Gas (bullets) in the sword Gas (bullet) array in the same coordinate (b[i].x,b[i].y ! = 0) {for (int i = 0; i < i++) {if (bullet[i].isexist) {//detects if there is no hit Bossis_hit_boss (Boss, Bullet[i]); if (Boss.boss_blood <= 0)//If Boss dies, stick out the winning picture {G_isboss = false; SelectObject (G_BUFDC, g_hvictory); TransparentBlt (G_MDC, +, 0, 0, 0, 0); G_pgame->setgamestate (TRUE);} (n); G_BUFDC In turn, determine if there is a hit bird for (int j = 0; J < + j + +) {if (Is_hit_bird (Bird[j], bullet[i])) {bullet[i].isexist = false;g_ibulletnum--; Bird[j].isexist = False;g_ibirdnum--;g_score + = 10;if (G_score >=) {G_isboss = true;}}} if (bullet[i].isexist) {TransparentBlt (G_MDC, bullet[i].x-70, bullet[i].y + 0, 0,, G_BUFDC,(0, 0, 0));} Set the coordinates of the next sword gas (bullet). The Sword Gas (bullet) is emitted from right to left, so the coordinate value on the x-axis decreases by 10 units each time, so that the map produces the effect of moving to the left. If the next coordinate of the sword gas (bullet) is beyond the visible range of the window (h[i].x<0), then the Sword Gas (bullet) is set to not exist, and the total number of sword gas (bullets) g_ibulletnum variable value minus 1.bullet[i].x = 10;if (bullet[i].x < 0) {g_ibulletnum--; Bullet[i].isexist = false;}}} Determine whether the number of birds "G_ibirdnum" is the value "0". If not 0, then the Sword Gas (bullet) in the array of sword gas (bullets) in accordance with its coordinates, loop map operation if (g_ibirdnum! = 0) {for (int i = 0; i <; i++) {if (bird[i].isexist) {if (Bird[i].picnum >= 5) {bird[i].picnum = 0;} SelectObject (G_BUFDC, Bird[i].hbird); BitBlt (G_MDC, bird[i].x, Bird[i].y, (+), G_BUFDC, Bird[i].picnum * +, Srcand); BitBlt (G_MDC, bird[i].x, Bird[i].y, G_BUFDC, 0, Bird[i].picnum), if (Srcpaint (Is_in_randge))) {if (B Ird[i].y < G_iyswordman) {bird[i].y + = 10;} ELSE{BIRD[I].Y-= 10;} bird[i].x + = 10;}                                   else{bird[i].x + = 10; Set the bird's forward Route}if (bird[i].x > Window_width) {bird[i].isexist = false;g_ibirdnum--;} Bird[i].picnum++;bird_hit_man (Bird[i]);}} Determine if the boss exists if (G_isboss) {boss_appear ();} Create font Hfont hfont;hfont = CreateFont (0, 0, 0, 0, 0, gb2312_charset, 0, 0, 0, 0, TEXT ("Microsoft Jas Black"));//select font in G_mdcsel Ectobject (G_MDC, Hfont);//Set background color transparent SetBkMode (G_MDC, TRANSPARENT);//Set Text color SetTextColor (G_MDC, RGB (255, 0, 0));// In the upper right corner of the text output swprintf_s (str, L "player blood Volume (Life):%d%%", g_life); TextOut (G_MDC, 850, 0, str, wcslen (str)); swprintf_s (str, L "player score (Score):%d", g_score); TextOut (G_MDC, 0, str, wcslen (str));//Paste BitBlt (g_hdc, 0, 0, window_width, window_height, G_MDC, 0, 0, srccopy);//Background Move G _ibgoffset + = 5;if (g_ibgoffset >= window_width)//If it is greater than the window width, it becomes 0{g_ibgoffset = 0;}} void Gameactivate (HWND Hwindow)//game activated {HDC hdc; Rect rect; GetClientRect (Hwindow, &rect); hdc = GetDC (Hwindow);D Rawtext (hdc, L "Welcome to Back",-1, &rect, Dt_singleline | Dt_center | Dt_vcenter); ReleaseDC (Hwindow, HDC);} void Gamedeactivate (HWND Hwindow)//game not activated {HDC hdc; Rect rect; GetClientRect (Hwindow, &rect); hdc = GetDC (Hwindow);D Rawtext (HDC, L "Pause",-1, &rect, Dt_singleline | Dt_center | Dt_vcenter); ReleaseDC (Hwindow, HDC);} void Gameend ()//game End Function {//Cleanup of resources DeleteObject (G_hbackground);D Eletedc (G_BUFDC);D eleted C (G_MDC); ReleaseDC (G_pgame->getwindow (), G_HDC);} void Handlekeys ()//key handling function {if (Key_down (vk_escape))/ /press ESC to exit {DestroyWindow (G_pgame->getwindow ()); return;} if (Key_down (VK_UP))//press Up (arrow up) player move up {g_iyswordman-= 50;if (G_iyswordman < 0) {G_iys Wordman = 0;}} if (Key_down (vk_shift))//press SHIFT for acceleration {g_ibgoffset + = 15;} if (Key_down (Vk_lbutton))//press the left mouse button {for (int i = 0; i <; i++) {if (! bullet[i].isexist) {bullet[i].x = G_ixswordman; BULLET[I].Y = G_iyswordman + 30; Bullet[i].isexist = True;g_ibulletnum++;break;}}} if (Key_down (Vk_down))//press Up (arrow up) to move the player {G_iyswordman + = 50;if (G_iyswordman> window_height) {g_iyswordman = Window_height;}}} BOOL Is_in_randge (Birdmaster &bir)//Determine if the player is within the bird's field of view radius, if so, then fly to the player {if (bir.isexist) {if (Bir.x & Lt G_ixswordman) {if (((Bir.x-g_ixswordman) * (Bir.x-g_ixswordman) + (Bir.y-g_iyswordman) * (Bir.y-g_iyswordman)) <= 90 000)//detect if within a circle {return true;}} return false;} Else{return false;}}  BOOL Is_hit_bird (Birdmaster &bir, swordbullets &bullet)//Judging skill has no hit bird {if (bir.isexist) {if (bullet.y + > Bir.y + +) && (Bullet.y + < BIR.Y +)) {if (Bullet.x < bir.x + +) && (bullet.x &G T Bir.x +)) {return true;}}} return false;}  void Bird_hit_man (Birdmaster &bir)//Bird collision with player {if (bir.isexist) {if ((Bir.y +) <= G_iyswordman + +) && ((bir.y + >= G_iyswordman +)) {if ((bir.x + > (G_ixswordman +)) & & ((bir.x + +) < (G_ixswordman +)) {bir.isexist = False;g_life-= 10;if (G_life = = 0) {SelectObject (G_BUFDC, g_hgameover); The player dies, post the failed picture BitBlt (G_MDC, 0, G_BUFDC, 543, 543, 396, Srcand); BitBlt (G_MDC, N, 0, 543, 396, G_BUFDC, 0, Srcpaint); G_pgame->setgamestate (TRUE);}}}} void Boss_appear ()//boss appears {if (Boss.picnum >= 6) {boss.picnum = 0;}                       SelectObject (G_BUFDC, Boss.hboss); Stick to the Boss of the picture BitBlt (G_MDC, Boss.x, Boss.y, $, $, G_BUFDC, Boss.picnum *, Srcand); BitBlt (G_MDC, Boss.x, Boss.y, G_BUFDC, 0, Boss.picnum); Boss.picnum++;if (((Boss.boss_x-g_ixswordman) * (Boss.boss_x-g_ixswordman) + (Boss.boss_y-g_iyswordman) * (Boss.boss_ Y-g_iyswordman)) <360000) {if (Boss.ipicnum >= 6) {boss.ipicnum = 0;} if (boss.skill_x-boss.x >= 380) {boss.skill_x = boss.x + 200;} if (Boss.skill_y-boss.y >= 280) {boss.skill_y = Boss.y + 100;}                     SelectObject (G_BUFDC, Boss.hbossskill); Skill map with Boss BitBlt (G_MDC, boss.skill_x, boss.skIll_y, G_BUFDC, Boss.ipicnum, Srcand); BitBlt (G_MDC, boss.skill_x, boss.skill_y, G_BUFDC, 0, Boss.ipicnum); Boss.ipicnum++;if (Boss_hit_man (Boss)) {g_life-= 20;if (g_life <= 0)//Determine if the player still has blood volume , if not, paste the failed picture {g_life = 0;                 SelectObject (G_BUFDC, g_hgameover); BitBlt (G_MDC, 543, G_BUFDC, 543, 0, Srcand); BitBlt (G_MDC, N, 0, 543, 396, G_BUFDC, 0, Srcpaint); G_pgame->setgamestate (TRUE);}} Else{if (boss.x >= g_ixswordman-400) {boss.x-= rand ()% 40;} Else{if (Boss.y > G_iyswordman) {boss.y-= 15;if (Boss.x < G_ixswordman) {boss.x + = 10;} Else{boss.x-= 20;}} else if (Boss.y < G_iyswordman) {boss.y + 15;if (Boss.x < G_ixswordman) {boss.x + = 10;} Else{boss.x-= 20;}} Else{if (Boss.x < G_ixswordman) {boss.x + = 10;} Else{boss.x-= 20;}}}} The initialization of void Boss_initalize ()//boss {g_isboss = false; Boss.boss_blood = 100; Boss.hboss = (HBITMAP) LoadImage (NULL, L "Gamemedia\\boss.bmp", Image_bitmap, 1320, lr_loadfromfile); Load boss Bitmap Boss.hbossskill = (hbitmap) loadimage (NULL, L "Gamemedia\\bossskill.bmp", Image_bitmap,, Lr_  LoadFromFile); Load boss Skill Map Boss.picnum = 0; Boss.ipicnum = 0; boss.x = 0; Boss.y = rand ()% Window_height;                     boss.skill_x = boss.x + 200; The initial position of the skill set boss.skill_y = Boss.y + 100;                      boss.boss_x = boss.x + 150; Boss's center position boss.boss_y = boss.y + 100;} BOOL Boss_hit_man (Bossmaster &bos)//Determine if the boss is colliding with the player, or if the boss skill is hitting the player {Bos. Skill_x + = 30;if (bos. Skill_y + < G_iyswordman) {bos.y + = 5;bos. Skill_y + = 20;} else if (BOS. Skill_y + > G_iyswordman) {bos.y-= 5;bos. Skill_y-= 20;} Else{bos.y = Bos.y;bos. skill_y = Bos. skill_y;} Determine if the boss touches the player if ((bos.boss_y <= G_iyswordman +) && (bos.boss_y >= G_iyswordman +)) {if (Bos.boss_x & Gt (G_ixswordman + 35)) && (Bos.boss_x < (G_ixswordman +))) {g_life-= 15;}} Determines if the boss skill hits the player if ((BOS. Skill_y + <= G_iyswordman + +) && (BOS. Skill_y + () >= G_iyswordman +) {if ((BOS) Skill_x +) > (G_ixswordman +)) && ((Bos. Skill_x + < (G_ixswordman +)) {return true;}} return false;}  void Is_hit_boss (Bossmaster &bos, swordbullets &bullet)//player hits Boss{if (Bullet.y + > (bos.boss_y -) && (Bullet.y + < Bos.boss_y +) {if (Bullet.x < bos.boss_x + +) && (Bullet.x > Bos                               . boss_x-30)) {Bos.boss_blood-= 15;g_score + = 20;bullet.isexist = false; The bullet disappears after hitting g_ibulletnum--;}}}

in the code basically have comments, here does not analyze, boss has 500 points of blood, is really not so simple:

But to be simple and easy, to reduce the boss blood volume to 1, O (∩_∩) O, to a victory as the end:


Of course, this is just a very simple game, we can look at, but also welcome you to give some suggestions! (PS: Window generated that part of the WIN32 code is a fixed format, in order to save space, so did not post)


The---of Win32 game production Swordsman

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.