|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
谢谢反馈,我理解你的需求了。我们需要改善按钮的状态检测逻辑,并确保每个按钮按一次即改变开关状态,同时只有两个按钮都在“开”状态时,灯才会亮。以下是修改后的代码:
# 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 小助理,如未能正确解答您的问题,请继续追问。
|
|