-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.py
54 lines (41 loc) · 1.93 KB
/
wall.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
import pygame
import random
height = 500
shift = 5
default_hole_size = 210
class Wall:
windows_size = ()
number_of_walls = 0
@classmethod
def set_window_size(cls, size):
cls.windows_size = size
def __init__(self, img_path="assets/pipe.png",width = 80):
Wall.number_of_walls += 1
hole_size = random.randint(default_hole_size - min(60,40+ Wall.number_of_walls), default_hole_size + max(-40,60- Wall.number_of_walls))
self.position = Wall.windows_size[0]
self.img = pygame.image.load(img_path)
self.img = pygame.transform.scale(self.img, (width, height))
self.width = width
self.hole_start = random.randint(70, Wall.windows_size[1] - hole_size - 70)
self.hole_end = self.hole_start + hole_size
def update(self, game_speed, player_position) -> bool:
old_position = self.position
self.position = self.position - shift * game_speed
return old_position > player_position[0] >= self.position
def draw(self, screen):
top = pygame.transform.flip(self.img, False, True)
bottom = self.img
bottom = bottom.subsurface((0, 0, self.width, self.__class__.windows_size[1] - self.hole_end))
screen.blit(top, (self.position, -height + self.hole_start))
screen.blit(bottom, (self.position, self.hole_end))
def get_position(self):
return self.position
def collide(self, position: tuple[float, float], size: (float, float)) -> bool:
if position[0] + size[0] / 2 < self.position or position[0] - size[0] / 2 > self.position + self.width:
return False
if position[1] - size[1] / 2 < self.hole_start or position[1] + size[1] / 2 > self.hole_end:
return True
return False
def in_wall(self, position, player_width) -> bool:
# start at position-width/2 and ends at position+width/2
return self.position <= position <= self.position + self.width