0% found this document useful (0 votes)
15 views6 pages

Wasif CG Part4 PipeGeneration

Uploaded by

wasif khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Wasif CG Part4 PipeGeneration

Uploaded by

wasif khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

 The clock object is used to control the frame rate of the game, which is set to

60 frames per second (FPS).

Game Variables

ground_scroll: The current scroll position of the ground.


scroll_speed: The speed at which the ground scrolls.
flying: A boolean indicating whether the bird is flying or not.
game_over: A boolean indicating whether the game is over or not.
pipe_gap: The gap between the top and bottom pipes.
pipe_frequency: The frequency at which new pipes are generated (in
milliseconds).
 The Pipe class represents a pipe in the game. It inherits from
pygame.sprite.Sprite.
 The __init__ method initializes the pipe's properties:
 image: The image of the pipe.
 rect: The rectangle representing the pipe's position and size.
 The update method updates the pipe's position and removes it if it goes off
the screen.
Condition For Generating Pipe

 code checks two conditions before generating new pipes:

 game_over == False: This ensures that new pipes are only generated when
the game is not over.
 flying == True: This ensures that new pipes are only generated when the bird
is flying.

Getting the Current Time


 The code uses the pygame.time.get_ticks() function to get the current time in
milliseconds. This function returns the number of milliseconds since
pygame.init() was called.

Checking the Pipe Frequency

 The code checks if the time elapsed since the last pipe was generated is
greater than the pipe_frequency. This is done by subtracting the last_pipe
time from the current time.

 time_now - last_pipe > pipe_frequency: This checks if the time elapsed


since the last pipe was generated is greater than the pipe_frequency.

Generating New Pipes


 If the time elapsed since the last pipe was generated is greater than the
pipe_frequency, the code generates new pipes. Here's what happens:
 pipe_height = random.randint(-100, 100): This generates a random height
for the pipe. The height is a random integer between -100 and 100.
 btm_pipe = Pipe(screen_width, int(screen_height / 2) + pipe_height, -1):
This creates a new bottom pipe at the right edge of the screen. The pipe's
height is determined by the pipe_height variable.
 top_pipe = Pipe(screen_width, int(screen_height / 2) + pipe_height, 1): This
creates a new top pipe at the right edge of the screen. The pipe's height is
determined by the pipe_height variable.
 pipe_group.add(btm_pipe): This adds the bottom pipe to the pipe_group
sprite group.
 pipe_group.add(top_pipe): This adds the top pipe to the pipe_group sprite
group.
 last_pipe = time_now: This updates the last_pipe variable to the current
time. This ensures that the next pipe is generated after the pipe_frequency
has elapsed.

Pipe Group
 The pipe_group sprite group is used to manage all the pipes in the game. By
adding pipes to this group, the game can easily update and draw all the pipes
at once.
 This is the update method of the Pipe class. It's responsible for updating the
position of the pipe and removing it from the game when it goes off the
screen. Here's a breakdown of what the code does:

Updating the Pipe's Position

 self.rect.x -= scroll_speed: This line of code updates the x-coordinate of the


pipe's rectangle (self.rect) by subtracting the scroll_speed from it. This
effectively moves the pipe to the left by the scroll_speed amount.
 Checking if the Pipe is Off the Screen

 if self.rect.right < 0: This line of code checks if the right edge of the pipe's
rectangle (self.rect.right) is less than 0. This means that the pipe has moved
off the left edge of the screen.

Removing the Pipe from the Game

 self.kill(): If the pipe is off the screen, this line of code removes it from the
game by calling the kill method. This method is a part of the
pygame.sprite.Sprite class, which the Pipe class inherits from.
 When a pipe is removed from the game, it's no longer updated or drawn, and
it's not considered for collision detection. This helps to improve performance
by reducing the number of objects that need to be processed.
Why is this necessary?

This code is necessary because pipes are constantly being generated and moved
across the screen. If pipes were not removed from the game when they went off the
screen, they would continue to exist and consume system resources, even though
they're no longer visible. By removing pipes from the game when they're no longer
needed, we can help to prevent memory leaks and improve overall performance.

How does this relate to the game loop?

 The update method of the Pipe class is called repeatedly in the game loop,
which is the main loop of the game that updates and draws all the game
objects. The game loop is responsible for calling the update method of all
the pipes in the game, which in turn updates their positions and removes
them from the game when they're no longer needed.

 This line checks if the bird has:

 Hit a pipe (collision with pipe_group)


 Flown off the top of the screen (flappy.rect.top < 0)
 If either of these conditions is true, the game ends (game_over = True).

You might also like