鱼C论坛

 找回密码
 立即注册
查看: 480|回复: 3

[已解决]pygame两个按钮控制一个灯的代码有什么问题?

[复制链接]
发表于 2024-8-28 19:16:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
设计为两个按钮同时按下,灯才亮。以下代码并不能实现这一逻辑关系,为什么呢?
# coding: utf-8
import pygame
import sys
from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((360, 450))
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WHITE =(255,255,255)
DIM =(40,40,40)
class Circle:
    def __init__(self, color, x, y, r, width):
        self.color = color
        self.x = x
        self.y = y
        self.r = r
        self.width = width
    def draw(self, win):
        pygame.draw.circle(win, self.color, (self.x, self.y),self.r , 0)

# 创建按钮类
class Button:
    def __init__(self,color, x, y, l,w):
        self.color =color
        self.x = x
        self.y = y
        self.l = l
        self.w = w
        self.count =0

    def draw(self, win):
        for event in events:
            if pos[0] > self.x and pos[0] < self.x + self.l:
                if pos[1] > self.y and pos[1] < self.y + self.w:
                    if event.type == pygame.MOUSEBUTTONDOWN:
                        self.count += 1

        if self.count % 2==1:
            color= GREEN
            pygame.draw.rect(win,color , (self.x, self.y, self.l, self.w), 0)
            return True
        if self.count % 2==0:
            color= RED
            pygame.draw.rect(win, color, (self.x, self.y, self.l, self.w), 0)
            return False

button1 = Button(RED,50, 150, 60,40)
button2 = Button(RED,250, 150, 60,40)
bulb = Circle(DIM, 180, 300, 60, 0)
while True:
    win.fill((0, 0, 0))
    events=pygame.event.get()
    pos = pygame.mouse.get_pos()
    for event in events:
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    '''
    if button1.draw(win) and button2.draw(win) :
        bulb.color = WHITE
    else:
        bulb.color = DIM
    '''
    if (button1.count==1) and  (button2.count == 1) :
        bulb.color = WHITE
    else:
        bulb.color = DIM
    button1.draw(win)
    button2.draw(win)
    bulb.draw(win)
    pygame.display.update()
上述代码显示了两种方法,结果都有问题。@FishC
最佳答案
2024-8-28 20:30:24
ydwb 发表于 2024-08-28 20:22
原设计是按键按一次是开,再按一次是关。两个按钮同时 ...


谢谢反馈,我理解你的需求了。我们需要改善按钮的状态检测逻辑,并确保每个按钮按一次即改变开关状态,同时只有两个按钮都在“开”状态时,灯才会亮。以下是修改后的代码:

  1. # coding: utf-8
  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. win = pygame.display.set_mode((360, 450))
  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)
  9. WHITE = (255, 255, 255)
  10. DIM = (40, 40, 40)

  11. class Circle:
  12.     def __init__(self, color, x, y, r, width):
  13.         self.color = color
  14.         self.x = x
  15.         self.y = y
  16.         self.r = r
  17.         self.width = width

  18.     def draw(self, win):
  19.         pygame.draw.circle(win, self.color, (self.x, self.y), self.r, 0)

  20. # 创建按钮类
  21. class Button:
  22.     def __init__(self, color, x, y, l, w):
  23.         self.color = color
  24.         self.x = x
  25.         self.y = y
  26.         self.l = l
  27.         self.w = w
  28.         self.is_on = False

  29.     def draw(self, win):
  30.         # 绘制按钮
  31.         current_color = GREEN if self.is_on else RED
  32.         pygame.draw.rect(win, current_color, (self.x, self.y, self.l, self.w), 0)

  33.     def check_click(self, pos, events):
  34.         for event in events:
  35.             if event.type == pygame.MOUSEBUTTONDOWN:
  36.                 if self.x < pos[0] < self.x + self.l and self.y < pos[1] < self.y + self.w:
  37.                     self.is_on = not self.is_on

  38. button1 = Button(RED, 50, 150, 60, 40)
  39. button2 = Button(RED, 250, 150, 60, 40)
  40. bulb = Circle(DIM, 180, 300, 60, 0)

  41. while True:
  42.     win.fill((0, 0, 0))
  43.     events = pygame.event.get()
  44.     pos = pygame.mouse.get_pos()
  45.     for event in events:
  46.         if event.type == QUIT:
  47.             pygame.quit()
  48.             sys.exit()

  49.     button1.check_click(pos, events)
  50.     button2.check_click(pos, events)

  51.     if button1.is_on and button2.is_on:
  52.         bulb.color = WHITE
  53.     else:
  54.         bulb.color = DIM

  55.     button1.draw(win)
  56.     button2.draw(win)
  57.     bulb.draw(win)
  58.     pygame.display.update()
