Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


Kurai Tsubasa

Member Since 22 Aug 2013
Offline Last Active Jul 21 2015 01:44 AM

Posts I've Made

In Topic: What is enumerate?

29 April 2015 - 06:30 AM

You need to learn how to look for information on the web yourself. I know very little about Python, but it took me about 10 seconds to find the answer to your question: https://docs.python.org/2/library/functions.html#enumerate

Ok thank you smile.png


In Topic: What happen if "self" in second argument

22 September 2014 - 04:17 AM

nothing to be sorry about, just constructive criticism.

 

 

 

 

Yeah,i am pretty familiar with OOP smile.png ,i know what self do in first argument but not in the second argument,thank you

 Oh sorry, your first post was hard to understand.







self.tilemap.update(dt / 1000., self)

tilemap.update() takes 3 arguments, self(i.e tilemap), dt, and game. Second self indicates to 'game' instance, which tilemap is a member of. And since we're in scope of game class. we pass it as 'self'.

 

 

Sorry for my post ^^a.Now i don't understand how game can be an instance mellow.png .Thank you

 

P.S: i only know how to make instance like this: a=classname() ^^a.

 

 

Let us walk through this code:

First off is



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Pyllet Town")
    Game(screen).main()

I think everything is quite straightforward except for the last line:

With Game(screen) you create an instance, but then it gets funny, because on that red hot instance a function is called (with .main()). Notice, that we do not take a reference to this new instance (we have no assignment). That means that the instance will be unreachable as soon as the function returns, but since the whole game runs in the main() method we are comfortable with that.

So now we look at the main() method:



def main(self):
    while 1: 
        self.tilemap.update(dt / 1000., self)

This is where things begin to get python and your confusion probably stems from:

From the Signature ( main(self) ) we can see, that we get passed one argument, the reference to the instance which this function is called on. This instance is of course the instance created with Game(screen). The tricky part is, that python passes this argument implicitly for every member method, which is why you don't see it in the call ( .main() ).

The next line is just the obvious main loop almost all games run in.

But then we get another strange line, which is easiest to understand when dissected:

self still is a reference to the instance of Game, so we are looking up it's tilemap (a reference to a tilemap instance) and call the function update on that tilemap instance.

This tilemap takes two arguments: The time that has passed since the last frame as well as the Game instance currently running the main funcion (probably to do some draw calls on that game object).

The update method itself will look like this:



def update(self, time_elapsed, game):
    #do something

What is important to understand is, that the self of the update function is a reference to the tilemap instance while the game in the update function is a reference to the game instance (we just handed this reference over by putting the self reference of the main function into this argument position).

 

I hope this helps, scoping and lifetimes can be quite confusing.

Wow,you explain it clearly.Thank you very much for your answer it really great explanation laugh.png 


In Topic: What happen if "self" in second argument

21 September 2014 - 04:56 AM

you beat me to the post.

@Kurai: this is the correct solution, next time you could specifically point out the line you are having trouble with (I only now saw it)

i'm really Sorry for my post ^^a


In Topic: What happen if "self" in second argument

21 September 2014 - 04:55 AM

 

Yeah,i am pretty familiar with OOP smile.png ,i know what self do in first argument but not in the second argument,thank you

 Oh sorry, your first post was hard to understand.





self.tilemap.update(dt / 1000., self)

tilemap.update() takes 3 arguments, self(i.e tilemap), dt, and game. Second self indicates to 'game' instance, which tilemap is a member of. And since we're in scope of game class. we pass it as 'self'.

 

 

Sorry for my post ^^a.Now i don't understand how game can be an instance mellow.png .Thank you

 

P.S: i only know how to make instance like this: a=classname() ^^a.


In Topic: What happen if "self" in second argument

21 September 2014 - 04:37 AM

In order for python to know which object you refer to, you use self

 

What you mean is self referring to the object Game(screen).main().I'm pretty confused with self in second argument ^^a


PARTNERS