What wrong with this player class

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 470 Views - Last Post: 19 August 2014 - 06:02 PM Rate Topic: -----

#1 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

What wrong with this player class

Posted 18 August 2014 - 03:25 PM

Hey I am a noob, I am trying to understand how to use objects. I am trying to modify player health in a action that could be though of as being attacked and re displaying the new values. I am reading book after book to learn python but objects and functions are not clicking hoping someone can help me out with this. Its not homework or anything just me trying to learn by doing any help will make me a grateful noob.

# a python game for learning
	
class player:
	health = 100
	armor = 100
	ammo = 100
	#return health, armor, ammo
	
	def incoming_damage(dps):
		if player.armor > 0:
			player.armor = player.armor - 25
			print "Your armor has been reduced by: 25"
			return player.armor
		elif player.armor < 0:
			player.health = player.health - 10
			print "Your health has been reduced by: 10"
			return player.health
		else:
			return 0;
	
	
player1 = player()
	


while player.health > 0:
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player.health, player.armor, player.ammo)
	print "_____________________________________________________________"

	print "Main game loop running!"	
	print hud
	player.incoming_damage(player1)
	print hud
	
	print "_____________________________________________________________"
	player.health = 0

This post has been edited by andrewsw: 18 August 2014 - 03:32 PM
Reason for edit:: Use CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: What wrong with this player class

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: What wrong with this player class

Posted 18 August 2014 - 03:34 PM

Please attempt to use code tags, this is particularly important for Python code.

Look in the area where you are typing your post, it demonstrates how to add these tags.

What's wrong with your code? Are there errors?
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7748
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: What wrong with this player class

Posted 18 August 2014 - 03:35 PM

You need to learn about self. 'self' is the first argument to an instance method in python: it means "the object that we're dealing with right now"

imagine we have an object called foo, which is an instance of class Foo.
class Foo has a method definition

def frobnicate (self, other):
...


think of a call to

foo.frobnicate(bar)

as being translated to frobnicate(foo, bar)

and passed to the CLASS Foo.

self in the function now refers to foo, the instance we're interested in, and it all works.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: What wrong with this player class

Posted 18 August 2014 - 03:45 PM

Once you've fixed self (so that you distinguish between the class and a current instance of the class) you should also note that printing hud twice will print the same string, it won't be re-evaluated between each printing.
Was This Post Helpful? 1
  • +
  • -

#5 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 18 August 2014 - 04:04 PM

View Postandrewsw, on 18 August 2014 - 03:34 PM, said:

Please attempt to use code tags, this is particularly important for Python code.

Look in the area where you are typing your post, it demonstrates how to add these tags.

What's wrong with your code? Are there errors?


Um well I am thinking I am using the object wrong, but what I was trying to do was have an object to give a player the values for health armor and ammo, then I wanted to make a function to damage the player lowering those values.


I see what you talking about for code tags I am sorry for not using them in the first place.

View Postandrewsw, on 18 August 2014 - 03:45 PM, said:

Once you've fixed self (so that you distinguish between the class and a current instance of the class) you should also note that printing hud twice will print the same string, it won't be re-evaluated between each printing.



Ok how what I change that print statement to reflect changes to the variable's? If I can ask that.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7748
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: What wrong with this player class

Posted 18 August 2014 - 04:08 PM

distinction between class and object. class is the common noun, object is the proper name. "dog" is a class of animals, Fido is an instance of dog.

"player" is your class. changing its values is like walking all dogs, when you want to walk Fido.

sorry about the telegraphic speech, I'm on my phone.
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: What wrong with this player class

Posted 18 August 2014 - 04:10 PM

Quote

Ok how what I change that print statement to reflect changes to the variable's? If I can ask that.

You could just redefine it each time:
hud = "..."
print hud
# laterz..
hud = "..."
print hud

Better would be to create a method of your class that returns this formatted string.

This post has been edited by andrewsw: 18 August 2014 - 04:10 PM

Was This Post Helpful? 1
  • +
  • -

#8 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 18 August 2014 - 04:15 PM

View Postjon.kiparsky, on 18 August 2014 - 04:08 PM, said:

distinction between class and object. class is the common noun, object is the proper name. "dog" is a class of animals, Fido is an instance of dog.

"player" is your class. changing its values is like walking all dogs, when you want to walk Fido.

sorry about the telegraphic speech, I'm on my phone.


Thanks so while I can use a class to give properties, my mistake is trying to change those properties on the fly as if the player is the class.

View Postandrewsw, on 18 August 2014 - 04:10 PM, said:

Quote

Ok how what I change that print statement to reflect changes to the variable's? If I can ask that.

You could just redefine it each time:
hud = "..."
print hud
# laterz..
hud = "..."
print hud

Better would be to create a method of your class that returns this formatted string.



Oh yea daaa... I was just printing the same thing two times because I had saved the string to a variable.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: What wrong with this player class

Posted 18 August 2014 - 04:16 PM

Quote

my mistake is trying to change those properties on the fly as if the player is the class.

That's right. Use 'self' throughout (within the class) to refer to the current instance of the class, and capitalize the class-name Player. Class-names should be capitalized.

This post has been edited by andrewsw: 18 August 2014 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#10 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 18 August 2014 - 04:21 PM

