albertohilal

Clase 10 ejercicio-07-A

Dec 8th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. '''Programa los procedimientos y funciones necesarios para guardar el estado actual de la
  2. pantalla en un archivo \pantalla.png".
  3. Encontrara util el procedimiento save(pantalla,nombreDeArchivo)
  4. que forma parte del modulo image de PyGame.
  5. La siguiente direccion tiene informacion util al respecto: https://www.pygame.org/
  6. docs/ref/image.html
  7.  
  8. Cambie la forma de dibujar rect y ellipse, porque en esta etapa me
  9. resulta mas sencillo dominar las distintas dimensiones'''
  10.  
  11. import pygame, sys
  12. VERDE = (0, 255, 0)
  13. AZUL = (0,0,205)
  14. BLANCO = (255, 255, 255)
  15. X=0
  16. Y=1
  17. archivo = ("clase10.png")
  18. def ScreenSize():
  19. return (800,600)
  20.  
  21. def GuardarPantalla(pantalla, archivo):
  22. pygame.image.save(pantalla, archivo)
  23.  
  24. #aqui se define cual figura se dibuja
  25. #si el diccionario fig toma la clave "forma", dibuja un rectangulo
  26. #de lo contrario una elipse
  27. def Figura(pantalla, fig):
  28. if fig["forma"]:
  29. pygame.draw.rect(pantalla, AZUL,(fig["x"], fig["y"], fig["ancho"], fig["alto"]), 4)
  30. else:
  31. pygame.draw.ellipse(pantalla,VERDE,((fig["x"], fig["y"]), (fig["ancho"], fig["alto"])), 4)
  32.  
  33. #Definimos dibujar la figura, que recibe dos parametros, la pantalla y el estado
  34. # usa la funcion figura definida arriba que recibe por parametros la pantalla
  35. #y el estado de la figura actual definida por el valor del diccionario estado
  36. #entonces recorre un bucle for en el diccionario estado
  37. #y nos devuelve cada Figura ( rectangulo o elipse) impresa
  38. def DibujarFigura(pantalla, estado):
  39. Figura(pantalla, estado["figuraActual"])
  40. for figimpresa in estado["figurasImpresas"]:
  41. Figura(pantalla, figimpresa)
  42.  
  43.  
  44.  
  45.  
  46.  
  47. def ProcesarEventos(estado, pantalla):
  48. # Si no hay eventos, el nuevo estado sera igual al anterior
  49. figuraActual= estado["figuraActual"]
  50. figurasImpresas = estado["figurasImpresas"]
  51.  
  52. # los eventos pueden producir a la figura actual
  53. #modificacion de posicion, ancho y alto, cambio de figura, elipse por rectangulo
  54. #y viceversa, impresion de la figura actual
  55. for event in pygame.event.get():
  56. if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  57. pygame.quit()
  58. sys.exit()
  59. if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
  60. figuraActual = dict(figuraActual, x=figuraActual["x"] + 10)
  61. elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
  62. figuraActual = dict(figuraActual, x=figuraActual["x"] - 10)
  63. elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
  64. figuraActual = dict(figuraActual, y=figuraActual["y"] + 10)
  65. elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
  66. figuraActual = dict(figuraActual, y=figuraActual["y"] - 10)
  67.  
  68. elif event.type == pygame.KEYDOWN and event.key == pygame.K_2:
  69. figuraActual = dict(figuraActual, ancho=figuraActual["ancho"]+10)
  70. elif event.type == pygame.KEYDOWN and event.key == pygame.K_1:
  71. figuraActual = dict(figuraActual, ancho=figuraActual["ancho"]-10)
  72. elif event.type == pygame.KEYDOWN and event.key == pygame.K_3:
  73. figuraActual = dict(figuraActual, alto=figuraActual["alto"]+10)
  74. elif event.type == pygame.KEYDOWN and event.key == pygame.K_4:
  75. figuraActual = dict(figuraActual, alto=figuraActual["alto"]-10)
  76. elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
  77. figurasImpresas = figurasImpresas + [ dict(figuraActual) ]
  78. elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  79. # Invertir forma. Si circulo -> rectangulo. Si rectangulo -> circulo
  80. estado["figuraActual"]["forma"] = not estado["figuraActual"]["forma"]
  81.  
  82. elif event.type == pygame.MOUSEMOTION:
  83. figuraActual = dict( figuraActual, **{
  84. "x" : pygame.mouse.get_pos()[0],
  85. "y" : pygame.mouse.get_pos()[1]
  86. } )
  87.  
  88. #el nuevo estado esta definido por un diccionario
  89.  
  90. nuevoEstado = {
  91. "figuraActual" : figuraActual,
  92. "figurasImpresas" : figurasImpresas
  93. }
  94. return nuevoEstado
  95.  
  96.  
  97.  
  98. def Ejecutar():
  99. #estado esta definido por un dicionario que tiene por valores un diccionario y
  100. #una lista vacia que se ha de llenar
  101. estado = {
  102. "figuraActual":{"x": 80, "y":0,"ancho":240,"alto":240,"forma":False},
  103. "figurasImpresas":[]
  104. }
  105.  
  106. pygame.init()
  107. pantalla = pygame.display.set_mode(ScreenSize())
  108. clock = pygame.time.Clock()
  109. while True:
  110. estado = ProcesarEventos(estado,pantalla)
  111. pantalla.fill(BLANCO)
  112. DibujarFigura(pantalla,estado)
  113. GuardarPantalla(pantalla, archivo)
  114. pygame.display.flip()
  115. tick = clock.tick(60)
  116.  
  117.  
  118. if __name__ == "__main__":
  119. Ejecutar()
Advertisement
Add Comment
Please, Sign In to add comment