-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpygame_minimal_sprite_bounce_and_inflate.py
66 lines (55 loc) · 2.64 KB
/
pygame_minimal_sprite_bounce_and_inflate.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
# I can't get the object to bounce off the walls normally due to its resizing in pygam
# https://stackoverflow.com/questions/77770306/i-cant-get-the-object-to-bounce-off-the-walls-normally-due-to-its-resizing-in-p/77770428#77770428
#
# GitHub - PyGameExamplesAndAnswers - Collision and Intersection - Collide with frame, window border and restrict to rectangle
# https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_collision_and_intesection.md
import pygame
import random
class Ball(pygame.sprite.Sprite):
def __init__(self, startpos, velocity, startdir):
super().__init__()
self.pos = pygame.math.Vector2(startpos)
self.velocity = velocity
self.dir = pygame.math.Vector2(startdir).normalize()
self.original_image = pygame.Surface((100, 100), pygame.SRCALPHA)
pygame.draw.circle(self.original_image, (0, 255, 0), (50, 50), 50)
self.radius = 5
self.image = pygame.transform.scale(self.original_image, (self.radius * 2, self.radius * 2))
self.rect = self.image.get_rect(center = (round(self.pos.x), round(self.pos.y)))
def reflect(self, NV, border_rect):
self.dir = self.dir.reflect(pygame.math.Vector2(NV))
self.radius = min(50, self.radius + 1)
self.image = pygame.transform.scale(self.original_image, (self.radius * 2, self.radius * 2))
self.rect = self.image.get_rect(center = (round(self.pos.x), round(self.pos.y)))
self.rect.clamp_ip(border_rect)
self.pos = self.rect.center
def update(self, border_rect):
self.pos += self.dir * self.velocity
self.rect.center = round(self.pos.x), round(self.pos.y)
if self.rect.left < border_rect.left:
self.reflect((1, 0), border_rect)
if self.rect.right > border_rect.right:
self.reflect((-1, 0), border_rect)
if self.rect.top < border_rect.top:
self.reflect((0, 1), border_rect)
if self.rect.bottom > border_rect.bottom:
self.reflect((0, -1), border_rect)
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
all_groups = pygame.sprite.Group()
start, velocity, direction = (250, 250), 5, (random.random(), random.random())
ball = Ball(start, velocity, direction)
all_groups.add(ball)
border_rect = pygame.Rect(100, 100, 300, 300)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
all_groups.update(border_rect)
window.fill('black')
pygame.draw.rect(window, (255, 0, 0), border_rect, 1)
all_groups.draw(window)
pygame.display.flip()