C + + game series 5: more than one weapon

Source: Internet
Author: User
Tags gety

many others see: C + + game series Folders

Knowledge Point: An array of objects as a data member
Improvement: The weapons held by each character are not just one, so the weapons they hold are represented by an array of objects, and of course, they can be empty-handed.

As a result, it is also recorded that a common possession of a few weapons, currently armed with which weapons.

"Project-Role has a variety of weapons"
1.game.h: class declaration

#ifndef game_h_included#define game_h_included#include <string>usingnamespace Std;Const intn=Ten;//Each character has the most weaponsConst intnoweapon=-1;//means no weapons in handClass Point//point class declaration{ Public://external interface Point(intx=0,inty=0);intGetX ();intGetY ();DoubleDistanceConstPoint &p);//Returns the distance from the other point P    voidMoveTo (intXinty);//Move to a different point    voidMoveintDxintDY);//move from current positionPrivate:intx, y;//coordinates};class weapon{ Public:Weapon(){}; Weapon (stringWnam,intFDoublek); Weapon (Constweapon&);stringGetwname ();intGetforce ();//Return to lethality    DoubleGetkillrange ();//return kill distancePrivate:stringWname;//Name    intForce//Lethality    DoubleKillrange;//Kill distance};class role{ Public:Role(stringNamintB, point L, weapon w[],intn);//Constructors~role ();//destructor    voidEatintD);//eat, up D blood (after death eat something to revive)    voidAttack (Role &r);//attack others, self-rising blood, at the same time the other side was attacked blood loss. The amount of blood depends on the current weapon.    voidBeattack (intf);//Be attacked by others, the number of references F is to withstand the attack    DoubleDistance (Role &r);//Return to the distance with a character    BOOLIsalived ();//Whether alive    voidMoveTo (intXinty);//Move to a different point    voidMoveintDxintDY);//move from current position    voidChangeweapon (intWNO);//Exchange weapons in hand    voidShow ();//DisplayPrivate:stringName//Role name    intBlood//Current blood volume    BOOLLife//Whether alivePoint location;//LocationWeapon Weapons[n];//Weapons    intWeaponnum;//Number of weapons    intHoldweapon;//What weapon is now in hand (empty-handed for noweapon, initial time-space hand)};#endif //game_h_included

2.point.cpp, defining the point class, representing the location

#include "game.h"#include <cmath>Point::P oint (intXintY): X (x), Y (y) {}intPoint::getx () {returnx;}intPoint::gety () {returnY;}//Move to a different pointvoidPoint::moveto (intXintY) { This->x=x; This->y=y;}//move from current positionvoidPoint::move (intDxintDY) { This->x+=dx; This->y+=dy;}DoublePoint::d istance (Constpoint& p) {DoubleDX = This->x-p.x;DoubleDY = This->y-p.y;return(sqrt (DX * dx + dy * dy));}

3.weapon.cpp, defining Weapon classes

#include "game.h"Weapon::Weapon(stringintdouble k):wname(wnam),force(f),killRange(k) {}Weapon::Weapon(const Weapon &w):wname(w.wname),force(w.force),killRange(w.killRange) {}string Weapon::getWname(){    return wname;}//返回杀伤力int Weapon::getForce(){    return force;}//返回杀伤距离double Weapon::getKillRange(){    return killRange;}

4.role.cpp, defines the role class. The role of participation in the game

#include <iostream>#include "game.h"using namespace STD; Role::role (stringNamintB, point L, weapon w[],intN): Name (NAM), blood (b), location (L), Weaponnum (n), Holdweapon (noweapon) {if(blood>0) life=true;ElseLife=false; for(intI=0; i<n; i++) weapons[i]=w[i];} Role::~role () {cout<<name<<"Exit the lake ..."<<endl;}//eat, up D blood (after death eat something to revive)voidRole::eat (intD//eat, up D blood (can eat, others feed, so that can revive){Blood+=d;if(blood>0) life=true;}//attack others and raise your own blood. At the same time, the opponent was attacked and bled. The amount of blood depends on the current weapon.//ability to attack within the range of the weapon's attackvoidRole::attack (Role &r) {if(Isalived () &&holdweapon>noweapon&&weapons[holdweapon].getkillrange () > This->distance (R))//alive and within the scope of the killing{Blood+=weapons[holdweapon].getforce ();    R.beattack (Weapons[holdweapon].getforce ()); }}//Be attacked by others. The number of references F is the damage that is sustainedvoidRole::beattack (intf) {blood-=f;if(blood<=0) life=false;}//Return to the distance with a characterDoubleRole::d istance (role &r) {returnLocation.distance (r.location);}//Exchange weapons in handvoidRole::changeweapon (intWNO) {if(Wno<weaponnum) Holdweapon=wno;}//Whether aliveBOOLRole::isalived () {returnLife;}//Move to a different pointvoidRole::moveto (intXintY) {if(Isalived ())//You can't move when you're dead.Location.moveto (x, y);}//move from current positionvoidRole::move (intDxintDY) {if(Isalived ()) Location.move (Dx,dy);}//DisplayvoidRole::show () {cout<<name<<"has"<<blood<<"Blood, hold.";if(Holdweapon==noweapon)cout<<"No Weapon";Else        cout<<weapons[holdweapon].getwname ();cout<<". He is in ("<<location.getx () <<", "<<location.gety () <<") and";if(Isalived ())cout<<"alived.";Else        cout<<"dead.";cout<<endl;}

5.main.cpp, test function, indicating position

#include <iostream>#include "game.h"Using namespaceSTD;int main () {Weapon w1[1]= {Weapon ("Gold stick", $, -)};//Golden CudgelWeapon w2[3]= {Weapon ("Fire-tip Lance", the, -),//Fire tip gun Weapon ("Universal Ring", -, -),//The Universe Circle Weapon ("Sky muddling Damask", -, +)//Mixed-day Aya};Role WuKong ("WuKong", -, Point (0,0), W1,1);Role Nezha ("Nezha", About, Point ( -, -), W2,3);WuKong. Changeweapon(0);Nezha. Changeweapon(0);cout<<"---begin---"<<endl;WuKong. Show();Nezha. Show();cout<<"---1st round---"<<endl;WuKong. Attack(Nezha);WuKong. Show();Nezha. Show();cout<<"---2nd round---"<<endl;Nezha. Changeweapon(2);Nezha. Attack(WuKong);WuKong. Show();Nezha. Show();cout<<"---3rd round---"<<endl;Nezha. MoveTo( -, -); WuKong. Attack(Nezha);WuKong. Show();Nezha. Show();cout<<"---4th round---"<<endl; Nezha. Attack(WuKong);WuKong. Show();Nezha. Show();cout<<"---then---"<<endl; Nezha. Attack(WuKong);Nezha. Attack(WuKong);WuKong. Attack(Nezha);WuKong. Show();Nezha. Show();cout<<"---end---"<<endl;Return0;}

C + + game series 5: more than one weapon

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.