0

I'm trying to create a animation that runs around a shape. The method I came up with is similar to the snake game, drawing the desired part and refresh it with white screen.

For example, if I want to draw a linear line animation, with the max animated length of 3:

lst = [1, 2, 3]
dummy_lst = [1, 1, 1, 2, 3, 3, 3]
colored_position = [[1,1,1], [1,1,2], [1,2,3], [2,3,3], [3,3,3]] # if the numbers overlapped, it draws the same place which is fine

and then I iterate the colored_position to get the animation done.

The real code:

import pygame
import sys

SCREEN_WIDTH = 800
CELLSIZE = 10

x, y = 150, 200

position = [[x+CELLSIZE*i, y] for i in range(50)] +\
           [[x+CELLSIZE*50, y+CELLSIZE*i] for i in range(50)] +\
           [[x+CELLSIZE*50-CELLSIZE*i, y+CELLSIZE*50]for i in range(50)] +\
           [[x, y+CELLSIZE*50-CELLSIZE*i]for i in range(50)]


# creates fake firsts and lasts
def dummy_list(list, length):
    return [list[0]]*(length-1) + list + [list[-1]]*(length-1)


def main():
    pygame.init()

    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_WIDTH))

    count = 0
    while True:
        clock.tick(10)

        screen.fill((255, 255, 255))
        dummy = dummy_list(position, 50)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit() 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    count = 0
                    while count < 1:   # I could chaget this to see many times it rotates
                        for i in range(len(dummy)-50+1):
                            for j in range(9): # change this to adjust how wide the colored part is 
                                pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(
                                    dummy[i+j][0], dummy[i+j][1], CELLSIZE, CELLSIZE), 0)
                                pygame.display.update()
                            screen.fill((255, 255, 255))
                        count += 1
        pygame.display.update()

But it seems very hard-coding. since I have to know each part of the desired position's Xs and Ys. What's the smarter way to complete the task?

3
  • You want to move the object along a path. How else do you want to define the path than by points?
    – Rabbid76
    Sep 19, 2021 at 17:17
  • I was thinking if there's build-in method to create this kind of movement, instead of having to hard-code this way. Let's say I want to create the same animation for a circle, it's gonna be troublesome. Also notice it has a "pause" between loops and I'd also like to fix that ( I mean it auto continue the second round without finishing the first one)
    – Cookie
    Sep 19, 2021 at 18:35
  • "also like to fix that" This is a new question. 1 question per post. Anyway this is unclear. If you want to move in a circle you must write a function that generates the points along a circle.
    – Rabbid76
    Sep 19, 2021 at 18:37

1 Answer 1

0

You want to move the object along a path. I don't see any problem in defining the path through a list of points.
However you can write a function that generates the list of points from a pygame.Rect object:

def generate_positions(rect, step):
    return [[x, rect.top] for x in range(rect.left, rect.right, step)] +\
           [[rect.right, y] for y in range(rect.top, rect.bottom, step)] +\
           [[x, rect.bottom] for x in range(rect.right, rect.left, -step)] +\
           [[rect.left, y] for y in range(rect.bottom, rect.top, -step)]

CELLSIZE = 10
x, y = 150, 200
position = generate_positions(pygame.Rect(x, y, 50*CELLSIZE, 50*CELLSIZE), CELLSIZE)

I recommend to use the application loop to draw the snake:

import pygame
import sys

SCREEN_WIDTH = 800

def generate_positions(rect, step):
    return [[x, rect.top] for x in range(rect.left, rect.right, step)] +\
           [[rect.right, y] for y in range(rect.top, rect.bottom, step)] +\
           [[x, rect.bottom] for x in range(rect.right, rect.left, -step)] +\
           [[rect.left, y] for y in range(rect.bottom, rect.top, -step)]

CELLSIZE = 10
x, y = 150, 200
position = generate_positions(pygame.Rect(x, y, 10*CELLSIZE, 10*CELLSIZE), CELLSIZE)
    
def main():
    pygame.init()

    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_WIDTH))

    current_point = None
    snake_len = 9
    while True:
        clock.tick(10)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit() 
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    current_point = -snake_len

        screen.fill((255, 255, 255))

        if current_point != None:
            for i in range(max(0, current_point), min(len(position), current_point+snake_len)):
                pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(*position[i], CELLSIZE, CELLSIZE), 0)
            current_point += 1
            if current_point >= len(position):
                current_point = None
            
        pygame.display.update()

main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.