我正在用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()
答案 0 :(得分:0)
当您按下鼠标按钮时,必须将其他变量设置为False
,这些变量未被单击。例如,filled_1
设置为True
后,没有任何内容将其设置为False
,因此第一个if语句将始终执行,其他内容将被设置到False
。
还有更简单的方法来完成你想要做的事情,我认为你可能会从查看Python documentation中的某些内容中受益,它会使你更简洁,清晰,而且在很多情况下,更快。