Jump to content

I am beginning to get into pygame a little bit which really makes good use of classes. I notice sometimes the class contains variables which I assume grants the user the ability to pass variables. 

 

class Ball(pygame.sprite.Sprite):

 

other times, they leave it blank and make an entire game based on that...

 

class Ball()

 

I am not understanding which situations you need to be able to pass variables from a class. In the simple game I'm creating, I defined variables in a class called Player...in order to access those variables, I refer to it in a move function by using Player.x, Player.y, etc. I assume that in the first example, we need to be able to pass pygame.sprite.sprite so we can update the sprite groups?

Link to comment
https://linustechtips.com/topic/1210292-another-stupid-python-question/
Share on other sites

Link to post
Share on other sites

Most object oriented languages allow you to make a class that inherits the characteristics of another class. This is intuitively called inheritance. In python the syntax for inheriting from another class is

class HeirClass(AncestorClass):
	...

In the first example, the new class Ball is inheriting the features of the class pygame.sprite.Sprite whereas in the second example it's not inheriting anything - depending on what you want to do it may or may not make sense to use inheritance.

 

Here's a small tutorial: https://www.w3schools.com/python/python_inheritance.asp

 

This is an intermediate topic so I wouldn't recommend you start from here as a beginner. Suffice it to say that in pygame you should inherit from a Sprite class if you want your object to be a sprite.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

4 hours ago, Sauron said:

Most object oriented languages allow you to make a class that inherits the characteristics of another class. This is intuitively called inheritance. In python the syntax for inheriting from another class is


class HeirClass(AncestorClass):
	...

In the first example, the new class Ball is inheriting the features of the class pygame.sprite.Sprite whereas in the second example it's not inheriting anything - depending on what you want to do it may or may not make sense to use inheritance.

 

Here's a small tutorial: https://www.w3schools.com/python/python_inheritance.asp

 

This is an intermediate topic so I wouldn't recommend you start from here as a beginner. Suffice it to say that in pygame you should inherit from a Sprite class if you want your object to be a sprite.

That makes sense. So, if Im understanding this correctly say I have two classes - Name() and Address() and I for whatever reason need to share a variable called 'fname' between them...both classes need to have 'name' in parenthesis. within the class I would refer to it as self.fname, outside of the Name class, I would call it by Name.fname.

 

This is a very confusing to me as a newbie, I've seen examples where a person defines a function within a class and calls a variable using classname.variablename, other times they refer to it using self.variablename...maybe I missed that the variable was being passed between the class and the function.

Link to post
Share on other sites

You typically don't use inheritance just to share a variable. You use it to express an "is-a" relationship between classes.

 

For example you could have a class called "Animal" that contains some typical properties of an animal. Than you could have a class called "Cat" that inherits from Animal and adds additional properties specific to cats. A Cat "is-a" Animal.

 

In your example above you have a Ball which is a Sprite. This makes sense, since a sprite is typically something that has a representation on screen and you want to be able to see a ball. However a sprite is just some generic (or "abstract") representation of something that can be shown on screen. So it might have properties like a position. But it doesn't actually have a graphic associated with it. A ball is a specialization of a sprite that not only has a position but you also know what it looks like.

 

For example your Sprite could have an abstract method called "draw" that can be used to draw it on screen. This method is abstract meaning it has no implementation, because a Sprite is just a concept not an actual implementation. A Ball on the other hand is a concrete implementation of a Sprite, so its draw method defines how to draw a ball.

 

The point of that is that whenever you have an instance of a class in hand that inherits from Sprite, you know you can draw it, because all of them have a "draw" method. So you could write another method that takes a list of sprites which loops over them to draw all of them on screen. You don't need to know whether what you have in hand is a Ball, Player or a Goal, etc.. You know it inherits from Sprite, so you know it has a draw method you can use to draw it and a position that tells you where to place it.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

8 hours ago, steelo said:

Name() and Address() and I for whatever reason need to share a variable called 'fname' between them...both classes need to have 'name' in parenthesis.

Not really, that's what a function parameter would do. If class B inherits from class A then all internal variables and methods from class A are available in class B but not vice versa.

8 hours ago, steelo said:

This is a very confusing to me as a newbie

That's just OOP for you :P

8 hours ago, steelo said:

I've seen examples where a person defines a function within a class and calls a variable using classname.variablename, other times they refer to it using self.variablename...maybe I missed that the variable was being passed between the class and the function.

self is a keyword that usually refers to the object that's running the code at that time. If an object needs to address itself inside one of its methods it will use self. If you're accessing something within that object from outside the object then you're going to use the name of the object and not self. This is not part of inheritance.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

1 hour ago, Sauron said:

Not really, that's what a function parameter would do. If class B inherits from class A then all internal variables and methods from class A are available in class B but not vice versa.

That's just OOP for you :P

self is a keyword that usually refers to the object that's running the code at that time. If an object needs to address itself inside one of its methods it will use self. If you're accessing something within that object from outside the object then you're going to use the name of the object and not self. This is not part of inheritance.

Ah, okay...it threw me off when they sometimes used self. and other times .class to reference a variable from within a method (even though the method was inside of the class. Thanks for the insight!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×