![]() This function uses the cosine function to achieve a smooth jump: """Calculate how high the bird should jump in a particular frame. # converting all images before use speeds up blitting '.png') of the required image, without a file path.įile_name = os.path.join('.', 'images', img_file_name) Img_file_name: The file name (including its extension, e.g. All images are converted before being returned to This function looks for images in the game's images folder """Return the loaded pygame image with the specified file name. Pipe-body: An image of a slice of a pipe's body. Pipe-end: An image of a pipe's end piece (the slightly wider bit). Use this and bird-wingup to create a flapping bird. Use this and bird-wingdown to create a flapping bird.īird-wingdown: An image of the bird with its wing pointing downward. The returned dict has the following keys:īird-wingup: An image of the bird with its wing pointing upward. """Load all images required by the game and return a dict of them. In_x_range = bx + BIRD_WIDTH > self.x and bx WIN_HEIGHT - self.bottom_height_px) """Get whether the bird crashed into a pipe in this PipePair.īird_position: The bird's position on screen, as a tuple in Return self.bottom_pieces * PIPE_PIECE_HEIGHTĭef is_bird_collision(self, bird_position): """Get the bottom pipe's height, in pixels.""" Return self.top_pieces * bottom_height_px(self): """Get the top pipe's height, in pixels.""" ![]() Self.score_counted = top_height_px(self): Top_pieces: The number of pieces, including the end piece, whichīottom_pieces: The number of pieces, including the end piece, Surface: A pygame.Surface which can be blitted to the main The new PipePair will automatically be assigned an x attribute of """Initialises a new PipePair with the given arguments. Top_pieces: The number of pieces, including the end piece, in theīottom_pieces: The number of pieces, including the end piece, inĭef _init_(self, surface, top_pieces, bottom_pieces): Surface: A pygame.Surface which can be blitted to the main surface The bird pass - if it collides with either part, the game is over. PIPE_PIECE_HEIGHT = BIRD_WIDTH = BIRD_HEIGHT = 32Ī PipePair has a top and a bottom pipe, and only between them can WIN_WIDTH = 284 * 2 # BG image size: 284x512 px tiled twice """Flappy Bird, implemented using Pygame."""ĮVENT_NEWPIPE = USEREVENT + 1 # custom eventįRAME_ANIMATION_WIDTH = 3 # pixels per frameįRAME_BIRD_DROP_HEIGHT = 3 # pixels per frameįRAME_BIRD_JUMP_HEIGHT = 5 # pixels per frameīIRD_JUMP_STEPS = 20 # see get_frame_jump_height docstring ![]() Github repo, with images #!/usr/bin/env python3 What do you think? Could this be made simpler/shorter? Is there anything in there that I shouldn't teach my students? To try out Pygame (I've never used it before) and to see exactly how easy it is to make a game, I made a clone of Flappy Bird. It's voluntary, so I thought I'd make it interesting by teaching Python programming and then introduce the kids to Pygame, so that they can make their own games. I'll soon begin teaching a beginners' programming class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |