Pygame Score per Click -
i create program whenever pressed mouse button, score incrementing 1.
here code far:
import pygame pygame.init() white = (255,255,255) display = pygame.display.set_mode((800, 400)) def count(): exit = true while exit: mouseclick = pygame.mouse.get_pressed() event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() quit() if event.type == pygame.mousebuttondown: global score score = 0 if mouseclick[0] == 1: score1=score+1 print (score1) else: print (score) display.fill(white) pygame.display.update() count()
the problem lies in way handle score:
if event.type == pygame.mousebuttondown: global score score = 0 if mouseclick[0] == 1: score1=score+1 print (score1) else: print (score) at every iteration, if click detected, reset score. it's equal 0.
just rid of score = 0 (and of global score well), , put right before main loop:
score = 0 while exit: ... now, when want increment score, don't need second variable. instead, write:
score += 1 which equivalent to:
score = score + 1 on other hand, code redefines new variable score1, , assign score + 1, equal 1. have score variable equal 0, , score1 variable equal 1.
a logical way handle of be:
score = 0 while exit: ... if event.type == pygame.mousebuttondown: if mouseclick[0] == 1: score += 1
Comments
Post a Comment