You need to enable JavaScript to run this app. Pygame实现点击显示图片直至鼠标按键松开功能的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Pygame实现点击显示图片直至鼠标按键松开功能的问题求助

问题核心原因
  • Pygame采用逐帧刷新的渲染逻辑,每次帧都会调用screen.fill清空整个画布,你当前仅在MOUSEBUTTONDOWN、MOUSEBUTTONUP单次事件触发的那一帧绘制对勾,后续帧没有重新绘制该元素,就会出现闪现后立刻消失的问题。
  • 你将全部绘制逻辑写在了事件遍历循环for event in pygame.event.get()内部,只有产生新的系统事件时才会执行绘制代码,无事件触发的帧不会执行绘制,也会导致元素显示异常。
修复方案
  1. 新增状态变量记录当前被按住的食物下标,初始化阶段添加:
pressed_food_idx = -1 # 值为-1代表没有食物被按住
  1. 拆分事件处理和绘制逻辑:事件循环仅负责更新状态变量,所有绘制逻辑统一移到事件循环外、pygame.display.flip()前执行,保证每帧都重绘所有元素。
  2. 在MOUSEBUTTONDOWN事件中判断是否点击到食物,命中则将pressed_food_idx赋值为对应食物的下标;MOUSEBUTTONUP事件中直接将pressed_food_idx重置为-1。
  3. 全局绘制阶段判断pressed_food_idx不为-1时,就为对应下标的食物绘制上方的对勾。
修复后完整代码
import pygame
import random as r
pygame.init()
screen = pygame.display.set_mode((325,325))
running = True
colors = [(199,203,132), (255,0,0), (0,255,0), (84,87,44)]
mouse_pos_x_left = [0.4, 1.98, 3.62, 5.22]
mouse_pos_x_right = [1.26, 2.86, 4.46, 6.06]
foods = ['bread', 'tomato', 'lettuce', 'ham']
food_selected = ''
customer1 = pygame.image.load('customer1.png')
food_box = pygame.image.load('food_box.png')
check = pygame.image.load('check.png')
same = False
# 提前缩放图片,避免每帧重复缩放损耗性能
customer1_img = pygame.transform.scale(customer1, (150,150))
food_box_img = pygame.transform.scale(food_box, (100, 125))
check_small = pygame.transform.scale(check, (30,30))
#rand
random_num_1 = r.randint(1,4)
random_num_2 = r.randint(1,4)
randoms = [1,2,3,4]
food_1 = ''
food_2 = ''
click = 0
able = False
pressed_food_idx = -1 # 新增的按住状态变量

while running:
    same_foods = False
    # 事件处理:仅更新状态
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            mouse_x = pos[0] / 50
            mouse_y = pos[1] / 50
            for i in range(4):
                if mouse_x > mouse_pos_x_left[i] and mouse_x < mouse_pos_x_right[i]:
                    if mouse_y > 5.4 and mouse_y < 6.26:
                        food_selected = foods[i]
                        pressed_food_idx = i
                        click +=1
        if event.type == pygame.MOUSEBUTTONUP:
            pressed_food_idx = -1
        if click == 3:
            click = 0   

    # 统一绘制所有元素
    screen.fill((255,255,255))
    pos = pygame.mouse.get_pos()
    mouse_x = pos[0] / 50
    mouse_y = pos[1] / 50

    # 绘制食物选项
    for i in range(4):
        pygame.draw.rect(screen, (colors[i]), (17 + 80 * i, 265, 50, 50))
        pygame.draw.rect(screen, (0,0,0), (17 + 80 * i, 265, 50, 50),5)
        # 鼠标悬浮高亮
        if mouse_x > mouse_pos_x_left[i] and mouse_x < mouse_pos_x_right[i]:
            if mouse_y > 5.4 and mouse_y < 6.26:
                pygame.draw.rect(screen, (255,255,255), (17 + 80 * i, 265, 50, 50), 5)
        # 按住状态显示对勾
        if pressed_food_idx == i:
            screen.blit(check, (17 + 80 * i, 210, 50, 50))

    # 绘制顾客和食物盒
    screen.blit(customer1_img, (50,50))
    screen.blit(food_box_img, (170,0))

    # 绘制顾客需要的食物
    for i in range(4):
        if random_num_1 == randoms[i]:
            food_1 = foods[i]
            pygame.draw.rect(screen, (colors[i]), (195, 5, 50, 50))
        if random_num_2 == randoms[i]:
            food_2 = foods[i]
            pygame.draw.rect(screen, (colors[i]), (195, 60, 50, 50))
    
    # 绘制选中匹配的对勾
    if food_1 == food_2:
        same_foods = True
        if click >= 1:
            screen.blit(check_small, (285, 10))
        if click >= 2:
            screen.blit(check_small, (285, 70))
    else:
        if food_selected == food_1:
            screen.blit(check_small, (285, 10))
        if food_selected == food_2:
            screen.blit(check_small, (285, 70))

    pygame.display.flip()
pygame.quit()

内容的提问来源于stack exchange,提问作者Morris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 05:57:01