-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoadBlock.py
94 lines (68 loc) · 3.12 KB
/
RoadBlock.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import pygame
import os
import random
import math
import Car
MAX_BIAS = 180
class RoadBlock:
MAX_ROT = 45
GAP = 200
COMPELLED_TURN = 0
Len = 300
IMG = pygame.transform.scale(pygame.image.load(os.path.join("images", "road.png")), (25, 300))
def __init__(self, start_x, start_y):
if self.COMPELLED_TURN == 1:
self.COMPELLED_TURN = 0
self.bias = random.randint(int(MAX_BIAS/2), MAX_BIAS)
elif self.COMPELLED_TURN == -1:
self.COMPELLED_TURN = 0
self.bias = random.randint(-MAX_BIAS, int(-MAX_BIAS/2))
elif random.randint(0, 1) == 0:
self.bias = random.randint(int(MAX_BIAS/2), MAX_BIAS)
else:
self.bias = random.randint(-MAX_BIAS, int(-MAX_BIAS/2))
self.angle = math.degrees(math.asin(self.bias/self.Len))
self.image = pygame.transform.rotozoom(self.IMG, self.angle, 1)
self.x = start_x
self.y = start_y
self.x2 = self.x - self.bias
self.y2 = self.y - self.Len
self.l_center = self.image.get_rect(
center=self.IMG.get_rect(topleft=(self.x - self.bias / 2, self.y)).center).center
self.r_center = self.image.get_rect(center=self.IMG.get_rect(topleft=
(self.x - self.bias / 2 + self.GAP + math.cos(math.radians(self.angle)) * 50, self.y)).center).center
self.passed = False
def get_rect_l(self):
return self.image.get_rect(
center=self.IMG.get_rect(topleft=(self.x - self.bias / 2, self.y)).center).topleft
def get_rect_r(self):
return self.image.get_rect(center=self.IMG.get_rect(topleft=(
self.x - self.bias / 2 + self.GAP + math.cos(math.radians(self.angle)) * 50, self.y)).center).topleft
def draw(self, win):
rect_l = self.get_rect_l()
rect_r = self.get_rect_r()
pygame.draw.line(win, (255, 0, 0), (self.x, self.y), (self.x2, self.y2))
pygame.draw.line(win, (0, 255, 0), (self.x + self.GAP, self.y), (self.x2 + self.GAP, self.y2))
def move(self):
self.y += Car.VEL
self.y2 += Car.VEL
def collide(self, car):
Vec_1 = pygame.math.Vector2(self.x2 - self.x, self.y2 - self.y)
Vec_2 = pygame.math.Vector2(car.center[0] - self.x, car.center[1] - self.y)
Vec_3 = pygame.math.Vector2(self.x2 - self.x, self.y2 - self.y)
Vec_4 = pygame.math.Vector2(car.center[0] - (self.x + self.GAP), car.center[1] - self.y)
Cross = Vec_1.cross(Vec_2)
Cross_2 = Vec_3.cross(Vec_4)
if Cross < 0 or Cross_2 > 0:
return True
return False
def get_distances(self, car):
rect_l = self.get_rect_l()
rect_r = self.get_rect_r()
stick_mask = pygame.mask.from_surface(Car.STICK)
curb_mask = pygame.mask.from_surface(self.image)
left_offset = (int(rect_l[0] - car.x), rect_l[1] - car.y)
right_offset = (int(rect_r[0] - car.x), rect_r[1] - car.y)
left_point = stick_mask.overlap_area(curb_mask, left_offset)
right_point = stick_mask.overlap_area(curb_mask, right_offset)
return left_point, right_point