2016-07-24 170 views
0

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

+0

参见[_GUIs与pygame_](http://www.pygame.org/wiki/gui)或[与谷歌搜索](HTTPS ://www.google.com/search HL = EN&as_q = pygame的+ GUI +模块&as_epq =&as_oq =&as_eq =&as_nlo =&as_nhi =& LR =&CR =&as_qdr =所有&as_sitesearch =&as_occt =任何&安全=图像&as_filetype =&as_rights =#newwindow = 1&HL =烯&as_qdr =所有&q = pygame的+%22radio +按钮%22)。 – martineau

回答

2

我知道你是如何感觉男人,我需要一些单选按钮。不过,我已经在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()