2017-12-08 7 views
1

저는 파이썬과 파이 게임으로 계속해서 진전을 이루고 있습니다. 색상이 바뀔 때 마우스 클릭 수를 계산하려고합니다. 웬일인지, 그것은 단지 0 또는 1을 세지 만, 그 이상은 아니다. 내가 잘못 뭐하는 거지에파이 게임 - 마우스 클릭 수

#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() 

어떤 도움이 많이도 설명을 감사 :

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)]) 

여기 내 코드입니다.

+1

코드에 대한 제안 : 지나치게 많은 의견. "# 변수를 False로 지정하십시오"라는 주석이 있고 lineClick : mouseClicked = False입니다. 난독 화입니다 :/ – sadida

+0

'{}'버튼을 사용하여 코드의 형식을 올바르게 지정하십시오.'모든 행에 대해'를 사용하지 마십시오. – furas

+1

코드를 보지 않고'countMouseClick'을 만들 때마다 – gdbj

답변

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

이렇게하면 모든 루프에서 값을 재설정 할 수 있습니다. 미리 설정해야합니다 while

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