파이 게임으로 이미지 크기를 늘리는 방법 [중복]
내가 만들고있는 게임에서 (old_width, old_height)에서 (new_width, new_height)로 크기를 조정해야하는 이미지가있어서 더 커지는 것처럼 보이고 게임 중에 여러 번 수행해야합니다 .
지금까지 pygame.trasform.smoothscale로 시도했지만 어떤 이유로 화면을 따라 이미지를 오른쪽 모서리로 이동하는 것뿐입니다.
나는 무슨 일이 일어나고 있는지 이해하지 못하기 때문에 매우 이상하게 보입니다.
이미지를 처리하는 클래스는 다음과 같습니다.
class GrowingImage:
def __init__(self, image_path, x, y, width, height):
self.width = width
self.height = height
self.object_image = pygame.image.load(image_path)
self.image = pygame.transform.scale(self.object_image, (self.width, self.height))
self.x_pos = x
self.y_pos = y
def draw(self, background):
background.blit(self.image, (self.x_pos, self.y_pos))
def grow(self):
self.image = pygame.transform.smoothscale(self.object_image, (self.width, self.height))
self.width += 1
self.height += 1
그리고 이것이 메서드 grow가 호출되는 곳입니다. 이 질문에 불필요한 나머지는 모두 삭제 했으므로 이론적으로 이미지는이 시점에서 끝없이 커져야합니다.
image = GrowingImage('image.png', 400, 100, 405, 640)
while not is_game_over:
for event in pygame.event.get():
# Bunch of stuffs
# Redraw the screen
self.game_screen.fill(WHITE_COLOR)
image.grow()
image.draw()
# Display the screen
pygame.display.update()
# Tick the clock to update everything within the game.
clock.tick(self.TICK_RATE)
글쎄요, 왜 이런 일이 일어나는지 전혀 모릅니다. 아무도 내가 여기서 뭘 잘못하고 있는지 알아?
답변
이미지가 실제로 움직이지 않습니다. 이미지의 왼쪽 상단 모서리에서 시작한다는 것을 기억하십시오. 이미지의 너비와 높이가 커지므로 오른쪽 하단 모서리를 향해 움직이는 것처럼 보일 수 있습니다. 그것이 그려지는 곳이기도합니다. 그래서 그것이 성장하는 것처럼 보이게하기 위해, 당신은 그것이 나의 절반의 너비와 높이를 그린 부분을 오프셋 할 수 있습니다. 이것은 기본적으로 그것이 중앙에 그려 지도록 오프셋을 의미합니다. 바꾸다
background.blit(self.image, (self.x_pos, self.y_pos))
와
background.blit(self.image, (self.x_pos - (self.width/2), self.y_pos - (self.height/2)))
작업 예
import pygame
class GrowingImage:
def __init__(self, image_path, x, y, width, height):
self.width = width
self.height = height
self.object_image = image_path
self.image = pygame.transform.scale(self.object_image, (self.width, self.height))
self.x_pos = x
self.y_pos = y
def draw(self, background):
background.blit(self.image, (self.x_pos - (self.width/2), self.y_pos - (self.height/2)))
def grow(self):
self.image = pygame.transform.smoothscale(self.object_image, (self.width, self.height))
self.width += 1
self.height += 1
pygame.init()
d = pygame.display.set_mode((600, 600))
image = pygame.Surface((1, 1))
growingImage = GrowingImage(image, 300, 300, 20, 20)
while True:
d.fill((255, 255, 255))
pygame.event.get()
growingImage.draw(d)
growingImage.grow()
pygame.display.update()
의 두 번째 인수 blit는 직사각형 일 수 있습니다. pygame.Rect개체를 사용하여 중심을 기준으로 이미지 크기를 조정합니다.
class GrowingImage:
def __init__(self, image_path, x, y, width, height):
self.object_image = pygame.image.load(image_path)
self.image = pygame.transform.scale(self.object_image, (self.width, self.height))
self.rect = self.image.get_rect(topleft = (x, y))
def draw(self, background):
background.blit(self.image, self.rect)
def grow(self):
w = self.rect.width + 1
h = self.rect.height + 1
self.image = pygame.transform.smoothscale(self.object_image, (w, h))
self.rect = self.image.get_rect(center = self.rect.center)
배율 변환 및 표면 확대 / 축소 참조