pygame圆填充不正确

2024-09-30 22:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pygame和python做一个选择题游戏。我做了一个单独的程序来处理循环填充/取消填充,我基本上是把它放下。圆圈在上升的过程中会充满,但在下降的过程中不会。很难解释,我建议你运行代码来真正理解我的意思。任何帮助都将不胜感激!你知道吗

这是我的密码:

# Imports a library of functions!
import pygame
import random
# Initializes the game engine
pygame.init()
# Defines the colors
BLACK = (  0,   0,   0)
GREEN = (  3, 255,   3)
# Controls the width of the circle
width_1 = 2
width_2 = 2
width_3 = 2
width_4 = 2
# Un-fills circles
filled_1 = False
filled_2 = False
filled_3 = False
filled_4 = False
# Sets the height and width of the screen
size = [720, 575] 
screen = pygame.display.set_mode(size)
# Loops until the user clicks the close button
done = False
clock = pygame.time.Clock()
# While loop
while not done:
    # Leaves the fps at 30
    clock.tick(30)
    # Cleans the screen and sets the screen background
    screen.fill(GREEN)
    for event in pygame.event.get(): # If user did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            # Finds where you clicked            
            x, y = event.pos
            # Check if mouse was over it
            if circle_1.collidepoint(x, y):
                # Lets the circle draw
                filled_1 = True
                width_1 = 0
            elif circle_2.collidepoint(x, y):
                # Lets the circle draw
                filled_2 = True
                width_2 = 0              
            elif circle_3.collidepoint(x, y):
                # Lets the circle draw
                filled_3 = True
                width_3 = 0
            elif circle_4.collidepoint(x, y):
                # Lets the circle draw
                filled_4 = True
                width_4 = 0
    if filled_1 == True:
        filled_2 = False
        filled_3 = False
        filled_4 = False
        width_2 = 2
        width_3 = 2
        width_4 = 2
    elif filled_2 == True:
        filled_1 = False
        filled_3 = False
        filled_4 = False
        width_1 = 2
        width_3 = 2
        width_4 = 2
    elif filled_3 == True:
        filled_1 = False
        filled_2 = False
        filled_4 = False
        width_1 = 2
        width_2 = 2
        width_4 = 2
    elif filled_4 == True:
        filled_1 = False
        filled_2 = False
        filled_3 = False
        width_1 = 2
        width_2 = 2
        width_3 = 2
    # Circles
    circle_1 = pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1)
    circle_2 = pygame.draw.circle(screen, BLACK, [250, 260], 7, width_2)
    circle_3 = pygame.draw.circle(screen, BLACK, [250, 290], 7, width_3)
    circle_4 = pygame.draw.circle(screen, BLACK, [250, 320], 7, width_4)
    # Update the screen
    pygame.display.flip()   

Tags: theeventfalsetrueifwidthscreenpygame
1条回答
网友
1楼 · 发布于 2024-09-30 22:17:13

您必须将其他变量设置为False,当您处理正在按下的鼠标按钮时,这些变量不会被单击。例如,一旦filled_1被设置为True,没有任何东西会将它设置为False,因此第一个if语句将始终执行,而其他语句将被设置为False。你知道吗

也有更简单的方法来完成你想做的事情,我想你可能会受益于看一些Python documentation,它会使你的文章更简洁,易读,而且在很多情况下,更快。你知道吗

相关问题 更多 >