Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''Agrega los eventos necesarios para que la figura a dibujar se ubique en la posicion del
- mouse y que se imprima haciendo click.'''
- import pygame, sys
- VERDE = (0, 255, 0)
- AZUL = (0,0,205)
- BLANCO = (255, 255, 255)
- X=0
- Y=1
- def ScreenSize():
- return (800,600)
- #aqui se define cual figura se dibuja
- #si el diccionario fig toma la clave "forma", dibuja un rectangulo
- #de lo contrario una elipse
- def Figura(pantalla, fig):
- if fig["forma"]:
- pygame.draw.rect(pantalla, AZUL,(fig["x"], fig["y"], fig["ancho"], fig["alto"]), 4)
- else:
- pygame.draw.ellipse(pantalla,VERDE,((fig["x"], fig["y"]), (fig["ancho"], fig["alto"])), 4)
- #Definimos dibujar la figura, que recibe dos parametros, la pantalla y el estado
- # usa la funcion figura definida arriba que recibe por parametros la pantalla
- #y el estado de la figura actual definida por el valor del diccionario estado
- #entonces recorre un bucle for en el diccionario estado
- #y nos devuelve cada Figura ( rectangulo o elipse) impresa
- def DibujarFigura(pantalla, estado):
- Figura(pantalla, estado["figuraActual"])
- for figimpresa in estado["figurasImpresas"]:
- Figura(pantalla, figimpresa)
- def ProcesarEventos(estado, pantalla):
- # Si no hay eventos, el nuevo estado sera igual al anterior
- figuraActual= estado["figuraActual"]
- figurasImpresas = estado["figurasImpresas"]
- # los eventos pueden producir a la figura actual
- #modificacion de posicion, ancho y alto, cambio de figura, elipse por rectangulo
- #y viceversa, impresion de la figura actual
- for event in pygame.event.get():
- if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
- figuraActual = dict(figuraActual, x=figuraActual["x"] + 10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
- figuraActual = dict(figuraActual, x=figuraActual["x"] - 10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
- figuraActual = dict(figuraActual, y=figuraActual["y"] + 10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
- figuraActual = dict(figuraActual, y=figuraActual["y"] - 10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_2:
- figuraActual = dict(figuraActual, ancho=figuraActual["ancho"]+10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_1:
- figuraActual = dict(figuraActual, ancho=figuraActual["ancho"]-10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_3:
- figuraActual = dict(figuraActual, alto=figuraActual["alto"]+10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_4:
- figuraActual = dict(figuraActual, alto=figuraActual["alto"]-10)
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
- figurasImpresas = figurasImpresas + [ dict(figuraActual) ]
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- # Invertir forma. Si circulo -> rectangulo. Si rectangulo -> circulo
- estado["figuraActual"]["forma"] = not estado["figuraActual"]["forma"]
- elif event.type == pygame.MOUSEMOTION:
- figuraActual = dict( figuraActual, **{
- "x" : pygame.mouse.get_pos()[0],
- "y" : pygame.mouse.get_pos()[1]
- } )
- #el nuevo estado esta definido por un diccionario
- nuevoEstado = {
- "figuraActual" : figuraActual,
- "figurasImpresas" : figurasImpresas
- }
- return nuevoEstado
- def Ejecutar():
- #estado esta definido por un dicionario que tiene por valores un diccionario y
- #una lista vacia que se ha de llenar
- estado = {
- "figuraActual":{"x": 80, "y":0,"ancho":240,"alto":240,"forma":False},
- "figurasImpresas":[]
- }
- pygame.init()
- pantalla = pygame.display.set_mode(ScreenSize())
- clock = pygame.time.Clock()
- while True:
- estado = ProcesarEventos(estado,pantalla)
- pantalla.fill(BLANCO)
- DibujarFigura(pantalla,estado)
- pygame.display.flip()
- tick = clock.tick(60)
- if __name__ == "__main__":
- Ejecutar()
Advertisement
Add Comment
Please, Sign In to add comment