gpt4 book ai didi

python - Pygame - 计算鼠标点击次数

转载 作者:行者123 更新时间:2023-12-01 02:23:20 26 4
gpt4 key购买 nike

我正在继续使用 python 和 pygames 取得进展。我试图计算颜色变化时鼠标点击的次数。由于某种原因,它只计算 0 或 1,仅此而已。

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

非常感谢任何帮助,甚至解释我做错了什么。

最佳答案

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

这样您就可以在每个循环中重置值。您必须在 while

之前设置它
countMouseClick = 0
while True:
# ... rest ...

关于python - Pygame - 计算鼠标点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com