6 Replies - 562 Views - Last Post: 20 May 2012 - 03:38 AM Rate Topic: -----

#1 Bazzalisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-May 12

Confused on how this can work...

Posted 19 May 2012 - 08:36 AM

I've spent quite a while trying to figure this out. But i cant, i have no clue.
I'm trying to make a very VERY simple fighting game,
i tried making two classes, with health, attack, avoid(forget about it for now) and heal. This game isn't going to be a turn based game. it will be basicaly. player 1 uses the key "a" to attack and player 2 uses "l" and there are no turns, player 1 can just spam "a" and kill player 2 very fast. So, i was wondering how would it work, im so confused. It appears im making a massive ask, but i really would like some help. I just want to know how to make it so when player 1 presses "a" 3 times, then player 2's health is decreased by 30.

ps: sorry about that code, it doesn't really help at all but im giving what i can =\.

class player1
{
	public:

		player1():
		  int health = 100;
		  
		  int attack = 10;
		  
		  int avoid = 500;
		  
		  int heal = 10;
		  

		 
		  void attackmsg()
		  {
			  cout << playername " dealed " attack " damage to" playername2".\n";
		  }

		  void avoidmsg()
		  {
			  cout << playername " avoided " playername2 "'s attack. \n";
		  }

		  void healmsg()
		  {
			  cout << playername " has healed " heal "health. \n";
		  }

	
}

	
}

class player2
{
	public:

		player2():
		  int health2 = 100;
		  
		  int attack2 = 10;
		  
		  int avoid2 = 500;
		  
		  int heal2 = 10;
		  

		 
		  void attackmsg2()
		  {
			  cout << playername2 " dealed " attack2 " damage to" playername".\n";
		  }

		  void avoidmsg2()
		  {
			  cout << playername2 " avoided " playername "'s attack. \n";
		  }

		  void healmsg2()
		  {
			  cout << playername2 " has healed " heal2 "health. \n";
		  }

	
		  
}
	
}


Thank you very much for reading and i will rep you for your answer. ^^

Is This A Good Question/Topic? 0
  • +

Replies To: Confused on how this can work...

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Confused on how this can work...

Posted 19 May 2012 - 08:56 AM

I guess you will have to use a non-standard way of doing this.. If you are on Windows, include Windows.h then use GetAsyncKeyState() to check if 'a' or 'l' was pressed.. If you can use conio.h, use getch(). By taking the input through these methods, if 'l' is pressed, then do something like
player1.health-=attack;
player1.attackmsg();


Also, note that it is bad practice to make all members of a class public. Try making them private and creating public functions to access them.

This post has been edited by aresh: 19 May 2012 - 08:57 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bazzalisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-May 12

Re: Confused on how this can work...

Posted 19 May 2012 - 05:44 PM

I'm sorry, but could you help me a bit more on Hotmail or msn :S.
it's something I've wanted to do for ages, I'm just a beginner. Everything I learn I am taking in and practising.

If you don't want to, thank you anyway! ^^
Was This Post Helpful? 0
  • +
  • -

#4 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Re: Confused on how this can work...

Posted 19 May 2012 - 06:12 PM

You will need to sit in a loop and wait for keyboard input, something like:

while (GAME_ON)
{
  char c = getch(); // #include the conio.h header for this one

  if (c == 'a')
  {
    player1_attack(player2);
  }
  else if (c == 'l')
  {
    player2_attack(player1);
  }

  if (player1_health == 0 || player2_health == 0)
  {
    GAME_ON == false;
  }
}


This post has been edited by peace_fixation: 19 May 2012 - 06:13 PM

Was This Post Helpful? 2
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1369
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: Confused on how this can work...

Posted 19 May 2012 - 10:02 PM

Hi, you can have one Player class and create several objects from that class. Your class declaration is not quite right in a couple of respects. So have a look at tutorials on creating classes.


So a Player needs to hit another Player, and also take a hit from another Player.

Here cin.get() is used so enter needs to be pressed after a letter is input.



#include <iostream>
#include <string>

using namespace std;


class Player
{
  public:
    int health;
    int attack;
    string name;

    // constructor - initialize data
    Player(string playername = "Nobody")
    {
      health = 100;
      attack = 10;
      name = playername;
    }

    // hit another player
    void hit(Player other)
    {
      other.takehit(attack);
      attackmsg(other);
    }

    // take a hit
    void takehit(int hitamount)
    {
      health = health - hitamount;
    }


    void attackmsg(Player other)
    {
      cout << name
           << " dealt "
           << attack
           << " damage to "
           << other.player_name()
           << ".\n";
    }


    string player_name() {return name;}
};



int main()
{
  Player cat("Cat-astrophe"), mouse("Supermouse");

  bool playing = true;

  while(playing)
  {
    char ch;

    cout << "enter >";
    ch = cin.get();
    cin.ignore();
    
    switch(ch)    
    {
      case 'a': cat.hit(mouse);
                break;
      case 'l': mouse.hit(cat);
                break;
      case 'q': playing = false;
                break;
    }

  }


  cin.get();
  return(0);
}


Was This Post Helpful? 0
  • +
  • -

#6 Bazzalisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-May 12

Re: Confused on how this can work...

Posted 19 May 2012 - 10:57 PM

I'll have a mess around with it, thanks #define! :)
Was This Post Helpful? 0
  • +
  • -

#7 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: Confused on how this can work...

Posted 20 May 2012 - 03:38 AM

@Peace Fixation : Change line 16 to
GAME_ON = false ;
And yes, his method is the most ideal one, because you don't have to press enter after every attack, as in the case of #define. Alternatively, if you don't want to use conio.h and getch(), you can use GetAsyncKeyState(). Here is the sample code :
while (GAME_ON)
{
  char b = GetAsyncKeyState('A'); // #include the Windows.h header for this one
  char c = GetAsyncKeyState('L'); // It returns 0 if the key is not pressed, otherwise -32767 or something

  if (B)/>
  {
    player1_attack(player2);
  }
  if (c) //With GetAsyncKeyState, no need to use else, because both can attack simultaneously.
  {
    player2_attack(player1);
  }

  if (player1_health == 0 || player2_health == 0)
  {
    GAME_ON = false;
  }
  Sleep(150); //Another function defined in Windows.h, and is used to delay the execution for 150(or whatever you specify) mili-seconds. Necessary with GetAsyncKeyState.
}

There should also be getch() in ncurses, so if you use linux, use getch(). But note that my method will keep on looping and attack only if key is pressed, so in the meantime you can show other animations or something, making it real time. But with getch(), your game becomes completely turn based.

This post has been edited by aresh: 20 May 2012 - 09:48 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1