python - Cannot get pygame animation to work properly -
hello i'm newbie @ pygames. trying add animation ( cat drifting across screen right left) in code not know what's i'm doing wrong. cat goes across screen drags trail of white or black , i've been trying forever solve problem. code far. know can't load because of images. want know , how can insert , edit cat code won't go across screen in loop dragging band of white.
import pygame # define 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 width , height of 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 living room. find secret library,", 2, black) screen.blit(room, (0, 300)) myfont = pygame.font.sysfont("monospace", 15) room = myfont.render("you must son hennry , daughter", 2, black) screen.blit(room, (0, 325)) myfont = pygame.font.sysfont("monospace", 15) room = myfont.render("alice sleep.", 2, black) screen.blit(room, (0, 350)) character(screen,200,200) #loop until user clicks close button. done = false # used manage how fast screen updates clock = pygame.time.clock() catx = 550 screen.blit(cat, [catx,275]) # coordinates of character function x = 250 y = 200 #list of rooms room_list = [] livingroom = ["this living room. find secret library,", "you must son hennry , daughter"," alice sleep.", 2, none, none, none,livingroom] room_list.append(livingroom) kitchen = ["you in kitchen. ah water boiling perfect!","now can make tea. after tea why don't around?", "the library might in room!", 3, 2, none, none,kitchen] room_list.append(kitchen) dininghall = ["this dining room. room has table in the","middle. after meal tired!. next, find", "where wife , nap!", 4, none, 0, 1,dininghall] room_list.append(dininghall) secretroom = ["ah have found secret library!", "i can return being human!","end", none, none, 1, none,secretroom] room_list.append(secretroom) southhall = ["you have entered second hall, south hall.","oh, it's noon! i'm hungry! find place", "where family dines.", 7, 5, 2, none,southhall] room_list.append(southhall) washroom = ["this bathroom. it's big , clean big tub.","after bath let's go tea. think have","some water boiling on stove.", 8, none, none, 4,washroom] room_list.append(washroom) childbedroom = ["you have entered children's bedroom. finally","cleaned room! next, find place in south","with beautiful red , gold carpet.", none, 7, none, none,childbedroom] room_list.append(childbedroom) northhall = ["this north hall. has paintings of ancestors on walls, ", "i'm feeling nice bath right now! take me to", "the washroom please!", none, 8, 4, 6,northhall] room_list.append(northhall) masterbedroom = ["you have entered bedroom. wife , i", "sleep. grandparents slept in room. find", "the hall painting of grandparents.", none, none, 5, 7, masterbedroom] room_list.append(masterbedroom) current_room = 0 # -------- main program loop ----------- while not done: # --- main event loop 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 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 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 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 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 , update screen we've drawn. pygame.display.flip() # --- limit 60 frames per second clock.tick(60) # close window , quit. # if forget line, program 'hang' # on exit if running idle. pygame.quit()
you draw cat
surface
onto screen
, again @ different position, , again etc...
imagine putting sticker on sheet of paper, @ different position, , again etc...
once draw surface
onto another, stays there. if "move" image in loop, see kind of trail, in reality images put onto screen
surface
.
a simple solution fill screen
solid color or background image once every iteration of loop before drawing other images.
Comments
Post a Comment