-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircleshape.py
41 lines (34 loc) · 1.4 KB
/
circleshape.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
import pygame
from constants import *
# Base class for game objects
class CircleShape(pygame.sprite.Sprite):
def __init__(self, x, y, radius):
if hasattr(self, "containers"):
super().__init__(self.containers)
else:
super().__init__()
self.position = pygame.Vector2(x, y)
self.velocity = pygame.Vector2(0, 0)
self.radius = radius
def draw(self, screen):
# must override
pass
def update(self, dt):
# must override
pass
def collision(self, other):
return self.position.distance_to(other.position) <= self.radius + other.radius
def wrap_position(self):
# Keeps objects within screen bounds by wrapping around edges
# When object moves off one edge, it appears on the opposite side
# Adds/subtracts screen dimensions based on position and radius
# Wrap horizontal position
if self.position.x < -self.radius:
self.position.x = SCREEN_WIDTH + self.radius
elif self.position.x > SCREEN_WIDTH + self.radius:
self.position.x = -self.radius
# Wrap vertical position
if self.position.y < -self.radius:
self.position.y = SCREEN_HEIGHT + self.radius
elif self.position.y > SCREEN_HEIGHT + self.radius:
self.position.y = -self.radius