gpt4 book ai didi

python - pygame圈子没有改变

转载 作者:太空狗 更新时间:2023-10-30 01:39:21 24 4
gpt4 key购买 nike

我终于解决了碰撞问题,当我的鼠标悬停在圆圈上并单击鼠标左键时,它会填充。它会向上变化,但向下不会。

这是我的代码:

# 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()

只需运行这段代码,看看会发生什么,这很难解释。

最佳答案

假设点击了第二个,那么

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()

采用这种设计:

问:再添一个圈子怎么办?
A:你把它添加到圈子

问:你怎么做才能使圆变大?
A: 你增加它的 size 属性。

问:你们如何改变检查碰撞的方式?
A:你改变了检查碰撞的一个地方。


看到好处了吗? :)

关于python - pygame圈子没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23847175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com