2017-12-08 19 views
1

私はpythonとpygamesで私の進歩を続けています。色が変わったときにマウスのクリック数を数えようとしています。何らかの理由で、0または1を数えるだけですが、それ以上は何もありません。Pygame - Countマウスクリック

if mouseClicked: 
    #I want to count the number of clicks when the color changes 
    if event.button == 1: 
     countMouseClick = countMouseClick + 1 
     #Change the window or screen display to a random integer mapped to the list. 
     displayScreen.fill(otherColors[random.randint(0,3)]) 

ここに私のコードです:

#Mouse click color changer. 
import pygame, random, sys 
from pygame.locals import * 
pygame.time.wait(1000) # 1000 ms = 1 sec. 
#Initializing variables 
#RGB =  R  G  B 
white = (255, 255, 255) 
black = (0,  0,  0) 
red  = (255, 0,  0) 
green = (0,  255, 0) 
blue = (0,  0, 255) 
otherColors = [black, red, green, blue] 
bgcolor = otherColors[random.randint(0,3)] 
width = 640 
height = 480 
fps  = 30 

#Create a function and call it main(). 
def main(): 
    pygame.init() 
    pygame.mixer.init() 
    fpsClock = pygame.time.Clock() 
    displayScreen = pygame.display.set_mode((width, height)) 
    pygame.display.set_caption('Mouse Click Color Changer') 
    colorBoard = getRandomizedColor(otherColors) 

    #Game Loop 
    while True: 
     #Process Input (Events)Step 
     mouseClicked = False 
     countMouseClick = 0 
     for event in pygame.event.get(): 
      if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): 
       print(countMouseClick) 
       pygame.quit() 
       sys.exit() 

      elif event.type == MOUSEBUTTONDOWN: 
       mouseClicked = True 

     #Update Step 
     if mouseClicked:   
      if event.button == 1: 
       countMouseClick = countMouseClick + 1    
       displayScreen.fill(otherColors[random.randint(0,3)]) 

     #Render (Draw) Step 
     #Use cases for .update() & .flip() (https://www.pygame.org/docs/tut/newbieguide.html) 
     pygame.display.update() 
     fpsClock.tick(fps) 

def getRandomizedColor(changeColor): 
    backgroundColor = [] 
    for color in otherColors: 
     backgroundColor.append(color) 
    changeColor = random.shuffle(backgroundColor) 
    return changeColor 

if __name__ == '__main__': 
    main() 

すべてのヘルプははるかに私が間違っているのかについても、説明を高く評価しています。

+1

あなたのコードに貼っておいてください:far too comment。あなたは "#変数をFalseにする"というコメントを持っていて、それからlineClick:mouseClicked = Falseです。それはわかりにくいです:/ – sadida

+0

'{}'ボタンを使ってコードを正しくフォーマットします。 'すべての行に'を使用しないでください。 – furas

+1

コードを見ずに、 – gdbj

答えて

2
while True: 
    countMouseClick = 0 
    # ... rest ... 

あなたはすべてのループ内で値をリセットこの方法です。あなたは前にそれを設定する必要がありますwhile

countMouseClick = 0 
while True: 
    # ... rest ...