Pygame。如何在每次吃鱼时调整我的 Fish Player 图像大小?
Pygame. How Do I Resize My Fish Player Image Everytime It Eats A Fish?
每次它吃鱼时,我都想让我的鱼变大,但我不确定为什么它不起作用,这就是我所做的
其他一切正常,但我的玩家身高或宽度不相加
video
# our main loop
runninggame = True
while runninggame:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
#[...........]
for blac in blacs:
if playerman.rect.colliderect(blac.rect):
playerman.width += 10
playerman.height += 10
blac.x = 880
我的完整代码:
https://pastebin.com/iL5h4fst
播放器所有图片需要pygame.transform.smoothscale()
缩放,放大后:
if playerman.rect.colliderect(blac.rect):
playerman.width += 10
playerman.height += 10
blac.x = 880
playerman.right = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.right]
playerman.left = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.left]
playerman.rect = pygame.Rect(playerman.x, playerman.y, playerman.width, playerman.height)
属性 self.width
和 self.height
必须由图像的缩小尺寸初始化(image.get_width()//8
分别 image.get_height()//8
)
随着播放器的增大,您需要将播放器的大小乘以比例因子以保持纵横比。使用pygame.transform.scale
时,宽高四舍五入为整数:
class player:
def __init__(self,x,y,height,width,color):
# [...]
self.right_original = [pygame.image.load("r" + str(i) + ".png") for i in range(1, 13)]
self.left_original = [pygame.image.load("l" + str(i) + ".png") for i in range(1, 13)]
self.width = self.right_original[0].get_width() / 8
self.height = self.right_original[0].get_height() / 8
self.scale_images()
# [...]
def scale_images(self):
w, h = round(self.width), round(self.height)
self.right = [pygame.transform.scale(image, (w, h)) for image in self.right_original]
self.left = [pygame.transform.scale(image, (w, h)) for image in self.left_original]
self.rect = pygame.Rect(self.x, self.y, w, h)
def collide(self, other_rect):
return self.rect.colliderect(other_rect)
def grow(self, scale):
self.width *= scale
self.height *= scale
self.scale_images()
for blac in blacs:
if playerman.collide(blac):
blac.x = 880
playerman.grow(1.1)
每次它吃鱼时,我都想让我的鱼变大,但我不确定为什么它不起作用,这就是我所做的
其他一切正常,但我的玩家身高或宽度不相加 video
# our main loop
runninggame = True
while runninggame:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
#[...........]
for blac in blacs:
if playerman.rect.colliderect(blac.rect):
playerman.width += 10
playerman.height += 10
blac.x = 880
我的完整代码: https://pastebin.com/iL5h4fst
播放器所有图片需要pygame.transform.smoothscale()
缩放,放大后:
if playerman.rect.colliderect(blac.rect):
playerman.width += 10
playerman.height += 10
blac.x = 880
playerman.right = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.right]
playerman.left = [pygame.transform.scale(image, (playerman.width,playerman.height)) for image in playerman.left]
playerman.rect = pygame.Rect(playerman.x, playerman.y, playerman.width, playerman.height)
属性 self.width
和 self.height
必须由图像的缩小尺寸初始化(image.get_width()//8
分别 image.get_height()//8
)
随着播放器的增大,您需要将播放器的大小乘以比例因子以保持纵横比。使用pygame.transform.scale
时,宽高四舍五入为整数:
class player:
def __init__(self,x,y,height,width,color):
# [...]
self.right_original = [pygame.image.load("r" + str(i) + ".png") for i in range(1, 13)]
self.left_original = [pygame.image.load("l" + str(i) + ".png") for i in range(1, 13)]
self.width = self.right_original[0].get_width() / 8
self.height = self.right_original[0].get_height() / 8
self.scale_images()
# [...]
def scale_images(self):
w, h = round(self.width), round(self.height)
self.right = [pygame.transform.scale(image, (w, h)) for image in self.right_original]
self.left = [pygame.transform.scale(image, (w, h)) for image in self.left_original]
self.rect = pygame.Rect(self.x, self.y, w, h)
def collide(self, other_rect):
return self.rect.colliderect(other_rect)
def grow(self, scale):
self.width *= scale
self.height *= scale
self.scale_images()
for blac in blacs:
if playerman.collide(blac):
blac.x = 880
playerman.grow(1.1)