Cannot get pygame animation to work properly

105 Views Asked by At

Hello I'm a newbie at Pygames. So I am trying to add animation ( a cat just drifting across the screen from right to left) in my code however I do not know what's I'm doing wrong. The cat goes across the screen but it drags a trail of white or black and I've been trying forever to solve this problem. This is my code so far. I know you can't load it because of the images. I just want to know where and how I can insert and edit my cat code so it won't go across the screen in a loop dragging a band of white.

import pygame

# Define some colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)
BROWN    = (  87,  52,  18)
BLUE     = (  17,  35, 107)

pygame.init()

# Character creation

def character(screen, x, y):

    #Head
    pygame.draw.circle (screen, BROWN, [20+x, 2+y], 10)
    pygame.draw.circle (screen, BLACK, [17+x, y], 1)
    pygame.draw.circle (screen, BLACK, [23+x, y], 1)

    #Coat
    pygame.draw.rect (screen, BLUE, [15+x,11+y, 10, 20])
    pygame.draw.line (screen,BLUE, [15+x,12+y], [x,20+y],5)
    pygame.draw.line (screen,BLUE, [23+x,12+y], [39+x,20+y],5)

    #Pants
    pygame.draw.line (screen,BLACK,[17+x,29+y],[10+x,49+y],5)
    pygame.draw.line (screen,BLACK,[22+x,29+y],[30+x,49+y],5)


# Set the width and height of the screen [width, height]
size = (500, 400)
screen = pygame.display.set_mode(size)

#Load pixel backgrounds

pygame.display.set_caption("My Game")

livingroom = pygame.image.load("Living Room Cropped.png")

dininghall = pygame.image.load("Dining Hall Cropped.png")

kitchen = pygame.image.load("Kitchen Cropped.png")

southhall = pygame.image.load("South Hall Cropped.png")

northhall = pygame.image.load("North Hall Cropped.png")

secretroom = pygame.image.load("Secret Library Cropped.png")

childbedroom = pygame.image.load("Children's Room Cropped.png")

masterbedroom = pygame.image.load("Master Bedroom Cropped.png")

washroom = pygame.image.load("Washroom Cropped.png")

cat = pygame.image.load("cat.png").convert()

#-----------------------

#image onto screen

#Backgound Updates:

screen.blit(livingroom, [0, 0])
myfont = pygame.font.SysFont("monospace", 15)
room = myfont.render("This is the living room. To find the secret library,", 2, BLACK)
screen.blit(room, (0, 300))
myfont = pygame.font.SysFont("monospace", 15)
room = myfont.render("you must look for where my son Hennry and my daughter", 2, BLACK)
screen.blit(room, (0, 325))
myfont = pygame.font.SysFont("monospace", 15)
room = myfont.render("Alice would sleep.", 2, BLACK)
screen.blit(room, (0, 350))
character(screen,200,200)


#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

catx = 550
screen.blit(cat, [catx,275])


# Coordinates of character for function
x = 250
y = 200


#List of Rooms

room_list = []

livingroom = ["This is the living room. To find the secret library,", "you must look for where my son Hennry and my daughter"," Alice would sleep.", 2, None, None, None,livingroom]
room_list.append(livingroom)

kitchen = ["You are in the kitchen. Ah the water is boiling perfect!","Now I can make tea. After tea why don't you look around?", "The library might just be in this room!", 3, 2, None, None,kitchen]
room_list.append(kitchen)

dininghall = ["This is the dining room. The room has a table in the","middle. After a meal I always get tired!. Next, find", "where my wife and I nap!", 4, None, 0, 1,dininghall]
room_list.append(dininghall)

secretroom = ["Ah You have found the secret library!", "I can finally return to being a human!","End", None, None, 1, None,secretroom]
room_list.append(secretroom)

southhall = ["You have entered the second hall, the south hall.","Oh, look it's already noon! I'm hungry! Find the place", "where my family dines.", 7, 5, 2, None,southhall]
room_list.append(southhall)

