-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.py
96 lines (63 loc) · 2.61 KB
/
collision.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
95
#collision
import pygame
from pygame.sprite import Sprite
class Block(Sprite):
def __init__(self, x, y, color, width, height, screen):
super(Block,self).__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.image_red = pygame.Surface([width, height])
self.image_red.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
self.rect = self.image.get_rect()
self.cornersList = self.find_corners()
def render(self, player):
# def wrapper():
# return area(self.cornersList)
if self.detect_collision(player):
self.screen.blit(self.image_red, [self.x, self.y])
else: self.screen.blit(self.image, [self.x, self.y])
# print '#######', self.x, self.y
# print '#######', self.x + self.width, self.y + self.height
def find_corners(self):
cornersList = []
top_left_corner = [self.x, self.y]
top_right_corner = [self.x + self.width, self.y]
bottom_left_corner = [self.x, self.y + self.height]
bottom_right_corner = [self.x + self.width, self.y + self.height]
cornersList.append(top_left_corner)
cornersList.append(top_right_corner)
cornersList.append(bottom_right_corner)
cornersList.append(bottom_left_corner)
return cornersList
def it_within_my_area(self, player):
for corner in player.cornersList:
if self.point_within_my_area(corner, self.cornersList):
return True
return False
def me_within_its_area(self, player):
for corner in self.cornersList:
if player.point_within_my_area(corner, player.cornersList):
return True
return False
def detect_collision(self, player):
return self.it_within_my_area(player) or self.me_within_its_area(player)
def point_within_my_area(self, point_coordinates, my_corners_list):
#x1,y1###############x2,y2#
# #
# #
#x4,y4###############x3,y3#
if not my_corners_list:
my_corners_list = self.cornersList
x1 = self.cornersList[0][0]
y1 = self.cornersList[0][1]
x3 = self.cornersList[2][0]
y3 = self.cornersList[2][1]
p_x = point_coordinates[0]
p_y = point_coordinates[1]
return p_x >= x1 and p_x <= x3 and p_y >= y1 and p_y <= y3