-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.py
75 lines (61 loc) · 1.88 KB
/
pipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pygame
import os
import random
gap = 200
vel = 5
class Pipe():
"""
represents a pipe object
"""
def __init__(self, x):
"""
initialize pipe object
:param x: int
"""
self.x = x
self.height = 0
self.top = 0
self.bottom = 0
pipe_img = pygame.transform.scale2x(pygame.image.load(os.path.join("assets","pipe.png")).convert_alpha())
self.pipe_top = pygame.transform.flip(pipe_img, False, True)
self.pipe_bottom = pipe_img
self.passed = False
self.set_height()
def set_height(self):
"""
set the height of the pipe, from the top of the screen
:return: None
"""
self.height = random.randrange(50, 450)
self.top = self.height - self.pipe_top.get_height()
self.bottom = self.height + gap
def move(self):
"""
move pipe based on vel
:return: None
"""
self.x -= vel
def draw(self, win):
"""
draw both the top and bottom of the pipe
:param win: pygame window/surface
:return: None
"""
win.blit(self.pipe_top, (self.x, self.top))
win.blit(self.pipe_bottom, (self.x, self.bottom))
def collide(self, bird, win):
"""
returns if a point is colliding with the pipe
:param bird: Bird object
:return: Bool
"""
bird_mask = bird.get_mask()
top_mask = pygame.mask.from_surface(self.pipe_top)
bottom_mask = pygame.mask.from_surface(self.pipe_bottom)
top_offset = (self.x - bird.x, self.top - round(bird.y))
bottom_offset = (self.x - bird.x, self.bottom - round(bird.y))
b_point = bird_mask.overlap(bottom_mask, bottom_offset)
t_point = bird_mask.overlap(top_mask,top_offset)
if b_point or t_point:
return True
return False