import pygame pygame.init() ventana = pygame.display.set_mode((640,480)) pygame.display.set_caption("Ejemplo 2") # Creo o obxecto pelota ball = pygame.image.load("ball.png") # Obteño o rectángulo da pelota ballrect = ball.get_rect() # Marco os valores de velocidade cos que se vai mover a pelota speed = [8,8] # Poño a pelota na orixe de coordenadas ballrect.move_ip(0,0) jugando = True while jugando: for event in pygame.event.get(): if event.type == pygame.QUIT: jugando = False if event.type==pygame.KEYDOWN: if event.key==pygame.K_q: jugando=False # Movo a pelota ballrect = ballrect.move(speed) # Comprobo se a pelota chega aos límites da ventá e fago que rebote if ballrect.left < 0 or ballrect.right > ventana.get_width(): speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > ventana.get_height(): speed[1] = -speed[1] ventana.fill((252, 243, 207)) # Debuxo a pelota ventana.blit(ball, ballrect) pygame.display.flip() pygame.time.Clock().tick(60) pygame.quit()