Lecture Sprite 함수
Lecture
• Views 44
• Comments 0
• Last Updated at 1 week ago
- Sprite
collidelist & collidelistall
사각형과 사각형 리스트와의 충돌을 감지한다.
import pygame
pygame.init()
window = pygame.display.set_mode((250, 250))
sprite1 = pygame.sprite.Sprite()
sprite1.image = pygame.Surface((75, 75))
sprite1.image.fill((255, 0, 0))
sprite1.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(75, 75)
sprite2 = pygame.sprite.Sprite()
sprite2.image = pygame.Surface((75, 75))
sprite2.image.fill((0, 255, 0))
sprite2.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(75, 75)
all_group = pygame.sprite.Group([sprite2, sprite1])
test_group = pygame.sprite.Group(sprite2)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sprite1.rect.center = pygame.mouse.get_pos()
collide = pygame.sprite.spritecollide(sprite1, test_group, False)
window.fill(0)
all_group.draw(window)
for s in collide:
pygame.draw.rect(window, (255, 255, 255), s.rect, 5, 1)
pygame.display.flip()
pygame.quit()
sprite1이 sprite2,3,4와의 충돌 감지한다.
import pygame
pygame.init()
window = pygame.display.set_mode((250, 250))
sprite1 = pygame.sprite.Sprite()
sprite1.image = pygame.Surface((50, 50))
sprite1.image.fill((255, 0, 0))
sprite1.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(50, 50)
sprite2 = pygame.sprite.Sprite()
sprite2.image = pygame.Surface((30, 30))
sprite2.image.fill((0, 255, 0))
sprite2.rect = pygame.Rect((50,50,30,30))
sprite3 = pygame.sprite.Sprite()
sprite3.image = pygame.Surface((30, 30))
sprite3.image.fill((0, 0, 255))
sprite3.rect = pygame.Rect((150,150,30,30))
sprite4 = pygame.sprite.Sprite()
sprite4.image = pygame.Surface((30, 30))
sprite4.image.fill((127, 127, 127))
sprite4.rect = pygame.Rect((100,100,30,30))
#.inflate(75, 75) #*window.get_rect().center
all_group = pygame.sprite.Group([sprite1, sprite2, sprite3, sprite4])
test_group = pygame.sprite.Group([sprite2, sprite3, sprite4])
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sprite1.rect.center = pygame.mouse.get_pos()
collide = pygame.sprite.spritecollide(sprite1, test_group, False)
window.fill(0)
all_group.draw(window)
for s in collide:
pygame.draw.rect(window, (255, 255, 255), s.rect, 5)
pygame.display.flip()
pygame.quit()
사각형이 아닌 원의 충돌을 감지한다.
import pygame
pygame.init()
window = pygame.display.set_mode((250, 250))
sprite1 = pygame.sprite.Sprite()
sprite1.image = pygame.Surface((60, 60), pygame.SRCALPHA)
# SRCALPHA :
pygame.draw.circle(sprite1.image, (255, 0, 0), (30, 30), 30)
sprite1.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(60, 60)
sprite1.radius = 30
sprite2 = pygame.sprite.Sprite()
sprite2.image = pygame.Surface((60, 60), pygame.SRCALPHA)
pygame.draw.circle(sprite2.image, (0, 255, 0), (30, 30), 30)
sprite2.rect = pygame.Rect(*window.get_rect().center, 0, 0).inflate(60, 60)
sprite2.radius = 30
all_group = pygame.sprite.Group([sprite2, sprite1])
test_group = pygame.sprite.Group(sprite2)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sprite1.rect.center = pygame.mouse.get_pos()
collide = pygame.sprite.spritecollide(sprite1, test_group, False, pygame.sprite.collide_circle)
window.fill(0)
all_group.draw(window)
for s in collide:
pygame.draw.circle(window, (255, 255, 255), s.rect.center, s.rect.width // 2, 5)
pygame.display.flip()
pygame.quit()
import pygame
class SpriteObject(pygame.sprite.Sprite):
def __init__(self, x, y, color):
super().__init__()
self.original_image = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.circle(self.original_image, color, (25, 25), 25)
self.click_image = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.circle(self.click_image, color, (25, 25), 25)
pygame.draw.circle(self.click_image, (255, 255, 255), (25, 25), 25, 4)
self.image = self.original_image
self.rect = self.image.get_rect(center = (x, y))
self.clicked = False
def update(self, event_list,velocity):
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.clicked = not self.clicked
self.image = self.click_image if self.clicked else self.original_image
x,y = self.rect.center
self.rect.center = (x + velocity[0],y + velocity[1])
#self.rect.center[1] = self.rect.center[1] + velocity[1]
#self.rect.center = self.rect.center + velocity
pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
sprite_object = SpriteObject(*window.get_rect().center, (128, 128, 0))
group = pygame.sprite.Group([
SpriteObject(window.get_width() // 3, window.get_height() // 3, (128, 0, 0)),
SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, (0, 128, 0)),
SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, (0, 0, 128)),
SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, (128, 128, 0)),
])
run = True
while run:
clock.tick(1)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
group.update(event_list,(1,1))
#group.move((1,0))
window.fill(0)
group.draw(window)
pygame.display.flip()
pygame.quit()
first article
last article
Login to write a comment.