nothing to be sorry about, just constructive criticism.
Yeah,i am pretty familiar with OOP ,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 .Thank you
P.S: i only know how to make instance like this: a=classname() ^^a.
Let us walk through this code:
First off isif __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