我很困惑。我目前的主要目标是使用户单击一个圆圈,并在每次单击时使屏幕左上方的数字增加一个(尝试制作基本的答题器游戏)。我已经知道,当您单击圆圈时,数字确实会上升!除了人数增加不止一次。这是代码所在的函数:
def bubble():
orange = (255, 165, 0)
dark_orange = (255, 140, 0)
bubble_color = dark_orange
bubble_x = 300
bubble_y = 400
bubble_pos = (bubble_x, bubble_y)
bubble_rad = 100
mouse_x, mouse_y = pygame.mouse.get_pos()
distance = math.hypot(bubble_x - mouse_x, bubble_y - mouse_y)
if bubble_rad >= distance:
bubble_color = orange
if bubble_rad >= distance and event.type == pygame.MOUSEBUTTONDOWN:
global kill_counter
kill_counter += 1
pygame.draw.circle(screen, bubble_color, bubble_pos, bubble_rad)
我曾尝试将mousebuttondown更改为mousebutton,但仅在单击后才发生相同的问题(当我移动鼠标时,它会停止)。接下来,我尝试执行此操作:
if bubble_rad >= distance and event.type == pygame.MOUSEBUTTONDOWN and event.type == pygame.MOUSEBUTTONUP
没用,这是有道理的,因为鼠标不能同时上下移动。有没有一种方法可以使kill_counter仅在单击鼠标然后取消单击时才会上升?这是我的整个代码,以防问题出在这里。
import math
pygame.init()
# General
display_width = 600
display_height = 800
background = pygame.image.load("background.png")
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("SLASHR")
pygame.display.set_icon(pygame.image.load("knifeart.png"))
# Global variables
kill_counter = 0
# Functions
def counter():
font = pygame.font.Font("freesansbold.ttf", 52)
crimson = (220, 20, 60)
screen.blit(font.render("Kill Count: " + str(kill_counter), True, crimson), (0, 10))
def bubble():
orange = (255, 165, 0)
dark_orange = (255, 140, 0)
bubble_color = dark_orange
bubble_x = 300
bubble_y = 400
bubble_pos = (bubble_x, bubble_y)
bubble_rad = 100
mouse_x, mouse_y = pygame.mouse.get_pos()
distance = math.hypot(bubble_x - mouse_x, bubble_y - mouse_y)
if bubble_rad >= distance:
bubble_color = orange
if bubble_rad >= distance and event.type == pygame.MOUSEBUTTONDOWN:
global kill_counter
kill_counter += 1
pygame.draw.circle(screen, bubble_color, bubble_pos, bubble_rad)
# Game Loop
running = True
while running:
screen.fill((255, 255, 255))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
counter()
bubble()
pygame.display.update()
感谢您阅读和帮助我!
if event.type == MOUSEBUTTONDOWN
应该在for event in pygame.event.get()
循环中,我想在循环之后,事件仍然是MOUSEBUTTONDOWN并且没有重置,尝试:
# Game Loop
running = True
click = False #have click variable
while running:
screen.fill((255, 255, 255))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
click = True
然后在冒泡功能中:
def bubble(): #other code if bubble_rad >= distance and click: global kill_counter kill_counter += 1