albertohilal

Clase 10 ejercicio-07-B

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