我终于把碰撞降下来了,这样当我的鼠标悬停在圆圈上,你点击鼠标左键,它就会填满。上升时会改变,但下降时不会
我的代码是:
# 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)
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
if filled_1 == True:
circle_1 = pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1)
elif circle_2.collidepoint(x, y):
# Lets the circle draw
filled_2 = True
width_2 = 0
if filled_2 == True:
circle_2 = pygame.draw.circle(screen, BLACK, [250, 260], 7, width_2)
elif circle_3.collidepoint(x, y):
# Lets the circle draw
filled_3 = True
width_3 = 0
if filled_3 == True:
circle_3 = pygame.draw.circle(screen, BLACK, [250, 260], 7, width_3)
elif circle_4.collidepoint(x, y):
# Lets the circle draw
filled_4 = True
width_4 = 0
if filled_4 == True:
circle_4 = pygame.draw.circle(screen, BLACK, [250, 260], 7, width_4)
# Cleans the screen and sets the screen background
screen.fill(GREEN)
# 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)
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
# Update the screen
pygame.display.flip()
运行这段代码,看看会发生什么,这很难解释。在
假设第二个被点击了,所以
然后单击第三个。因此
^{pr2}$然后你有:
它会(临时地)画出两个黑色的圆圈。在
你要做的是:
然后呢
所以现在
这不是要的!现在
所以点击第三个没用!这是因为您没有存储所需的订单。在
我建议将此设置移到click处理程序内部的部件中:
那很好用。在
现在,仍然存在一个问题。如果在第一次迭代中是事件,
circle_1
将不会被定义。这会使程序崩溃。你应该先定义圆。在一些代码建议。在
而不是
使用
他们为你而存在!在
而不是
列出字典:
其中使用
circles[1]["thing"]
而不是thing_1
。在这使您可以“简化”为:
这看起来并不简单,但它允许您使用循环,而不是一遍又一遍地指定的内容:
采用这种设计:
问:你怎么再加一个圆?
A: 将其添加到
circles
。在问:怎样才能使一个圆变大?
A: 增加它的
size
属性。在问:如何改变检查碰撞的方式?
A: 更改一个位置碰撞被选中。在
看到好处了吗?:)
相关问题 更多 >
编程相关推荐