Jump to content
  • Advertisement

Hraefn

Member
  • Content Count

    46
  • Joined

  • Last visited

Everything posted by Hraefn

  1. Hraefn

    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 
  2. Hraefn

    What is enumerate?

    Ok thank you 
  3.  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. 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
  5. i'm really Sorry for my post ^^a
  6.  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.
  7.   What you mean is self referring to the object Game(screen).main().I'm pretty confused with self in second argument ^^a
  8. Yeah,i am pretty familiar with OOP  ,i know what self do in first argument but not in the second argument,thank you
  9. 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
  10. Hraefn

    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
  11. Hraefn

    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
  12. Hraefn

    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  
  13. Hraefn

    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. 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
  15. Hraefn

    Help me learn Pygame

    Thank you very much it help me a lot 
  16. Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this: class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print line happy_bday = Song(["Happy birthday to you", "I don't want to get sued", "So I'll stop right there"]) bulls_on_parade = Song(["They rally around the family", "With pockets full of shell"]) happy_bday.sing_me_a_song() bulls_on_parade.sing_me_a_song() And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what : self.lyrics = lyrics and  for line in self.lyric: print line is do for .Sorry for asking to much
  17. Hraefn

    OOP is so confusing[wrong question]

    Yeah i still beginner and i think i'm trying to understand that i haven't ready yet.i guess i will try to find some free books that is easy to learn for beginner^^
  18. Hraefn

    OOP is so confusing[wrong question]

      This problem here is nothing really to do with object oriented programming. Keywords like 'for' and 'in' are fundamental to python (and a lot of other programming languages -- object oriented or not) so I would suggest that you take I look a 'getting started' tutorial for python.   -Josh             oh,sorry i think i misunderstanding this,i was thinking this is OOP.Sorry ^^a
  19. Hi everyone  i tried to make item in adventure text based game but it seems not working i make the variable for key so when the variable go to number 1 it would open the door but when i tried it.It show me an error : Name:Global name key_work_room is not defined   here my script(fixed for global variable): from sys import exit global key_work_room key_work_room = 0 def work_room(): global key_work_room if key_work_room == 1: print "There is a book." print"""Choices: 1.Read 2.Leave """ next = raw_input(">") if next == "1": print """ Go to the Dark corridor and there is a door that keep a magic sword.It can used to kill demon in that door there is a key 541 """ elif next== "2": stair() else: print "Check your spelling." print " " work_room() else: print "Check your spelling." print " " work_room() def bed_room(): print "there is a key in the desk." print """choices: 1.Take. 2.leave. """ next = raw_input("> ") if next == "1": global key_work_room key_work_room += 1 stair() elif next == "2": stair() else: print "Check your spelling." print " " bed_room() def second_floor(): print "There is so many torch in this room but there is a dark side." print """Choices: 1.Take the torch. 2.Go to the dark side. 3.back """ next = raw_input("> ") if next == "1": print "You get the torch" torch = 1 second_floor() elif next == "2": print "Something lurking in the dark attack you..." elif next == "3": stair() else: print "Check your spelling." print " " second_floor() def stair(): print key_work_room print "Now you're in the 2nd floor." print "There are 4 room." print "which one do you choose? or Do you want take a look around?" print "1. to look door 1." print "2. to look door 2." print "3. to look door 3." print "4. to look door 4." print "5. to look around." next = raw_input("> ") if next == "1": king_throne() elif next == "2": bed_room() elif next == "3": veranda() elif next == "4": work_room() elif next == "5": second_floor() elif next == "exit": exit(0) else: print "Check your spelling." print " " stair() def living_room(): print "you find yourself in living room." print "You don't remember anything it looks like something hit your head." print "You now fully regain your consciousness." print "You start to walking and you found a stair and dark corridor." next = raw_input("> ") if next == "stair": stair() elif next == "dark corridor": dark_corridor elif next == "exit": exit(0) else: print "Check your spelling." print " " living_room() def start(): print "Welcome in Fantasy World." print "This is simple game type 'exit' to exit." print "choose your destination by type the destination name." print "Are you boy or girl?" character = raw_input("> ") if character == "boy" in character or "girl" in character: living_room() elif character == "exit": exit(0) else: print "Check your spelling." print " " start() def dead(): print "you died and the mystery still uncovered." print """Choices: 1.Play again 2.Take a rest. """ choice = raw_input("> ") if choice == "1": start() elif choice == "2": exit(0) else: print "Check your spelling." print " " dead() start() it haven't finished yet but i need key to know how make the variable for item work also if you find anything weird in my script please tell me .Thank you
  20. I already reach the exercise 35 learnpythonthehardway and i think it is really fun making an adventure text game.But i got some error after playing it.I tried to figure it out but i thing there is nothing wrong :   File "C:\Documents and Settings\User\My Documents\Notepad++\Project\ex35.py", line 76, in <module> start() File "C:\Documents and Settings\User\My Documents\Notepad++\Project\ex35.py", line 69, in start bear_room() File "C:\Documents and Settings\User\My Documents\Notepad++\Project\ex35.py", line 37, in bear_room gold_room() File "C:\Documents and Settings\User\My Documents\Notepad++\Project\ex35.py", line 16, in gold_room dead("You good greedy bastard!") File "C:\Documents and Settings\User\My Documents\Notepad++\Project\ex35.py", line 59, in dead exit(0) SystemExit: 0   That is a bunch of error i wonder what is wrong with my script.I can run it perfectly fine.   here my script:   from sys import exit def gold_room(): print "This is full of gold. How much do you take?" next = raw_input("> ") if "0" in next or "1" in next: how_much = int(next) else: dead("man,learn how to type a number.") if how_much < 50: print "nice you're good greedy,you win!" exit(0) else: dead("You good greedy bastard!") def bear_room(): print "There is a bear here." print "The bear has a bunch of honey." print "The fat bear is in front of another door." print "How are you going to move the bear?" bear_moved = False while True: next = raw_input("> ") if next == "take honey": dead("The bear looks at you then slaps your face off.") elif next == "taunt bear" and not bear_moved: print "The bear has moved from door." bear_moved = True elif next == "Taunt bear" and bear_moved: dead("The bear gets pissed off and chews your leg off.") elif next == "Open door" and bear_moved: gold_room() else: print "I got no idea what that means." def cthulhu_room(): print "Here you see the great evil Cthulhu." print "He, it, whatever stares at you and you go insane." print "Do you flee for your life or eat your head?" next = raw_input("> ") if "flee" in text: start() elif"head" in next: dead("Well that was tasty!") else: cthulhu_room() def dead(why): print why,"Good job!" exit(0) def start(): print "You are in a dark room." print "there is a door to your right and left." print "Which one do you take?" next = raw_input("> ") if next == "left": bear_room() elif next == "right": cthulhu_room() else: dead("You stumble around the room until you starve.") start()   And there is something i want to ask,what is int() do? and why the code is written from backward but when i tried put the def start(),def cthulhu_room,def bear_room,def dead,and def gold room it still work.Are def can be place anywhere so it doesn't matter where you put it,if it in ends or first or it may make some bugs that i didn't notice? Thank you so much your answer is really appreciated it.
  21. Exercise 35 from learnpythonthehardway (i forget to write it ^^a)but thank you for your suggestion 
  22. Hraefn

    Exercise 23 Zed Shaw

    Thank you so much 
  23. Hello everyone  i just continue my study for python after take a break from my final exams for this year and i really surprised in Zed Shaw tutorial because in tutorial 22 and 23 it is like exam(another exam after take a break) i just finished writing the name and character that used in python from ex1-22 but in exercise 23 he want me to read a python code and i searched it but what i found is really extreme hard code  so if anyone want to give me some code or some link that will show me simple code i will appreciate it .   Also it is normal to feel like this code is really hard and sometimes i forget the symbol or function and even some code from zhed shaw i can't understand it but i can do it(because i just see it and write it but don't understand the process ). TT_TT   By the way thank you for your help i will really appreciate it.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!