复制代码


解释:
1. **Button类**: 我添加了一个 `is_on` 属性来跟踪按钮状态。按钮被点击时,状态在“开”和“关”之间切换。
2. **绘制颜色**: 根据 `is_on` 状态改变按钮颜色。
3. **check_click 方法**: 检查按钮是否被点击并切换状态。
4. **灯的控制**: 判断两个按钮都是“开”状态,如果是,则灯亮,否则灯灭。

现在,点击按钮时它们的状态会正确记录,当两个按钮同时处于“开”状态时,灯会亮。希望这能解决你的问题!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-28 19:22:49 | 显示全部楼层
你好,鱼油。你提出的代码逻辑问题主要是在于按钮状态的检测以及 `button1.count` 和 `button2.count` 的值更新方式。这导致在点击按钮后没有正确记录状态。以下是修改建议和解释:

1. **使用 `pygame.mouse.get_pressed()` 检测鼠标按压状态:** 这样可以确保检测到按下和释放的事件。
2. **避免使用多余的计数器**:可以用布尔变量来检测按钮是否被按下。
3. **在主循环中检测鼠标点击**:这样可以持续更新按钮状态。

修改后的代码如下:

  1. # coding: utf-8
  2. import pygame
  3. import sys
  4. from pygame.locals import *

  5. pygame.init()
  6. win = pygame.display.set_mode((360, 450))
  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)
  9. WHITE = (255, 255, 255)
  10. DIM = (40, 40, 40)

  11. class Circle:
  12.     def __init__(self, color, x, y, r, width):
  13.         self.color = color
  14.         self.x = x
  15.         self.y = y
  16.         self.r = r
  17.         self.width = width

  18.     def draw(self, win):
  19.         pygame.draw.circle(win, self.color, (self.x, self.y), self.r, 0)

  20. # 创建按钮类
  21. class Button:
  22.     def __init__(self, color, x, y, l, w, initial_state=False):
  23.         self.color = color
  24.         self.x = x
  25.         self.y = y
  26.         self.l = l
  27.         self.w = w
  28.         self.pressed = initial_state

  29.     def draw(self, win):
  30.         color = GREEN if self.pressed else RED
  31.         pygame.draw.rect(win, color, (self.x, self.y, self.l, self.w), 0)

  32.     def check_click(self, mouse_pos, mouse_pressed):
  33.         if (self.x < mouse_pos[0] < self.x + self.l and
  34.             self.y < mouse_pos[1] < self.y + self.w):
  35.             if mouse_pressed[0]: # 检测左键
  36.                 self.pressed = True
  37.             else:
  38.                 self.pressed = False

  39. button1 = Button(RED, 50, 150, 60, 40)
  40. button2 = Button(RED, 250, 150, 60, 40)
  41. bulb = Circle(DIM, 180, 300, 60, 0)

  42. while True:
  43.     win.fill((0, 0, 0))
  44.     events = pygame.event.get()
  45.     pos = pygame.mouse.get_pos()
  46.     mouse_pressed = pygame.mouse.get_pressed()
  47.    
  48.     for event in events:
  49.         if event.type == QUIT:
  50.             pygame.quit()
  51.             sys.exit()

  52.     button1.check_click(pos, mouse_pressed)
  53.     button2.check_click(pos, mouse_pressed)

  54.     if button1.pressed and button2.pressed:
  55.         bulb.color = WHITE
  56.     else:
  57.         bulb.color = DIM

  58.     button1.draw(win)
  59.     button2.draw(win)
  60.     bulb.draw(win)

  61.     pygame.display.update()
