Skip to content Skip to sidebar Skip to footer

Pygame Rotate Line Delete Old Line

How can I rotate a line in pygame using math module and every second rotate line delete old line. I've just used other code but the problem was old line rotate and so I watched an

Solution 1:

To easily assist you with a specific answer for your problem, you really need to show your code. Please see how to ask.

I've prepared a generic example that randomises one end point of a line when a mouse button is clicked.

# pyg_line_demoimport pygame
import random

defget_random_position():
    """return a random (x,y) position in the screen"""return (random.randint(0, screen_width - 1),  #randint includes both endpoints.
            random.randint(0, screen_height - 1)) 

defrandom_line(line):
    """ Randomise an end point of the line"""if random.randint(0,1):
        return [line[0], get_random_position()]
    else:
        return [get_random_position(), line[-1]]


# initialisation
pygame.init()
screen_width, screen_height = 640, 480
surface = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Lines')
clock = pygame.time.Clock() #for limiting FPS
FPS = 30# initial line
line = [(10, 10), (200, 200)]

finished = Falsewhilenot finished:
    for event in pygame.event.get():            
        if event.type == pygame.QUIT:
            finished = Trueif event.type == pygame.MOUSEBUTTONDOWN:
            line = random_line(line)
    #redraw background
    surface.fill(pygame.Color("white"))
    #draw line
    pygame.draw.aalines(surface, pygame.Color("blue"), False, line)
    # update display
    pygame.display.update()
    #limit framerate
    clock.tick(FPS)
pygame.quit()

You should be able to insert your line rotation function in place of the random_line() function.

Let us know if you have any further questions.

Random lines example

Post a Comment for "Pygame Rotate Line Delete Old Line"