2017-09-13 4 views
1

Je le code pour un bouton qui est pressable:Insérer score dans une fonction de jeu bouton

def button(msg,xloc,yloc,xlong,ylong,b1,b2,action=None): 

    hover = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 

    if xloc < hover [0] < xloc+xlong and yloc< hover [1] < yloc+ylong: 
     pygame.draw.rect(display, b1, (xloc ,yloc ,xlong,ylong)) 
     if click [0]==1 and action != None: 
      action() 

    else: 
     pygame.draw.rect(gameDisplay, inactiveButton, (xloc ,yloc ,xlong,ylong)) 
    label = pygame.font.SysFont("arial",16) 
    textSurf, textBox = textMsg(msg, label) 
    textBox.center = ((xloc +(300)),((yloc +(150)) 
    gameDisplay.blit(textSurf,textBox) 

et le code de la notation est:

for event in pygame.event.get(): 
    if event.type == pygame.QUIT: 
     pygame.quit() 
     quit() 

    if event.type == pygame.MOUSEBUTTONDOWN: 
     score+=1 
     print (score) 

Je voudrais avoir un score qui – en appuyant sur le bouton correct dans les choix afin de répondre à un jeu-questionnaire – sera affiché et incrémenté de 1. Comment puis-je faire cela?

+0

Votre question manque quelque chose ... Une question. –

+0

Bienvenue dans Stack Overflow! S'il vous plaît prendre la visite pour voir comment fonctionne le site et quelles questions sont sur le sujet ici, et éditez votre question en conséquence. Voir aussi: https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question –

Répondre

1

Voici la façon la plus simple d'implémenter un bouton que je connais. Créez un rect pour le bouton et dessinez-le avec pygame.draw.rect (ou blit une image). Pour la détection de collision, vérifiez si le event.pos d'un événement pygame.MOUSEBUTTONDOWN entre en collision avec le rect, puis incrémente simplement la variable score.

import pygame as pg 


pg.init() 
screen = pg.display.set_mode((640, 480)) 

GRAY = pg.Color('gray15') 
BLUE = pg.Color('dodgerblue1') 


def main(): 
    clock = pg.time.Clock() 
    font = pg.font.Font(None, 30) 
    button_rect = pg.Rect(200, 200, 50, 30) 

    score = 0 
    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.MOUSEBUTTONDOWN: 
       if event.button == 1: 
        if button_rect.collidepoint(event.pos): 
         print('Button pressed.') 
         score += 1 

     screen.fill(GRAY) 
     pg.draw.rect(screen, BLUE, button_rect) 
     txt = font.render(str(score), True, BLUE) 
     screen.blit(txt, (260, 206)) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    main() 
    pg.quit() 

Addendum: En fait, je mettre en œuvre un bouton à l'aide des classes, des sprites et des groupes d'images-objets. Si vous ne savez pas comment fonctionnent les classes et les sprites, je vous recommande de consulter le Program Arcade Games (chapter 12 and 13).

import pygame as pg 


pg.init() 
GRAY= pg.Color('gray12') 
BLUE = pg.Color('dodgerblue1') 
FONT = pg.font.Font(None, 30) 


# The Button is a pygame sprite, that means we can add the 
# instances to a sprite group and then update and render them 
# by calling `sprite_group.update()` and `sprite_group.draw(screen)`. 
class Button(pg.sprite.Sprite): 

    def __init__(self, pos, callback): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((50, 30)) 
     self.image.fill(BLUE) 
     self.rect = self.image.get_rect(topleft=pos) 
     self.callback = callback 

    def handle_event(self, event): 
     """Handle events that get passed from the event loop.""" 
     if event.type == pg.MOUSEBUTTONDOWN: 
      if self.rect.collidepoint(event.pos): 
       print('Button pressed.') 
       # Call the function that we passed during the 
       # instantiation. (In this case just `increase_x`.) 
       self.callback() 


class Game: 

    def __init__(self): 
     self.screen = pg.display.set_mode((800, 600)) 
     self.clock = pg.time.Clock() 

     self.x = 0 
     self.button = Button((200, 200), callback=self.increase_x) 
     self.buttons = pg.sprite.Group(self.button) 
     self.done = False 

    # A callback function that we pass to the button instance. 
    # It gets called if a collision in the handle_event method 
    # is detected. 
    def increase_x(self): 
     """Increase self.x if button is pressed.""" 
     self.x += 1 

    def run(self): 
     while not self.done: 
      self.handle_events() 
      self.run_logic() 
      self.draw() 
      self.clock.tick(30) 

    def handle_events(self): 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       self.done = True 

      for button in self.buttons: 
       button.handle_event(event) 

    def run_logic(self): 
     self.buttons.update() 

    def draw(self): 
     self.screen.fill(GRAY) 
     self.buttons.draw(self.screen) 
     txt = FONT.render(str(self.x), True, BLUE) 
     self.screen.blit(txt, (260, 206)) 

     pg.display.flip() 


if __name__ == "__main__": 
    Game().run() 
    pg.quit()