复制代码


主要修改点:
1. 在 `Button` 类中新加入 `pressed` 布尔变量,用来记录按钮是否被按下。
2. 定义 `check_click` 方法来检测鼠标点击并更新 `pressed` 状态。
3. 在主循环中调用 `check_click` 方法来检测鼠标点击事件并判断按钮状态。

这样修改之后,只有在两个按钮都被按下时,灯才亮。希望这能解决你的问题!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-8-28 20:22:20 | 显示全部楼层
原设计是按键按一次是开,再按一次是关。两个按钮同时是开,灯才亮。@FishC, 你的设计是同时按,那必须要有两个鼠标。运行了,通不过。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-28 20:30:24 | 显示全部楼层    本楼为最佳答案   
ydwb 发表于 2024-08-28 20:22
原设计是按键按一次是开,再按一次是关。两个按钮同时 ...


谢谢反馈,我理解你的需求了。我们需要改善按钮的状态检测逻辑,并确保每个按钮按一次即改变开关状态,同时只有两个按钮都在“开”状态时,灯才会亮。以下是修改后的代码:

  1. # coding: utf-8
  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. win = pygame.display.set_mode((360, 450))
  7. GREEN = (0, 255, 0)
  8. RED = (255, 0, 0)
  9. WHITE = (255, 255, 255)
  10. DIM = (40, 40, 40)

  11. class Circle:
  12.     def __init__(self, color, x, y, r, width):
  13.         self.color = color
  14.         self.x = x
  15.         self.y = y
  16.         self.r = r
  17.         self.width = width

  18.     def draw(self, win):
  19.         pygame.draw.circle(win, self.color, (self.x, self.y), self.r, 0)

  20. # 创建按钮类
  21. class Button:
  22.     def __init__(self, color, x, y, l, w):
  23.         self.color = color
  24.         self.x = x
  25.         self.y = y
  26.         self.l = l
  27.         self.w = w
  28.         self.is_on = False

  29.     def draw(self, win):
  30.         # 绘制按钮
  31.         current_color = GREEN if self.is_on else RED
  32.         pygame.draw.rect(win, current_color, (self.x, self.y, self.l, self.w), 0)

  33.     def check_click(self, pos, events):
  34.         for event in events:
  35.             if event.type == pygame.MOUSEBUTTONDOWN:
  36.                 if self.x < pos[0] < self.x + self.l and self.y < pos[1] < self.y + self.w:
  37.                     self.is_on = not self.is_on

  38. button1 = Button(RED, 50, 150, 60, 40)
  39. button2 = Button(RED, 250, 150, 60, 40)
  40. bulb = Circle(DIM, 180, 300, 60, 0)

  41. while True:
  42.     win.fill((0, 0, 0))
  43.     events = pygame.event.get()
  44.     pos = pygame.mouse.get_pos()
  45.     for event in events:
  46.         if event.type == QUIT:
  47.             pygame.quit()
  48.             sys.exit()

  49.     button1.check_click(pos, events)
  50.     button2.check_click(pos, events)

  51.     if button1.is_on and button2.is_on:
  52.         bulb.color = WHITE
  53.     else:
  54.         bulb.color = DIM

  55.     button1.draw(win)
  56.     button2.draw(win)
  57.     bulb.draw(win)
  58.     pygame.display.update()
复制代码


解释:
1. **Button类**: 我添加了一个 `is_on` 属性来跟踪按钮状态。按钮被点击时,状态在“开”和“关”之间切换。
2. **绘制颜色**: 根据 `is_on` 状态改变按钮颜色。
3. **check_click 方法**: 检查按钮是否被点击并切换状态。
4. **灯的控制**: 判断两个按钮都是“开”状态,如果是,则灯亮,否则灯灭。

现在,点击按钮时它们的状态会正确记录,当两个按钮同时处于“开”状态时,灯会亮。希望这能解决你的问题!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表