[Pygame] Sprite Collision Detection 파이게임 스프라이트 충돌 감지
Python 2023. 9. 4. 17:01 |스프라이트 그룹의 충돌을 감지해 보자.
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
import pygame
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.direction = -1
self.speed = 4
self.image = pygame.image.load("player.png").convert()
self.image.set_colorkey(self.image.get_at((0, 0)))
self.size = (self.image.get_width()*1.5, self.image.get_height()*1.5)
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect(center=position)
def flip_image(self):
self.image = pygame.transform.flip(self.image, True, False)
def update(self):
pass
# 플레이어 클래스
class Bubble(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("bubble.png").convert()
self.image.set_colorkey(self.image.get_at((0, 0)))
self.size = (self.image.get_width()*6, self.image.get_height()*6)
self.image = pygame.transform.scale(self.image, self.size)
self.rect = self.image.get_rect(center=position)
self.collided = False
def update(self):
if self.collided == True:
self.rect.top -= 1
# 버블 클래스
# 충돌(self.collided)을 감지하면 위치(self.rect.top)가 변한다.
def main():
player = Player((screen.get_width()/2, screen.get_height()/2))
player_sprite = pygame.sprite.Group(player)
# 플레이어 스프라이트 그룹
bubbles = [
Bubble((40, screen.get_height()/2)),
Bubble((160, screen.get_height()/2)),
Bubble((480, screen.get_height()/2)),
Bubble((600, screen.get_height()/2))]
bubble_sprites = pygame.sprite.Group(bubbles)
# 버블 스프라이트 그룹
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if player.direction > 0:
player.flip_image()
player.direction = -1
player.rect.move_ip(-player.speed, 0)
if keys[pygame.K_RIGHT]:
if player.direction < 0:
player.flip_image()
player.direction = 1
player.rect.move_ip(player.speed, 0)
collision = pygame.sprite.spritecollide(player, bubble_sprites, False)
for bubble in collision:
bubble.collided = True
# 플레이어와 버블의 충돌을 감지하고 충돌한 버블의 collided 값을 True로 바꾼다.
player_sprite.update()
bubble_sprites.update()
screen.fill("black")
player_sprite.draw(screen)
bubble_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
if __name__ == '__main__':
main()
|
※ 참고
pygame.sprite.spritecollideany()
Simple test if a sprite intersects anything in a group.
● spritecollideany(sprite, group, collided = None) -> Sprite Collision with the returned sprite.
● spritecollideany(sprite, group, collided = None) -> None No collision
If the sprite collides with any single sprite in the group, a single sprite from the group is returned. On no collision None is returned.
If you don't need all the features of the pygame.sprite.spritecollide() function, this function will be a bit quicker.
The collided argument is a callback function used to calculate if two sprites are colliding. It should take two sprites as values and return a bool value indicating if they are colliding. If collided is not passed, then all sprites must have a "rect" value, which is a rectangle of the sprite area, which will be used to calculate the collision.
'Python' 카테고리의 다른 글
[Pygame] Look at the Mouse Slowly 파이게임 마우스 따라 천천히 회전하기 (0) | 2023.09.05 |
---|---|
[Pygame] Look at the Mouse 파이게임 마우스 따라 회전하기 (0) | 2023.09.05 |
[Pygame] Bezier Curve 파이게임 베지어 곡선 (0) | 2023.09.04 |
[Pygame] Tweening 파이게임 트윈(트위닝) (0) | 2023.09.04 |
[Pygame] Sprite Animation 파이게임 스프라이트 애니메이션 (0) | 2023.09.03 |