-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobstacles.py
More file actions
62 lines (56 loc) · 2.71 KB
/
obstacles.py
File metadata and controls
62 lines (56 loc) · 2.71 KB
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
# Obstacles.py
# Manages the dots, or "obstacles"
# Note: the dots were initially supposed to be obstacles that decreased the player's health
import pygame, random, mechanics, screen, levels
class Obstacle():
def __init__(self, image, rect):
self.image = image
self.rect = rect
self.motion = mechanics.Linear_Motion()
self.color = screen.colors["white"]
def draw(self, time):
coord = self.motion.coordinate(time)
self.rect.left, self.rect.top = coord
pygame.draw.circle(screen.surface, self.color, coord, 3, 3)
obstacles_list = []
# Function to generate obstacles for each new level
# Based on randomness and ensures no playthrough of a level will be the exact same
# The same principle is used for the void
def generate_obstacles():
global obstacles_list
obstacles_list = []
for i in range(50):
obstacle = Obstacle(image="", rect=pygame.Rect(0, 0, 5, 5))
origin = random.randrange(0, screen.width), random.randrange(0, screen.height)
v = [random.randrange(-50, 50), random.randrange(-50, 50)]
obstacle.motion.change_motion(time=0,
origin=origin,
init_velocity_x=v[0],
init_velocity_y=v[1],
acceleration_x=0,
acceleration_y=0)
obstacles_list.append(obstacle)
def draw_obstacles(time):
for obstacle in obstacles_list:
obstacle.draw(time)
x, y = obstacle.motion.coordinate(time)
if x < 0 or x > screen.width:
obstacle.motion.change_motion(time, init_velocity_x=-obstacle.motion.init_velocity_x)
if y < 0 or y > screen.height:
obstacle.motion.change_motion(time, init_velocity_y=-obstacle.motion.init_velocity_y)
# Checks if the player collides with the dot to give the player health
# The function for collision is in Mechanics.py
def check_collide(player):
level = levels.levels[levels.level]
for obstacle in obstacles_list:
if mechanics.rect_collide(player.rect, obstacle.rect):
player.health += level.gain
if player.health > 100: player.health = 100
origin = random.randrange(0, screen.width), random.randrange(0, screen.height)
v = [random.randrange(-50, 50), random.randrange(-50, 50)]
obstacle.motion.change_motion(time=0,
origin=origin,
init_velocity_x=v[0],
init_velocity_y=v[1],
acceleration_x=0,
acceleration_y=0)