albertohilal

Clase 10 Ejemplo 3.2.d

Oct 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import pygame
  2. import sys
  3. BLANCO = (255, 255, 255)
  4. ROJO = (255, 0, 0)
  5. X = 0
  6. Y = 1
  7. ABAJO = 1
  8. ARRIBA = -1
  9. GRAVEDAD = 1
  10. MEDIDA_PANTALLA = (640, 480)
  11. def Pelota(pantalla, posicion):
  12. pygame.draw.circle(pantalla, ROJO, posicion, 50)
  13.  
  14. def DibujarPantalla(pantalla, estado):
  15. Pelota(pantalla, estado["posicion"])
  16.  
  17. def actualizarEstado(estado):
  18. if estado["posicion"][Y] >= 480 - 50:
  19. # Si llega al Borde de pantalla
  20. dir = ARRIBA
  21. elif estado["velocidad"] <= 0:
  22. # Si se queda sin fuerza para seguir subiendo
  23. dir = ABAJO
  24. else:
  25. # De lo contrario, continuamos en la misma direccion
  26. dir = estado["direccion"]
  27.  
  28. vel = max(0, estado["velocidad"] + GRAVEDAD * dir)
  29. pos = (estado["posicion"][X], estado["posicion"][Y] + dir * vel)
  30.  
  31. # Retorno el nuevo estado
  32. return {
  33. "posicion" : pos,
  34. "direccion" : dir,
  35. "velocidad" : vel
  36. }
  37. def EjecutarCiclo():
  38. estado = {
  39. "posicion": (320, 240),
  40. "direccion": ABAJO,
  41. "velocidad": 1
  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. if __name__ == "__main__":
  53. EjecutarCiclo()
Advertisement
Add Comment
Please, Sign In to add comment