| 75 | ADD_INTERVAL = 3000 |
| 76 | |
| 77 | def __init__(self, pipe_end_img, pipe_body_img): |
| 78 | self.x = float(W_WIDTH - 1) |
| 79 | self.score_counted = False |
| 80 | |
| 81 | self.image = pygame.Surface((PipePair.WIDTH, W_HEIGHT), SRCALPHA) |
| 82 | self.image.convert() # speeds up blitting |
| 83 | self.image.fill((0, 0, 0, 0)) |
| 84 | total_pipe_body_pieces = int( |
| 85 | ( |
| 86 | W_HEIGHT |
| 87 | - 3 * Bird.HEIGHT # fill window from top to bottom |
| 88 | - 3 * PipePair.PIECE_HEIGHT # make room for bird to fit through |
| 89 | ) |
| 90 | / PipePair.PIECE_HEIGHT # 2 end pieces + 1 body piece # to get number of pipe pieces |
| 91 | ) |
| 92 | self.bottom_pieces = randint(1, total_pipe_body_pieces) |
| 93 | self.top_pieces = total_pipe_body_pieces - self.bottom_pieces |
| 94 | |
| 95 | # bottom pipe |
| 96 | for i in range(1, self.bottom_pieces + 1): |
| 97 | piece_pos = (0, W_HEIGHT - i * PipePair.PIECE_HEIGHT) |
| 98 | self.image.blit(pipe_body_img, piece_pos) |
| 99 | bottom_pipe_end_y = W_HEIGHT - self.bottom_height_px |
| 100 | bottom_end_piece_pos = (0, bottom_pipe_end_y - PipePair.PIECE_HEIGHT) |
| 101 | self.image.blit(pipe_end_img, bottom_end_piece_pos) |
| 102 | |
| 103 | # top pipe |
| 104 | for i in range(self.top_pieces): |
| 105 | self.image.blit(pipe_body_img, (0, i * PipePair.PIECE_HEIGHT)) |
| 106 | top_pipe_end_y = self.top_height_px |
| 107 | self.image.blit(pipe_end_img, (0, top_pipe_end_y)) |
| 108 | |
| 109 | # compensate for added end pieces |
| 110 | self.top_pieces += 1 |
| 111 | self.bottom_pieces += 1 |
| 112 | |
| 113 | # for collision detection |
| 114 | self.mask = pygame.mask.from_surface(self.image) |
| 115 | |
| 116 | @property |
| 117 | def top_height_px(self): |