albertohilal

Clase 10 Ejemplo 3.2.c

Oct 8th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4. BLANCO = (255, 255, 255)
  5. ROJO = (255, 0, 0)
  6. X = 0
  7. Y = 1
  8. ABAJO = 1
  9. ARRIBA = -1
  10. MEDIDA_PANTALLA = (640, 480)
  11.  
  12. def Pelota(pantalla, posicion):
  13. pygame.draw.circle(pantalla, ROJO, posicion, 50)
  14.  
  15. def DibujarPantalla(pantalla, estado):
  16. Pelota(pantalla, estado["posicion"])
  17.  
  18. def actualizarEstado(estado):
  19. if estado["posicion"][Y] >= 480 - 50:
  20. # Si llega al Borde de pantalla
  21. dir = ARRIBA
  22. elif estado["posicion"][Y] <= 240:
  23. # Si alcanza la posicion inicial
  24. dir = ABAJO
  25. else:
  26. # De lo contrario, continuamos en la misma direccion
  27. dir = estado["direccion"]
  28. vel = estado["velocidad"]
  29. pos = (estado["posicion"][X], estado["posicion"][Y] + dir * vel)
  30. # Retorno el nuevo estado
  31. return {
  32. "posicion" : pos,
  33. "direccion" : dir,
  34. "velocidad" : vel
  35. }
  36.  
  37. def EjecutarCiclo():
  38. estado = {
  39. "posicion": (320, 240),
  40. "direccion": ABAJO,
  41. "velocidad": 10
  42. }
  43. pygame.init()
  44. pantalla = pygame.display.set_mode( MEDIDA_PANTALLA )
  45. reloj = pygame.time.Clock()
  46. while True:
  47. estado = actualizarEstado(estado)
  48. pantalla.fill(BLANCO)
  49. DibujarPantalla(pantalla, estado)
  50. pygame.display.flip()
  51. tick = reloj.tick(60)
  52.  
  53. if __name__ == "__main__":
  54. EjecutarCiclo()
Advertisement
Add Comment
Please, Sign In to add comment