Ok, now my code does what I wanted it to do. I see what I did wrong but does this work the way it should or did I just get lucky that it worked in the first place?


# a python game for learning
	
class player:
	health = 100
	armor = 100
	ammo = 100
	#return health, armor, ammo
	
	def incoming_damage(dps):
		if player.armor > 0:
			player.armor = player.armor - 25
			print "Your armor has been reduced by: 25"
			return player.armor
		elif player.armor < 0:
			player.health = player.health - 10
			print "Your health has been reduced by: 10"
			return player.health
		else:
			return 0;
	
	
player1 = player()
	


while player.health > 0:
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player.health, player.armor, player.ammo)
	print "_____________________________________________________________"

	print "Main game loop running!"	
	print hud
	player.incoming_damage(player1)
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player.health, player.armor, player.ammo)
	print hud
	
	print "_____________________________________________________________"
	player.health = 0



View Postandrewsw, on 18 August 2014 - 04:16 PM, said:

Quote

my mistake is trying to change those properties on the fly as if the player is the class.

That's right. Use 'self' throughout (within the class) to refer to the current instance of the class, and capitalize the class-name Player. Class-names should be capitalized.


Reworking it as you are sub-jesting ill re-post when I have fixed it.

This post has been edited by andrewsw: 18 August 2014 - 04:38 PM
Reason for edit:: Removed double quote

Was This Post Helpful? 0
  • +
  • -

#11 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 18 August 2014 - 05:57 PM

View Postandrewsw, on 18 August 2014 - 03:45 PM, said:

Once you've fixed self (so that you distinguish between the class and a current instance of the class) you should also note that printing hud twice will print the same string, it won't be re-evaluated between each printing.



Is this the correct usage of self that you were talking about? Code does what I wanted it to do just want to know if I did it right in terms of being able to reuse the class for other players.


# a python game for learning
	
class player:
	health = 100
	armor = 100
	ammo = 100
	#return health, armor, ammo
	
	def incoming_damage(self):
		if player.armor > 0:
			player.armor = player.armor - 25
			print "Your armor has been reduced by: 25"
			return player.armor
		elif player.armor < 0:
			player.health = player.health - 10
			print "Your health has been reduced by: 10"
			return player.health
		else:
			return 0;
	
	


player1 = player()
	


while player1.health > 0:
	print "_____________________________________________________________"
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player1.health, player1.armor, player1.ammo)
	print "Main game loop running!"	
	print hud
	player1.incoming_damage()
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player1.health, player1.armor, player1.ammo)
	print hud
	
	print "_____________________________________________________________"
	player1.health = 0



Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7748
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: What wrong with this player class

Posted 18 August 2014 - 06:08 PM

No. player refers to the class. This reduces armor by 25 or health by 10 for anything that happens to be a player.

You've done the right thing to start by passing self as an argument to the function. Now we know what instance is getting hit. Now you have to hit that self and not all players.



example:

class Value (object):
    def __init__(self, val):
        self.val=val

    def increment(self):
        self.val +=1

    def decrement(self):
        self.val -=1



try that out in the interpreter, and see what happens when you do stuff like this:

>>> v = Value(4)
>>> v.increment()
>>> v.val
>>> v.decrement()
>>> v.val

Was This Post Helpful? 1
  • +
  • -

#13 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 18 August 2014 - 10:20 PM

Is this better or am I way of base on what you are talking about?


# a python game for learning
	
class Player:
	health = 100
	armor = 100
	ammo = 100
	
	def incoming_damage(self):
		if self.armor > 0:
			self.armor = self.armor - 25
			print "Your armor has been reduced by: 25"
			return self.armor
		elif self.armor < 0:
			self.health = self.health - 10
			print "Your health has been reduced by: 10"
			return self.health
		else:
			return 0;
	
	


player1 = Player()
	


while player1.health > 0:
	print "_____________________________________________________________"
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player1.health, player1.armor, player1.ammo)
	print "Main game loop running!"	
	print hud
	player1.incoming_damage()
	hud = "Your current health is: %d Your current armor is: %d and you have %d rounds \nof ammunition remaining." % (player1.health, player1.armor, player1.ammo)
	print hud
	
	print "_____________________________________________________________"
	player1.health = 0



Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7748
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: What wrong with this player class

Posted 18 August 2014 - 10:39 PM

That's a lot closer. What you're missing now is the setup part - how does a player end up with health, armor, and whatnot? What you're doing now is just attaching those to the class. You want them on the player (the class of dogs doesn't have fleas, it's Fido and Sparky that have the fleas)

So the way this is done is by writing an __init__ function. This is a function that is called when you create a new instance of an object, and a big part of its purpose is to "construct" the object, by attaching values to it.

Look at my Value example again, and you'll see a simple example. Then google it - the official python documentation has good explanations of all of this stuff, so read that over carefully.
Was This Post Helpful? 1
  • +
  • -

#15 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with this player class

Posted 19 August 2014 - 11:20 AM

The __int__ thing is not clicking for me I am doing research now watching some video's, I am unsure how it is used I think I understand why but ill have to do some reading up on it ill post the next iteration of my code should i figure something you I am grateful for the help you have given me this community is a great one.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2