ball.py 683 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/python
  2. # using pygame or python gaming
  3. # https://www.pygame.org/wiki/GettingStarted
  4. import sys, pygame
  5. pygame.init()
  6. size = width, height = 900, 700
  7. speed = [2, 2]
  8. black = 0, 0, 0
  9. screen = pygame.display.set_mode(size)
  10. ball = pygame.image.load("intro_ball.gif")
  11. ballrect = ball.get_rect()
  12. while 1:
  13. for event in pygame.event.get():
  14. if event.type == pygame.QUIT: sys.exit()
  15. ballrect = ballrect.move(speed)
  16. if ballrect.left < 0 or ballrect.right > width:
  17. speed[0] = -speed[0]
  18. if ballrect.top < 0 or ballrect.bottom > height:
  19. speed[1] = -speed[1]
  20. screen.fill(black)
  21. screen.blit(ball, ballrect)
  22. pygame.display.flip()