C + + flying game development Log

Source: Internet
Author: User


Design ideas:
Console mode
Initialization
Set up the screen, initialize the data
Game process:
1. Get the action
2. Modify the data
3. Update screen
End:
Close screen, delete dynamically allocate data

4.29 Days

Create a game background to enable the aircraft to move operations, to achieve bullet flight

4.30 days

Achieve game data management, aircraft shooting down animation, random enemy aircraft

Code:

#include <iostream>#include<list>#include<time.h>#include<easyx.h>#include<graphics.h>#include"stdio.h"#include"math.h"#include"Dos.h"#include<windows.h>#include<mmsystem.h>#include<cstdlib>#defineKey_down (Vk_c) (Getasynckeystate (Vk_c) &0x8000)using namespacestd;/*How to design an airplane game? It's not good to write. Class handling is general: Refactoring: flyingobject{data: Pos,speed,dir operation: Getpos Setpos getspeed Set ... Move} class: Bullet plane Console Mode initialization: Build screen, initialize data game process: 1. Get action 2. Modify Data 3. End of update screen: Close screen, delete dynamic allocation data*/Doubledirection[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };structpos{Doublex, y;};classbullet{ Public: Bullet (pos _p,BOOL_UP):p (_p), Speed (1), Up (_up) {} inlineBOOLCheck () {if(P.x <0|| P.y <0|| P.x>768|| P.y> +)            return false; return true; }    voidgetimg () {LoadImage (&img, _t ("D:\\bullet1.ico")); }    voidMove (); intgetspeed () {returnSpeed ; } POS Getpos () {returnp; } IMAGE img;Private: POS p; intSpeed ; BOOLUp ;};voidBullet::move () {if(UP) p.y-=getspeed (); Elsep.y+=getspeed ();} IMAGE background,ash;list<bullet>L;classplane{ Public:    ~plane () {} plane (int_x,int_y) {p.x= _x; P.Y = _y; Speed =1; }    voidMove (intdir); voidSetpos (DoubleXDoubley) {p.x= x; P.Y =y; }    voidSetspeed (Double_s) { speed=_s; }    voidShoot (BOOLUp ); voidDrawplane (); POS Getpos () {returnp; } IMAGE img;Private:    DoubleSpeed ;    POS p; intLife ;}; List<plane>Planelist;plane Hero ( the, the);voidplane::D rawplane () {putimage (p.x, P.Y,&img);}voidPlane::move (intdir) {    if(p.x + direction[dir][0] >=- -&& p.x + direction[dir][0] <= -) Setpos (p.x+ direction[dir][0]*speed, P.y + direction[dir][1] *Speed ); Else if(p.x + direction[dir][0] * Speed <- -) Setpos ( -, P.y + direction[dir][1] *Speed ); ElseSetpos (0, P.y + direction[dir][0] *Speed );}voidPlane::shoot (BOOLUp ) {POS TP=p; Tp.x+= the;    Bullet TMP (TP, UP); Tmp.    Getimg ();    L.insert (L.begin (), TMP); Putimage (tp.x, P.Y,&tmp.img); Flushbatchdraw ();}voidinit () {initgraph ( +,768); LoadImage (&background, _t ("d:\\background.jpg")); LoadImage (&hero.img, _t ("D:\\hero1.ico")); LoadImage (&ash, _t ("D:\\ashes.ico")); Putimage (0,0, &background);}voidCheck () {POS tmp; if(!l.empty ()) for(List<bullet>::iterator it = L.begin (); it! =l.end ();) {It-Move (); TMP= it->Getpos (); if(Tmp.y <0|| Tmp.y> the) {It=L.erase (IT); }        Elseit++; }     for(List<plane>::iterator it = Planelist.begin (); it! =planelist.end ();) {It->move (2); TMP= it->Getpos (); if(Tmp.y <- -|| Tmp.y> -) {It=Planelist.erase (IT); }        Elseit++; }    intD = -;  for(List<bullet>::iterator it = L.begin (); it! =l.end ();) {Auto Tbullet= it->Getpos (); BOOLf =false;  for(Auto k = Planelist.begin (); K! =planelist.end ();) {Auto TPOs= k->Getpos (); Tpos.x+= the; Tpos.y+= the; if(tbullet.x-tpos.x<d&&tbullet.x-tpos.x>-d) && (Tbullet.y-tpos.y<d&&tbullet.y- tpos.y>-d)) {f=true; K=Planelist.erase (k); }            Elsek++; }        if(!f) It++; Elseit=L.erase (IT); }}voidUpdate () {Putimage (0,0, &background); if(rand ()% +==0) {plane P1 (rand ()% -+ -,0); P1. Setspeed (0.2); LoadImage (&p1.img, _t ("D:\\plane1.ico"));    Planelist.insert (Planelist.begin (), p1); } hero.    Drawplane ();    Check (); if(!l.empty ()) for(List<bullet>::iterator it = L.begin (); It! = L.end (); it++) {Auto T= it->Getpos (); Putimage (T.x, T.y,&it->img); }     for(Auto it = Planelist.begin (); It! = Planelist.end (); it++) {It-Drawplane (); } flushbatchdraw ();}intMain () {init ();    Beginbatchdraw ();  while(1) {update (); if(Key_down (vk_up)) {hero. Move (3); }        Else if(Key_down (Vk_left)) {hero. Move (1); }        Else if(Key_down (vk_right)) {hero. Move (0); }        Else if(Key_down (Vk_down)) {hero. Move (2); }        Else if(Key_down (Vk_return) | |Key_down (Vk_space)) {             while(Key_down (Vk_return) | |Key_down (Vk_space)) { }            if(L.size () <5) Hero. Shoot (true); }    }    return 0; }

5.1 Days

The feeling class is poorly written, resulting in a lot of duplicated code. decided to refactor.

C + + flying game development Log

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.