无法使pygame动画正常工作

时间:2015-06-14 21:50:00

标签: python pygame

你好,我是Pygames的新手。所以我试图在我的代码中添加动画(一只猫从右到左漂移在屏幕上)然而我不知道我做错了什么。猫穿过屏幕,但它拖着一条白色或黑色的痕迹,我一直在努力解决这个问题。到目前为止这是我的代码。我知道你因为图像而无法加载它。我只是想知道在哪里以及如何插入和编辑我的猫代码,这样就不会在循环中拖动一条白色的光圈。

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 个答案:

答案 0 :(得分:0)

您将cat Surface绘制到screen上,然后再次位于稍微不同的位置,再次等等......

想象一下,在一张纸上贴一张贴纸,然后在另一张稍微不同的位置放一张贴纸等等......

一旦你将Surface画到另一个上,它就会停留在那里。如果你"移动"那个图像稍微在一个循环中,你会看到这种痕迹,实际上只是你已经放在screen Surface上的图像。

一个简单的解决方案是,在绘制所有其他图像之前,每次循环迭代时,只需用纯色或背景图像填充screen