简体   繁体   中英

Drawing a straight line with the mouse in pygame?

I am creating a similar game to Flow. If you're familiar with it it requires the user to match circles of the same together through lines of the same color on a grid. I am having a problem where, wherever there is a Mouse motion event I would like the user to only be able to draw in straight lines (left, right, up, down). Currently the user can draw anywhere, I have a function which draws circles whenever the mouse motion event happens and although I have used some if statements to try and restrict where the user can draw it still is not working. Perhaps there is a better way rather using mouse motion events? Am not sure but any help is greatly appreciated!

Here is some of the code:

''' *** 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")

So, rather then thinking in terms of "is this where my mouse is located", try and think in terms of "which box is my mouse located in?". If it is, then color that box. It might help to

For example, if each of your squares is 100x100 pixels, and your mouse is located at coordinates 424 and 651, then you can compute the grid location by doing int(424 / 100) and int(651 / 100) -- your mouse is located at position 4x6 on the grid (note that we're counting from zero, so the square in the upper-left corner is at position 0x0).

Then, you can check if grid 4x6 is already filled or not, check if it's filled with a different color, and handle all the logic according.

Flow also has a feature where it'll draw a line connecting where you've entered and where you left. It'll also delete half of a pre-existing line if you "cut" through it with another color.

One way of implementing this would be to create a "square" object that keeps track of its current color, a reference to the square that you came from, and a reference to the square that you ended up picking.

That way, you're creating a sort of "linked list" inside your grid -- each square points to the next square it's connected to, so you know whether you need to draw a vertical bar, a horizontal bar, or a corner. If you cut through a square that has already set the color, you can keep following the chain of references to find the next square that the previous color had flowed through, and set those back to empty before continuing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM