gpt4 book ai didi

python - Pygame 中的单选按钮?

转载 作者:行者123 更新时间:2023-12-05 08:33:32 28 4
gpt4 key购买 nike

我想知道 pygame 或其模块中是否有单选按钮。我正在制作一个需要单选按钮的问题游戏。

最佳答案

我知道你的感受,伙计,我需要一些单选按钮。但是,我一直在研究 pygame 中的复选框。如果你想使用它,我会在下面发布代码:

import pygame as pg
pg.init()


class Checkbox:
def __init__(self, surface, x, y, color=(230, 230, 230), caption="", outline_color=(0, 0, 0),
check_color=(0, 0, 0), font_size=22, font_color=(0, 0, 0), text_offset=(28, 1)):
self.surface = surface
self.x = x
self.y = y
self.color = color
self.caption = caption
self.oc = outline_color
self.cc = check_color
self.fs = font_size
self.fc = font_color
self.to = text_offset
# checkbox object
self.checkbox_obj = pg.Rect(self.x, self.y, 12, 12)
self.checkbox_outline = self.checkbox_obj.copy()
# variables to test the different states of the checkbox
self.checked = False
self.active = False
self.unchecked = True
self.click = False

def _draw_button_text(self):
self.font = pg.font.Font(None, self.fs)
self.font_surf = self.font.render(self.caption, True, self.fc)
w, h = self.font.size(self.caption)
self.font_pos = (self.x + 12 / 2 - w / 2 + self.to[0], self.y + 12 / 2 - h / 2 + self.to[1])
self.surface.blit(self.font_surf, self.font_pos)

def render_checkbox(self):
if self.checked:
pg.draw.rect(self.surface, self.color, self.checkbox_obj)
pg.draw.rect(self.surface, self.oc, self.checkbox_outline, 1)
pg.draw.circle(self.surface, self.cc, (self.x + 6, self.y + 6), 4)

elif self.unchecked:
pg.draw.rect(self.surface, self.color, self.checkbox_obj)
pg.draw.rect(self.surface, self.oc, self.checkbox_outline, 1)
self._draw_button_text()

def _update(self, event_object):
x, y = event_object.pos
# self.x, self.y, 12, 12
px, py, w, h = self.checkbox_obj # getting check box dimensions
if px < x < px + w and px < x < px + w:
self.active = True
else:
self.active = False

def _mouse_up(self):
if self.active and not self.checked and self.click:
self.checked = True
elif self.checked:
self.checked = False
self.unchecked = True

if self.click is True and self.active is False:
if self.checked:
self.checked = True
if self.unchecked:
self.unchecked = True
self.active = False

def update_checkbox(self, event_object):
if event_object.type == pg.MOUSEBUTTONDOWN:
self.click = True
# self._mouse_down()
if event_object.type == pg.MOUSEBUTTONUP:
self._mouse_up()
if event_object.type == pg.MOUSEMOTION:
self._update(event_object)

def is_checked(self):
if self.checked is True:
return True
else:
return False

def is_unchecked(self):
if self.checked is False:
return True
else:
return False

但有一些警告。代码中仍有一些错误需要解决,其中之一是您可以取消选中复选框而无需将鼠标悬停在它上面。目前它们的功能不像单选按钮。还有一件事。我在 python 3.5 中编写了所有这些代码。抱歉

也就是说,非常欢迎您使用代码开始构建自己的单选按钮。请务必向我展示您的想法 ;)。

这是一个如何使用该模块的例子:

import checkbox
import pygame as pg

def main():
WIDTH = 800
HEIGHT = 600
display = pg.display.set_mode((WIDTH, HEIGHT))

chkbox = checkbox.Checkbox(display, 400, 400)

running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
pg.quit()
quit()
chkbox.update_checkbox(event)

display.fill((200, 200, 200))
chkbox.render_checkbox()
pg.display.flip()

if __name__ == '__main__': main()

关于python - Pygame 中的单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551168/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com