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() 改为左击放大,双击缩小
时间: 2024-01-12 16:03:07 浏览: 169
你可以将代码中以下部分:
```
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.button == 1: # left click
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # single click
scale += 0.1
if scale > max_scale:
scale = max_scale
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and event.pos == last_click_pos: # double click
scale -= 0.1
if scale < min_scale:
scale = min_scale
last_click_pos = event.pos
```
这样就可以实现左击放大,双击缩小的效果了。
相关问题
import pygame import sys 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 = 10.0 last_click_time = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # left click if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # single click if pygame.time.get_ticks() - last_click_time < 300: # double click scale -= 1 if scale < min_scale: scale = min_scale else: # single click scale += 1 if scale > max_scale: scale = max_scale last_click_time = pygame.time.get_ticks() last_click_pos = event.pos 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() 改为直接从摄像头读取图片
好的,你可以将代码改为从摄像头读取图片并进行缩放显示。下面是修改后的代码:
```python
import cv2
import pygame
import numpy as np
pygame.init()
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Resize Image")
cap = cv2.VideoCapture(0)
scale = 1.0
min_scale = 0.5
max_scale = 10.0
last_click_time = 0
while True:
ret, frame = cap.read()
if not ret:
continue
# 转换图像格式
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
for event in pygame.event.get():
if event.type == pygame.QUIT:
cap.release()
cv2.destroyAllWindows()
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left click
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: # single click
if pygame.time.get_ticks() - last_click_time < 300: # double click
scale -= 1
if scale < min_scale:
scale = min_scale
else: # single click
scale += 1
if scale > max_scale:
scale = max_scale
last_click_time = pygame.time.get_ticks()
last_click_pos = event.pos
# 将图像转换为 Pygame Surface 对象
surf = pygame.surfarray.make_surface(frame)
# 缩放图像
scaled_surf = pygame.transform.scale(surf, (int(surf.get_width() * scale), int(surf.get_height() * scale)))
# 显示图像
screen.fill((255, 255, 255))
screen.blit(scaled_surf, (SCREEN_WIDTH // 2 - scaled_surf.get_width() // 2, SCREEN_HEIGHT // 2 - scaled_surf.get_height() // 2))
pygame.display.flip()
```
这个程序会不断读取摄像头数据,将其转换为 Pygame Surface 对象,然后在 Pygame 窗口中进行缩放显示。你可以通过按下 ESC 键或关闭 Pygame 窗口来退出程序。在程序运行过程中,你可以使用鼠标左键来进行缩放操作。
import sys import pygame import pymunk import math import pymunk.pygame_util import os # 资源文件目录访问 def source_path(relative_path): # 是否Bundle Resource if getattr(sys, 'frozen', False): base_path = sys._MEIPASS else: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) # 修改当前工作目录,使得资源文件可以被正确访问 cd = source_path('') os.chdir(cd) pygame.init() SCREEN_WIDTH = 1200 SCREEN_HEIGHT = 678 BOTTOM_PANEL = 50 # 游戏窗口 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT + BOTTOM_PANEL)) pygame.display.set_caption("台球游戏") logo = pygame.image.load("assets/favicon.ico") pygame.display.set_icon(logo)代码注释
这段代码是一个使用 Pygame 和 Pymunk 库开发的台球游戏。它通过读取本地资源文件并设置游戏窗口的大小和标题,来初始化游戏界面。其中,source_path() 函数用于获取资源文件的绝对路径,并通过修改当前工作目录来确保资源文件可以被正确访问。BOTTOM_PANEL 变量用于指定底部面板的高度。接下来,通过 pygame.display.set_mode() 函数来设置游戏窗口的大小,以及通过 pygame.display.set_caption() 和 pygame.display.set_icon() 函数来设置游戏窗口的标题和图标。
阅读全文