washroom = ["This is the bathroom. It's big and clean with a big tub.","After this bath let's go get some tea. I think I have","some water boiling on the stove.", 8, None, None, 4,washroom]
room_list.append(washroom)

childbedroom = ["You have entered my children's bedroom. They finally","cleaned their room! Next, find the place in the south","with beautiful red and gold carpet.", None, 7, None, None,childbedroom]
room_list.append(childbedroom)

northhall = ["This is the north hall. It has paintings of my ancestors on the walls, ", "I'm feeling up for a nice bath right now! Take me to", "the washroom please!", None, 8, 4, 6,northhall]
room_list.append(northhall)

masterbedroom = ["You have entered a bedroom. This is where my wife and I", "sleep. My grandparents also slept in this room. Find", "the hall with the painting of my grandparents.", None, None, 5, 7, masterbedroom]
room_list.append(masterbedroom)

current_room = 0



# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
#------------------------------------- North ---------------------------------------

            if event.key == pygame.K_UP:
                next_room = room_list[current_room][3]
                if next_room == None:
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render("You cannot go that way.", 1, BLACK)
                    screen.blit(room, (200, 250))
                else:
                    current_room = next_room
                    screen.blit(room_list[current_room][7],[0,0])
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][0], 5, BLACK)
                    screen.blit(room, (0, 300))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][1], 5, BLACK)
                    screen.blit(room, (0, 325))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][2], 5, BLACK)
                    screen.blit(room, (0, 350))
                    character(screen, 375,70)


#------------------------------------- East -----------------------------------------

            elif event.key == pygame.K_RIGHT:
                next_room = room_list[current_room][4]
                if next_room == None:
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render("You cannot go that way.", 1, BLACK)
                    screen.blit(room, (200, 250))
                else:
                    current_room = next_room
                    screen.blit(room_list[current_room][7], [0,0])
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][0], 1, BLACK)
                    screen.blit(room, (0, 300))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][1], 5, BLACK)
                    screen.blit(room, (0, 325))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][2], 5, BLACK)
                    screen.blit(room, (0, 350))
                    character(screen,375,65)

# ----------------------------------- South ----------------------------------------

            elif event.key == pygame.K_DOWN:
                next_room = room_list[current_room][5]
                if next_room == None:
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render("You cannot go that way.", 1, BLACK)
                    screen.blit(room, (200, 250))
                else:
                    current_room = next_room
                    screen.blit(room_list[current_room][7], [0,0])
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][0], 1, BLACK)
                    screen.blit(room, (0, 300))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][1], 5, BLACK)
                    screen.blit(room, (0, 325))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][2], 5, BLACK)
                    screen.blit(room, (0, 350))
                    character(screen,65,90)


#------------------------------------- West -----------------------------------------

            elif event.key == pygame.K_LEFT:
                 next_room = room_list[current_room][6]
                 if next_room == None:
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render("You cannot go that way.", 1, BLACK)
                    screen.blit(room, (200, 250))
                 else:
                    current_room = next_room
                    screen.blit(room_list[current_room][7],[0,0])
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][0], 1, BLACK)
                    screen.blit(room, (0, 300))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][1], 5, BLACK)
                    screen.blit(room, (0, 325))
                    myfont = pygame.font.SysFont("monospace", 15)
                    room = myfont.render(room_list[current_room][2], 5, BLACK)
                    screen.blit(room, (0, 350))
                    character(screen,250,180)

    if catx > -50:
        catx -= 1
    elif catx == -50:
        catx = 550

    screen.blit(cat, [catx,275])

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()


    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
1

There are 1 best solutions below

0
On

You draw the cat Surface onto the screen, then again at a slightly different position, and again etc...

Imagine putting a sticker on a sheet of paper, then another at a slightly different position, and again etc...

Once you draw a Surface onto another, it stays there. If you "move" that image slightly in a loop, you see this kind of trail, which in reality are just the images you already put onto the screen Surface.

A simple solution is to just fill the screen with a solid color or a background image once every iteration of your loop before drawing all other images.