-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (26 loc) · 1.05 KB
/
utils.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
from pygame.image import load
from pygame.math import Vector2
import random
def load_sprite(name, with_alpha=True):
path = f"assets/sprites/{name}.png"
loaded_sprite = load(path)
if with_alpha:
return loaded_sprite.convert_alpha()
else:
return loaded_sprite.convert()
def wrap_position(position, surface):
x, y = position
w, h = surface.get_size()
return Vector2(x % w, y % h)
def get_random_position(surface):
# This will generate a random set of coordinates on a given surface and return the result as a Vector2 instance.
return Vector2(
random.randrange(surface.get_width()),
random.randrange(surface.get_height()),
)
def get_random_velocity(min_speed, max_speed):
# The method will generate a random value between min_speed and max_speed and a random angle between 0 and 360
# degrees. Then it will create a vector with that value, rotated by that angle.
speed = random.randint(min_speed, max_speed)
angle = random.randrange(0, 360)
return Vector2(speed, 0).rotate(angle)