Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 在pygame中用鼠标画一条直线?_Python_Pygame - Fatal编程技术网

Python 在pygame中用鼠标画一条直线?

Python 在pygame中用鼠标画一条直线?,python,pygame,Python,Pygame,我正在创建一个类似于Flow的游戏。如果您熟悉它,它要求用户通过网格上相同颜色的线将相同的圆匹配在一起。我有一个问题,无论哪里有鼠标运动事件,我希望用户只能画直线左,右,上,下。目前,用户可以在任何地方绘制,我有一个函数,每当鼠标运动事件发生时,它都会绘制圆圈,尽管我使用了一些if语句尝试限制用户可以绘制的位置,但它仍然不起作用。也许有更好的方法,而不是使用鼠标运动事件?我不确定,但是非常感谢您的帮助 下面是一些代码: ''' *** IN PROGRESS *** ''' while not

我正在创建一个类似于Flow的游戏。如果您熟悉它,它要求用户通过网格上相同颜色的线将相同的圆匹配在一起。我有一个问题,无论哪里有鼠标运动事件,我希望用户只能画直线左,右,上,下。目前,用户可以在任何地方绘制,我有一个函数,每当鼠标运动事件发生时,它都会绘制圆圈,尽管我使用了一些if语句尝试限制用户可以绘制的位置,但它仍然不起作用。也许有更好的方法,而不是使用鼠标运动事件?我不确定,但是非常感谢您的帮助

下面是一些代码:

''' *** IN PROGRESS *** '''
while not done:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        done = True  # Closes the game and exits the loop

    elif event.type == pygame.MOUSEBUTTONDOWN:
        x, y = event.pos
        pygame.mouse.set_visible(True)
        ''' If the user clicks down on the left button the mouse coordinates at that             point are assigned to variables 
        x and y which are used to check the condition of the click_detection function'''

    elif event.type == pygame.MOUSEBUTTONUP:
        pygame.mouse.set_visible(True)
        # Makes the cursor visible to choose a new circle easily

    elif event.type == pygame.MOUSEMOTION:

        ''' State is assigned to an array for each of three mouse buttons
        at state[0] it checks the left mouse button'''
        state = pygame.mouse.get_pressed()

        for circle in circle_grid_list:
            # Checks following conditions for every circle in that Sprite group

            if ClickDetection.click_detection(circle) == True:
                ''' Checks whether a circle is being clicked
                - If so then variable colour is assigned to the colour of the clicked circle
                - This is used so that the move method from circle_line class can be called using the colour of the clicked circle'''
                colour = circle.colour

                # *** WORK IN PROGRESS ***
                if MovementChecker.check() != False:
                    print("can draw")
                    if state[0] == 1:
                        # Checking the left mouse button state, if 1 then button is being clicked or held down
                        moving_line_mouse.Move()

                elif MovementChecker.check() == False:
                    # Used to stop the move method for the moving_line object from being called if movement checker is false
                    print("Can't draw")

所以,与其考虑我的鼠标在哪里,不如试着想想我的鼠标在哪个盒子里?。如果是,则为该框着色。这可能有助于

例如,如果每个正方形为100x100像素,并且鼠标位于坐标424和651处,则可以通过执行int424/100和int651/100计算网格位置-鼠标位于网格上的位置4x6注意,我们从零开始计数,因此左上角的正方形位于位置0x0

然后,您可以检查网格4x6是否已经填充,检查它是否填充了不同的颜色,并根据需要处理所有逻辑

Flow还有一个功能,它将画一条线连接您输入的位置和离开的位置。如果你用另一种颜色剪切一条已经存在的线,它也会删除它的一半

实现这一点的一种方法是创建一个正方形对象,该对象跟踪其当前颜色、对来自的正方形的引用以及对最终拾取的正方形的引用


这样,您就在网格中创建了一种链表——每个正方形都指向它所连接的下一个正方形,这样您就知道是需要绘制垂直条、水平条还是角点。如果剪切已设置颜色的正方形,可以继续沿着参照链查找前一种颜色经过的下一个正方形,在继续之前,请将其设置为空。

我从来没有听说过任何Python游戏的名称。您能否提供该游戏代码的链接以便我们查看?检查您的鼠标是否移动到水平或垂直网格位置之外,如果移动到水平或垂直网格位置,请拒绝。将delta鼠标x和y除以网格元素的大小;其中只有一个可能是1。这真的很聪明!我已经想出了如何让它画出我想要的线,根据它点击我渲染圆的位置区域,如果它点击圆的半径大约50x50+/-40,那么我知道它点击了那个圆,它画出了那个颜色。您知道连接现有线路的功能是什么吗?因为我现在正在做的是在每个鼠标运动事件上画一个圆圈,但这个圆圈不起作用。谢谢你的帮助!