鱼C论坛

 找回密码
 立即注册
查看: 88|回复: 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
原设计是按键按一次是开,再按一次是关。两个按钮同时 ...


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

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

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

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()

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

    if button1.is_on and button2.is_on:
        bulb.color = WHITE
    else:
        bulb.color = DIM

    button1.draw(win)
    button2.draw(win)
    bulb.draw(win)
    pygame.display.update()

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

修改后的代码如下:
# 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, initial_state=False):
        self.color = color
        self.x = x
        self.y = y
        self.l = l
        self.w = w
        self.pressed = initial_state

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

    def check_click(self, mouse_pos, mouse_pressed):
        if (self.x < mouse_pos[0] < self.x + self.l and
            self.y < mouse_pos[1] < self.y + self.w):
            if mouse_pressed[0]: # 检测左键
                self.pressed = True
            else:
                self.pressed = 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()
    mouse_pressed = pygame.mouse.get_pressed()
    
    for event in events:
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

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

    if button1.pressed and button2.pressed:
        bulb.color = WHITE
    else:
        bulb.color = DIM

    button1.draw(win)
    button2.draw(win)
    bulb.draw(win)

    pygame.display.update()

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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


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

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

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

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()

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

    if button1.is_on and button2.is_on:
        bulb.color = WHITE
    else:
        bulb.color = DIM

    button1.draw(win)
    button2.draw(win)
    bulb.draw(win)
    pygame.display.update()

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-17 03:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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