我终于发生了碰撞,所以当我的鼠标悬停在圆圈上并且你点击鼠标左键时,它会填充。它会变化,但是它会向下变化。
这是我的代码:
# 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()
只需运行此代码,看看会发生什么,很难解释。
答案 0 :(得分:38)
我们假设点击了第二个,所以
filled_1 = False
filled_2 = True
filled_3 = False
filled_4 = False
然后点击第三个。因此,
filled_1 = False
filled_2 = True
filled_3 = True
filled_4 = False
然后你有:
screen.fill(GREEN)
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:
# Not run
然后
elif filled_2 == True:
filled_1 = False
filled_3 = False
filled_4 = False
width_1 = 2
width_3 = 2
width_4 = 2
所以现在
filled_1 = False
filled_2 = True
filled_3 = False
filled_4 = False
这不是想要的!现在
elif filled_3 == True:
# Not run
elif filled_4 == True:
# Not run
所以点击第三个没有用!这是因为您不会存储订单,这是必需的。
我建议将此设置设置为false,放入点击处理程序中的部分:
if circle_1.collidepoint(x, y):
# Lets the circle draw
filled_1 = True
width_1 = 0
filled_2 = False
filled_3 = False
filled_4 = False
width_2 = 2
width_3 = 2
width_4 = 2
if filled_1 == True:
circle_1 = pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1)
# ... etc ...
效果很好。
现在,仍然存在问题。如果在第一次迭代中是事件,则不会定义circle_1
。这会使程序崩溃。您应该首先定义圈子 。
一些代码建议。
而不是
BLACK = ( 0, 0, 0)
GREEN = ( 3, 255, 3)
使用
pygame.Color("black")
pygame.Color(3, 255, 3)
他们为您存在!
而不是
thing_1 = ...
thing_2 = ...
...
other_thing_1 = ...
other_thing_2 = ...
...
有一个词典列表:
circles = [
{
"thing": ...,
"other_thing": ...
}, {
"thing": ...,
"other_thing": ...
},
...
]
您使用circles[1]["thing"]
代替thing_1
。
这使您可以简化"到:
import pygame
import random
pygame.init()
circles = [
{
"width": 2,
"filled": False,
"position": [250, 230]
}, {
"width": 2,
"filled": False,
"position": [250, 260]
}, {
"width": 2,
"filled": False,
"position": [250, 290]
}, {
"width": 2,
"filled": False,
"position": [250, 320]
},
]
size = [720, 575]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
# Check if mouse was over it
if circles[1]["bounding box"].collidepoint(x, y):
# Lets the circle draw
circles[1]["filled"] = True
circles[1]["width"] = 0
circles[2]["filled"] = False
circles[3]["filled"] = False
circles[4]["filled"] = False
circles[2]["width"] = 2
circles[3]["width"] = 2
circles[4]["width"] = 2
if circles[1]["filled"] == True:
circles[1]["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), [250, 230], 7, circles[1]["width"])
...
# Cleans the screen and sets the screen background
screen.fill(pygame.Color(3, 255, 3))
# Circles
circles[1]["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circles[1]["position"], 7, circles[1]["width"])
circles[2]["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circles[2]["position"], 7, circles[2]["width"])
circles[3]["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circles[3]["position"], 7, circles[3]["width"])
circles[4]["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circles[4]["position"], 7, circles[4]["width"])
# Update the screen
pygame.display.flip()
这看起来可能不简单,但它允许您使用循环而不是一遍又一遍地指定 :
import pygame
import random
pygame.init()
circles = [
{
"width": 2,
"filled": False,
"position": [250, 230]
}, {
"width": 2,
"filled": False,
"position": [250, 260]
}, {
"width": 2,
"filled": False,
"position": [250, 290]
}, {
"width": 2,
"filled": False,
"position": [250, 320]
},
]
size = [720, 575]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
while not done:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
for circle in circles:
# Check if mouse was over it
if circle["bounding box"].collidepoint(x, y):
# Lets the circle draw
circle["filled"] = True
circle["width"] = 0
for othercircle in circles:
if othercircle is not circle:
othercircle["filled"] = False
othercircle["width"] = 2
if circle["filled"] == True:
circle["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circle["position"], 7, circle["width"])
# Cleans the screen and sets the screen background
screen.fill(pygame.Color(3, 255, 3))
# Circles
for circle in circles:
circle["bounding box"] = pygame.draw.circle(screen, pygame.Color("black"), circle["position"], 7, circle["width"])
# Update the screen
pygame.display.flip()
有了这个设计:
问:你又做了什么来添加另一个圈子?
答:您将其添加到circles
。
问:你怎么做一个更大的圆圈?
答:您增加了size
属性。
问:你如何改变检查碰撞的方式?
答:您更改了一个地点碰撞。
看到好处? :)