albertohilal

Clase 10 ejercicio-06-B

Dec 8th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 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
  5. import sys
  6.  
  7. BLANCO = (255,255,255)
  8. VERDE = (0,255,0)
  9. AZUL = (0,0,255)
  10. X = 0
  11. Y = 1
  12. def ScreenSize():
  13. return (800,600)
  14.  
  15. def Pelota(pantalla, posicion, dimension):
  16. pygame.draw.ellipse (pantalla, VERDE, (posicion, dimension), 5)
  17.  
  18. def Rectangulo(pantalla, posicion, dimension):
  19. pygame.draw.rect (pantalla, AZUL, (posicion, dimension), 5)
  20.  
  21. def DibujarFigura(pantalla, posicion, dimension, forma):
  22.  
  23. if forma == "pelota":
  24. forma = Pelota(pantalla, posicion, dimension)
  25. else:
  26. forma = Rectangulo(pantalla, posicion, dimension)
  27.  
  28.  
  29. def DibujarPantalla(pantalla, estado):
  30. DibujarFigura(pantalla, estado ["figuraActual"]["posicion"],estado ["figuraActual"]["dimension"], estado ["figuraActual"]["forma"])
  31.  
  32. for figura in estado ["figurasImpresas"]:
  33. DibujarFigura (pantalla, figura ["posicion"], figura ["dimension"], figura["forma"])
  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. # cambiar forma
  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