-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.py
82 lines (71 loc) · 2.9 KB
/
obstacle.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
# File: obstacle.py
import pygame;
import os;
import random;
from config import *;
class Obstacle:
TYPE_WEATHER = 0
TYPE_MOUNTAIN = 1
def __init__(self, obs_type, location):
self.location = location
self.type = obs_type
self.colliding = []
if(self.type == Obstacle.TYPE_WEATHER):
self.image = pygame.image.load(os.path.join('data', 'obs_weather.png'))
elif(self.type == Obstacle.TYPE_MOUNTAIN):
self.image = pygame.image.load(os.path.join('data', 'obs_mountain.png'))
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.topleft = self.location
def getType(self):
return self.type
def setLocation(self, location):
self.location = location
self.rect.topleft = self.location
def draw(self, surface):
surface.blit(self.image, self.rect)
def collideAircraft(self, aircraft):
newCollides = 0
for a in aircraft:
currCollide = self.isColliding(a.getLocation())
if(a in self.colliding):
prevCollide = True
else:
prevCollide = False
if(currCollide == True and prevCollide == False):
self.colliding.append(a)
newCollides += 1
elif(currCollide == False and prevCollide == True):
self.colliding.remove(a)
return newCollides
def isColliding(self, point):
collide = False
#Don't bother with the masky stuff if the ac is outside rect
if(self.rect.collidepoint(point) == True):
acLocOffsetX = int(point[0] - self.rect.left)
acLocOffsetY = int(point[1] - self.rect.top)
if(self.mask.get_at((acLocOffsetX, acLocOffsetY)) != 0):
collide = True
return collide
@staticmethod
def generateGameObstacles(screen_w, screen_h, destinations):
ret = []
x = 0
while x < Config.NUMBEROFOBSTACLES:
randtype = random.randint(0, 1)
randx = random.randint( 40, screen_w - 100 )
randy = random.randint( 40, screen_h - 80 )
obstacle = Obstacle(randtype, (randx, randy))
collide = False
for d in destinations:
collide |= obstacle.isColliding(d.getLocation())
while(collide == True):
randx = random.randint( 40, screen_w - 100 )
randy = random.randint( 40, screen_h - 80 )
obstacle.setLocation((randx, randy))
collide = False
for d in destinations:
collide |= obstacle.isColliding(d.getLocation())
ret.append(obstacle)
x += 1
return ret