|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pygame
- pygame.init()
- screen = pygame.display.set_mode((500,666))
- clock = pygame.time.Clock()#刷新帧率
- pygame.display.set_caption('测试')
- ratio = 1.0
- bg = pygame.image.load('背景.jpeg')
- yun = pygame.image.load('陨石.png')
- running = True
- while True:
- clock.tick(20)
- screen.blit(bg,(0,0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_DOWN:
- ratio += 0.1
- if event.key == pygame.K_UP:
- ratio -= 0.1
- if event.key == pygame.K_SPACE:
- ratio = 1.0
- pygame.transform.smoothscale( yun, (int(yun.get_rect().width * ratio), int(yun.get_rect().height * ratio)) )
- print(yun.get_rect().width,yun.get_rect().height)
- screen.blit(yun, (yun.get_rect().width,yun.get_rect().height))
- pygame.display.update()
复制代码
在你的代码中,你使用了`pygame.transform.smoothscale`对图片进行缩放,但是你并没有将缩放后的图片重新赋值给变量`yun`,所以在绘制时仍然使用的是原始大小的图片。
要修复这个问题,你需要将缩放后的图片赋值给变量`yun`,使得后续的绘制能够使用正确的尺寸。你可以在按下键盘按键时,将缩放操作移到事件处理的外部,像这样:
- while True:
- clock.tick(20)
- screen.blit(bg,(0,0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_DOWN:
- ratio += 0.1
- if event.key == pygame.K_UP:
- ratio -= 0.1
- if event.key == pygame.K_SPACE:
- ratio = 1.0
-
- yun = pygame.transform.smoothscale(yun, (int(yun.get_rect().width * ratio), int(yun.get_rect().height * ratio)))
-
- screen.blit(yun, (yun.get_rect().width, yun.get_rect().height))
- pygame.display.update()
复制代码
现在,当你按下键盘按键时,图片将按照指定的比例进行缩放并显示在屏幕上。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|