En este video damos movimiento a un rectangulo usando las flechas del teclado, modificando nuestro codigo anterior de Pygame.
#importamos modulosimport pygame, sysfrom pygame.locals import *from random import randint
#init antes de usar pygamepygame.init()#declaramos ventana con alto anchoventana=pygame.display.set_mode((500,400))#titulopygame.display.set_caption("Animacion")#variablescolorFondo=(25,150,70)colorRectangulo=(255,255,255)velocidad=10
posX,posY=randint(1,400),randint(1,300)#bucle ejecucion ventanawhile True: ventana.fill(colorFondo) pygame.draw.rect(ventana,colorRectangulo,(posX,posY,70,40)) #control de eventos for evento in pygame.event.get(): if evento.type==QUIT: pygame.quit() sys.exit() #actualizamos segun pulse tecla flechas elif evento.type==pygame.KEYDOWN: if evento.key==K_LEFT: posX-=velocidad if posX<0: posX=0 elif evento.key==K_RIGHT: posX+=velocidad if posX>(500-70): posX=500-70 elif evento.key==K_UP: posY-=velocidad if posY<0: posY=0 elif evento.key==K_DOWN: posY+=velocidad if posY>360: posY=360 #actualiza ventana pygame.display.update()