Cách làm cho hình ảnh tăng kích thước với pygame [trùng lặp]
Trong một trò chơi mà tôi đang làm, tôi có một hình ảnh mà tôi cần mở rộng tỷ lệ từ (old_width, old_height) thành (new_width, new_height), để nó có vẻ lớn hơn và tôi cần phải làm điều đó nhiều lần trong trò chơi .
Cho đến nay, tôi đã thử với pygame.trasform.smoothscale, nhưng vì một số lý do, tất cả những gì nó làm được là nó đang di chuyển hình ảnh dọc theo màn hình về phía góc bên phải.
Nó có vẻ rất lạ đối với tôi, vì tôi không hiểu chuyện gì đang xảy ra.
Đây là lớp xử lý hình ảnh:
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
Và đây là nơi mà phương pháp grow được gọi là. Tôi đã xóa tất cả phần còn lại thừa cho câu hỏi này, vì vậy về mặt lý thuyết, hình ảnh sẽ phát triển không ngừng tại thời điểm này:
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)
Chà, nó không xảy ra và tôi hoàn toàn không biết tại sao điều này lại xảy ra. Có ai biết tôi đang làm gì sai ở đây không?
Trả lời
Hình ảnh không thực sự chuyển động. Nó có thể trông giống như di chuyển về phía góc dưới bên phải bởi vì nó ngày càng lớn hơn tức là chiều rộng và chiều cao của nó đang tăng lên, mà hãy nhớ rằng, bắt đầu ở góc trên cùng bên trái của hình ảnh. Đó cũng là nơi nó được vẽ. Vì vậy, để làm cho nó trông giống như đang phát triển, bạn có thể bù đắp nơi mà nó được vẽ bằng nửa chiều rộng và chiều cao của tôi, về cơ bản có nghĩa là bù đắp nó để nó được vẽ ở trung tâm. Thay thế
background.blit(self.image, (self.x_pos, self.y_pos))
với
background.blit(self.image, (self.x_pos - (self.width/2), self.y_pos - (self.height/2)))
Ví dụ làm việc
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()
Đối số thứ 2 của blitcó thể là một hình chữ nhật. Sử dụng một pygame.Rectđối tượng, để chia tỷ lệ hình ảnh so với trung tâm của nó:
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)
Xem thêm Biến đổi tỷ lệ và bề mặt thu phóng