import pygame pygame.init() SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Resize Image") image = pygame.image.load("miaojv.jpg") image_rect = image.get_rect() scale = 1.0 min_scale = 0.5 max_scale = 2.0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # left click scale += 0.1 if scale < min_scale: scale = min_scale elif event.button == 3: # right click scale -= 0.1 if scale > max_scale: scale = max_scale screen.fill((255, 255, 255)) scaled_image = pygame.transform.scale(image, (int(image_rect.width * scale), int(image_rect.height * scale))) screen.blit(scaled_image, (SCREEN_WIDTH // 2 - scaled_image.get_width() // 2, SCREEN_HEIGHT // 2 - scaled_image.get_height() // 2)) pygame.display.flip() 改为左击放大,连续左击缩小
时间: 2023-12-02 21:03:54 浏览: 124
可以将代码中以下部分:
```
if event.button == 1: # left click
scale += 0.1
if scale < min_scale:
scale = min_scale
elif event.button == 3: # right click
scale -= 0.1
if scale > max_scale:
scale = max_scale
```
修改为:
```
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left click
scale += 0.1
if scale > max_scale:
scale = max_scale
elif event.button == 3: # right click
scale -= 0.1
if scale < min_scale:
scale = min_scale
```
这样就可以实现左击放大,连续左击缩小的效果了。
阅读全文