Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 游戏圈没有改变_Python_Pygame_Mouse_Fill - Fatal编程技术网

Python 游戏圈没有改变

Python 游戏圈没有改变,python,pygame,mouse,fill,Python,Pygame,Mouse,Fill,我终于解决了碰撞问题,所以当我的鼠标悬停在圆圈上时,你点击鼠标左键,它就会填充。上升时会变,但下降时不会变 这是我的密码: # 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 th

我终于解决了碰撞问题,所以当我的鼠标悬停在圆圈上时,你点击鼠标左键,它就会填充。上升时会变,但下降时不会变

这是我的密码:

# 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)
它会(暂时)画两个黑色的圆圈

然后你要:

然后

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)
使用

他们为你而存在

而不是

thing_1 = ...
thing_2 = ...
...

other_thing_1 = ...
other_thing_2 = ...
...
有一个字典列表:

circles = [
    {
        "thing": ...,
        "other_thing": ...
    }, {
        "thing": ...,
        "other_thing": ...
    },
    ...
]
使用
圆圈[1][“thing”]
而不是
thing\u 1

这允许您“简化”到:

这看起来可能并不简单,但它允许您使用循环,而不是反复指定:


采用这种设计:

问:如何添加另一个圆
答:您将其添加到
圆圈中

问:你怎么做才能使圆变大
答:您增加了它的
大小
属性

问:您如何改变检查碰撞的方式
答:您更改了检查碰撞的一个位置



看到好处了吗?:)

一个最小的、可运行的样本会带来奇迹。它可能适合你,但不适合我。去掉问题出现时不需要的所有内容(如果只需要一个圆圈,则只保留一个圆圈;如果只需要
if
的那部分,则始终保持为true并删除复选框),但保留所有需要的内容(例如,保留事件循环和显示更新)。好的,现在我明白了。我马上就去那太好了!现在您知道错误可能在哪里了,所以可以开始调试它了!从完整的代码开始,解决问题。慢慢地删除不重要的部分,直到你得到这个问题的代码。在某个地方,它会在行为之间切换。这就是问题所在。然后你可以改进你的问题。仅供参考:这个问题在meta上讨论。非常简单,谢谢你的额外建议。不过有一个问题,边界框是做什么的?
circles[X][“bounding box”]
相当于您的
circle\ux
。因此,当您绘制圆时返回的是
pygame.Rect
。我称它为“边界框”,因为它实际上是一个正方形,而不是你说的圆形。如果将圆变大,可以看到可以在圆外单击,因为正方形在圆外有角。
thing_1 = ...
thing_2 = ...
...

other_thing_1 = ...
other_thing_2 = ...
...
circles = [
    {
        "thing": ...,
        "other_thing": ...
    }, {
        "thing": ...,
        "other_thing": ...
    },
    ...
]
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()