albertohilal

Clase 10 ejercicio-06-A

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