KuraiTsubasa

Members
  • Content count

    46
  • Joined

  • Last visited

Community Reputation

231 Neutral

About KuraiTsubasa

  • Rank
    Member
  1. What is enumerate?

    Ok thank you 
  2. What is enumerate?

    Hi,i just read a code and there is an enumerate function but i don't know what enumerate do?I read some question and it appear that enumerate is giving index for each loops but if i delete the enumerate it give me an error so i think there are some things that i missed. Here the error: too many values to unpack(expected 2)  And here here the code: def load_tile_table(filename, width, height): image = pygame.image.load(filename).convert() image_width, image_height = image.get_size() tile_table = [] for tile_x in range(0, image_width//width): line = [] tile_table.append(line) for tile_y in range(0, image_height//height): rect = (tile_x*width, tile_y*height, width, height) line.append(image.subsurface(rect)) return tile_table if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((128,98)) screen.fill((255, 255, 255)) table = load_tile_table('ground.png', 24, 16) for x, row in enumerate(table): for y,tile in enumerate(row): screen.blit(tile, (x*32, y*24)) pygame.display.flip() while pygame.event.wait().type != pygame.locals.QUIT: pass Thank you very much 
  3. What happen if "self" in second argument

     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 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  
  4. What happen if "self" in second argument

    i'm really Sorry for my post ^^a
  5. What happen if "self" in second argument

     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.
  6. What happen if "self" in second argument

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

    Yeah,i am pretty familiar with OOP  ,i know what self do in first argument but not in the second argument,thank you
  8. Hi i tried to read this code but i don't really understand what "self" being in second argument.Here is the example: class Player(pygame.sprite.Sprite): def update(self, dt, game): game.tilemap.set_focus(new.x, new.y) class Game(object): def initArea(self, mapFile): self.tilemap = tmx.load(mapFile, screen.get_size()) self.sprites = tmx.SpriteLayer() self.tilemap.layers.append(self.sprites) def main(self): while 1: self.tilemap.update(dt / 1000., self) if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Pyllet Town") Game(screen).main() I already tried to ask others they said game is being an instance of Game(),but i really don't get it can you explain me a bit detailed?Thank you very much  any help will be very appreciated   P.S:i cut some line but i hope you could still understand this code
  9. What is super().__init()

      For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education!   In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.   I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.         Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial     Thank you for the Tutorial actually i already do that but never finished it,but i do learn class from video tutorial and book.So I tried to using the super()__init__() and i figured it what it is for   but i still curious why we need to pass  *groups inside __init__() in my code when i play around with super i never pass anything inside __init__() because i don't know what it is for(i only know def __init__ or something to init first).This is my last question  .Thank you very much   __init__ is called a Constructor. It's a function (like any other), with a small twist. It gets called every time you create a new instance of a Class. For example, let's say we want to create a generic enemy class. We want all of the instances of our class to be the same in every regard except their health. A class like that would look something like this: class Enemy(object): def __init__(self, health): print "Inside __init__" self.health = health Every time we create a new Enemy, __init__ gets ran with whatever "health" we pass in. Let's create some Enemy objects: # You pass in what their health should be when creating them enemy_one = Enemy(10) enemy_two = Enemy(5) And here's the program output: Inside __init__ Inside __init__ When you make a new Object and pass things to it in parenthesis, you're actually passing stuff to the __init__ function.     Ok,i got it now.Thank you very much for the explanation
  10. What is super().__init()

      For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education!   In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.   I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.         Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial     Thank you for the Tutorial actually i already do that but never finished it,but i do learn class from video tutorial and book.So I tried to using the super()__init__() and i figured it what it is for   but i still curious why we need to pass  *groups inside __init__() in my code when i play around with super i never pass anything inside __init__() because i don't know what it is for(i only know def __init__ or something to init first).This is my last question  .Thank you very much
  11. What is super().__init()

    Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^       Thank you it help me understanding about class and super(There is a link in comment),but i still not get used with it,maybe playing around a little bit tomorrow will help me get used to it.     Well,i think i understand that a little.I will play around with it tomorrow so i can really understand it     I will try to play with it a bit to really understand that     Well,Thank you everyone to help me.I will try my best  
  12. What is super().__init()

    Hi,i just found a TMX library but i can't find any tutorial that explain it clearly so i tried to understand the library myself but i don't know what is super().__init__().I already tried to search about it but i don't really understand it,the explaination is to advance for me   because i'm still new in programming world so this is code: class Player(pygame.sprite.Sprite): def __init__(self, location, orientation, *groups): super(Player, self).__init__(*groups) self.image = pygame.image.load('sprites/player.png') self.imageDefault = self.image.copy() self.rect = pygame.Rect(location, (64,64)) self.orient = orientation self.holdTime = 0 self.walking = False self.dx = 0 self.step = 'rightFoot' # Set default orientation self.setSprite() Any helps will be very appreciate and please explain it using easy example  if it possible also if you need a full code i will add it since the code is too long.Thank you so much
  13. Help me learn Pygame

    It's okay also programarcadegames it is a great website for learning pygame,thanks for telling me that website and thanks for the advice i should think more deeper and tried the codes that i don't understand line by line and improving a basic codes really helping me. 
  14. Help me learn Pygame

    Thank you very much it help me a lot 
  15. Help me learn Pygame

    hello, it's been a while since I posted .i have national exam this early month but it's already over now ^^(i need to study hard to pass the exams so a few month ago i can't open my computer to long but now i have 1 month holiday).So,i think i already know the basics of the python and want to move on to game developing,i tried Invent Your Game with Python book but i feel like it is still hard to understand.So i'm looking some book that easy to understand,to read in this holiday or video/tutorial online that can improve both python and pygame.(i really need collision detection and tile map because i only want to make a simple RPG)   I already tried(Python): -New boston video -byte of python -learn Python the hard way -and some web(i forgot the web ^^a)   And for pygame i only tried Invent Your Game with Python.Sometimes i feel down because i can't understand what that code do(but i feel i can't stop trying) .If you have great tutorial to help me or advice i'm really appreciate that.Thank You