2015-06-14 127 views
0

你好,我是Pygames的新手。所以我試圖在代碼中添加動畫(一隻貓從右到左在屏幕上漂移),但是我不知道我做錯了什麼。貓穿過屏幕,但它拖着一條白色或黑色的痕跡,我一直在努力解決這個問題。這是我的代碼到目前爲止。我知道你無法加載它,因爲圖像。我只想知道我在哪裏以及如何插入和編輯我的貓代碼,以便它不會穿過拖動白色條紋的循環穿過屏幕。無法讓pygame動畫正常工作

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() 

回答

0

你在一個稍微不同的位置繪製catSurfacescreen,然後再,再等等......

想象把貼紙上的一張紙上,然後又在一個稍微不同的位置,並再次等...

一旦你將Surface繪製到另一個,它停留在那裏。如果你在一個循環中稍微「移動」那個圖像,你會看到這種線索,實際上這只是你已經放在screenSurface上的圖像。

一個簡單的解決方案是在繪製所有其他圖像之前,在循環的每次迭代中只填充純色或背景圖像的screen