问题描述
每次尝试吃一条鱼时,我都试图使它变大,但是我不确定为什么它不起作用,这就是我所做的
其他所有方法都有效,但我的玩家身高或宽度未加在一起 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.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,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)