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
- import sys
- BLANCO = (255,255,255)
- VERDE = (0,255,0)
- AZUL = (0,0,255)
- X = 0
- Y = 1
- def ScreenSize():
- return (800,600)
- def Pelota(pantalla, posicion, dimension):
- pygame.draw.ellipse (pantalla, VERDE, (posicion, dimension), 5)
- def Rectangulo(pantalla, posicion, dimension):
- pygame.draw.rect (pantalla, AZUL, (posicion, dimension), 5)
- def DibujarFigura(pantalla, posicion, dimension, forma):
- if forma == "pelota":
- forma = Pelota(pantalla, posicion, dimension)
- else:
- forma = Rectangulo(pantalla, posicion, dimension)
- def DibujarPantalla(pantalla, estado):
- DibujarFigura(pantalla, estado ["figuraActual"]["posicion"],estado ["figuraActual"]["dimension"], estado ["figuraActual"]["forma"])
- for figura in estado ["figurasImpresas"]:
- DibujarFigura (pantalla, figura ["posicion"], figura ["dimension"], figura["forma"])
- 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:
- # cambiar forma
- 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