def dibujar_juego(juego): """ Esta función se encarga de dibujar la grilla del juego, los escombros que hay en la grilla """ gamelib.draw_rectangle(0, 0, ANCHO_INTERFAZ, ALTO_INTERFAZ, fill='black') # Dibujo panel superior dibujar_panel_superior(juego) # Dibujo grilla dibujar_grilla() # Dibujo robots. robots = juego[1] for robot in robots: x_celda, y_celda = robot[0], robot[1] gamelib.draw_image("media/robot.gif", ANCHO_Y_ALTO_CELDA * x_celda, MARGEN_SUPERIOR + ANCHO_Y_ALTO_CELDA * y_celda) # Dibujo escombros escombros = buscar_n_en_tablero(3, juego[2]) for i in range(len(escombros)): x_celda, y_celda = escombros[i][0], escombros[i][1] gamelib.draw_image("media/escombros.gif", ANCHO_Y_ALTO_CELDA * x_celda, MARGEN_SUPERIOR + ANCHO_Y_ALTO_CELDA * y_celda) # Dibujo jugador jugador = juego[0] x_celda, y_celda = jugador[0], jugador[1] gamelib.draw_image("media/astronauta.gif", 1 + ANCHO_Y_ALTO_CELDA * x_celda, MARGEN_SUPERIOR + ANCHO_Y_ALTO_CELDA * y_celda)
def mostrar_estado_juego(juego): gamelib.draw_begin gamelib.draw_rectangle(1,1,405,405, fill="black") gamelib.draw_line(401,0, 401, 400) gamelib.draw_text('CONTROLES:', 500, 30, size=10, fill='salmon') gamelib.draw_text('t = teletransportarse', 500, 60, size=8) gamelib.draw_text('a = mover izq', 500, 90, size=8) gamelib.draw_text('s = mover abajo', 500, 120, size=8) gamelib.draw_text('w = mover arriba', 500, 150, size=8) gamelib.draw_text('d = mover der',500, 180, size=8) gamelib.draw_text('e = mover arriba-der', 500, 210, size=8) gamelib.draw_text('q = mover arriba-izq', 500, 240, size=8) gamelib.draw_text('x = mover abajo-der:', 500, 270, size=8) gamelib.draw_text('z = mover abajo-izq:', 500, 300, size=8) gamelib.draw_text('NIVEL ACTUAL:', 500, 330, size=10, fill='salmon') gamelib.draw_text(f'{juego.mostrar_nivel()}', 500, 350, size=9) gamelib.draw_text('Teletransportes restantes: ', 500, 369, size=10, fill='salmon') gamelib.draw_text(f'{juego.teletransportes}', 500, 387, size=9) for fila in range(len(juego.grilla)): for columna in range(len(juego.grilla)): if juego.grilla[fila][columna] == 1: gamelib.draw_text("@", (columna * 20) + 10, (fila * 20) + 10, fill='gold') if juego.grilla[fila][columna] == 2: gamelib.draw_text("+", (columna * 20) + 10, (fila * 20) + 10, fill='red') if juego.grilla[fila][columna] == 3: gamelib.draw_text("*", (columna * 20) + 10, (fila * 20) + 10, fill='blue') gamelib.draw_end
def piezas(juego, siguiente): ''' Recibe un juego y la ficha siguiente. Del juego se dibuja la superficie consolidada y la ficha en juego. Por otra parte se dibuja la ficha siguiente en el corner superior derecho ''' grilla, pieza = juego superficie = tetris.buscar_elemento(juego, tetris.SUPERFICIE) # Dibujar cuadrados para ficha en juego for coordenada in pieza: cuadrado_pieza(coordenada, "blue") # Dibujar cuadrados para superficie for coordenada in superficie: cuadrado_pieza(coordenada, "green") # Dibujar siguiente pieza en un costado: gamelib.draw_text("Siguiente:", TABLERO_x1 + (TAM_PIEZA + 1) * tetris.ANCHO_JUEGO, TABLERO_y1, anchor="sw", fill="white") for coordenada in siguiente: x, y = coordenada gamelib.draw_rectangle( TABLERO_x1 + TAM_PIEZA * tetris.ANCHO_JUEGO + TAM_PIEZA + (x * TAM_PIEZA) , TABLERO_y1 + (y * TAM_PIEZA),\ TABLERO_x1 + TAM_PIEZA * tetris.ANCHO_JUEGO + TAM_PIEZA + TAM_PIEZA + (TAM_PIEZA * x), TABLERO_y1 + TAM_PIEZA + (TAM_PIEZA * y), fill = "white")
def dibujar_grilla(juego): """ Recibe el juego y dibuja el la grilla """ ancho, alto = tetris.dimensiones(juego) proporcio = alto / ancho grilla_ancho = RES / proporcio columna_ancho = grilla_ancho / ancho panel_ancho = RES - grilla_ancho margen = RES / 10 for columna in range(ancho): color = "#fff" if columna % 2 == 0: color = "#f4efe9" gamelib.draw_rectangle( (columna * columna_ancho), 0, ((columna + 1) * columna_ancho), RES, fill=color, outline=color, ) # Panel gamelib.draw_rectangle( grilla_ancho, 0, RES, RES, fill="#f4eee2", outline="#ecdbc4", width=2 ) # Texto gamelib.draw_text( "TETRIS", grilla_ancho + (panel_ancho / 2), margen, fill="#4f6dae", size=30 )
def cuadrado_pieza(coordenada_pieza, color): ''' Recibe la coordenada de una pieza y el color. La dibuja en el tablero segun su posicion y su tamanio relativo al tamanio de la pantalla ''' x, y = coordenada_pieza gamelib.draw_rectangle(TABLERO_x1 + (x * TAM_PIEZA) , TABLERO_y1 + (y * TAM_PIEZA),\ TABLERO_x1 + TAM_PIEZA + (TAM_PIEZA * x), TABLERO_y1 + TAM_PIEZA + (TAM_PIEZA * y), fill = color)
def dibujar_siguiente(pieza): gamelib.draw_text('Siguiente pieza:', 210, 30) for x, y in pieza: inicio = (250 + 15 * (x + 2), 5 + 10 * (y + 1)) final = (250 + 15 * (x + 3), 5 + 10 * (y + 2)) gamelib.draw_rectangle(inicio[0], inicio[1], final[0], final[1], outline='white', fill='red')
def main(): # Inicializar el estado del juego #siguiente_pieza = tetris.generar_pieza() siguiente = tetris.generar_pieza() t1= time.time() juego = tetris.crear_juego(tetris.generar_pieza()) ancho, alto = tetris.dimensiones(juego) lista_teclas = leer_teclas() gamelib.resize(800, 900) timer_bajar = ESPERA_DESCENDER while gamelib.loop(fps=30): gamelib.draw_begin() dibujar_superficie(juego) dibujar_pieza(juego) dibujar_siguiente(juego, siguiente) gamelib.draw_end() for event in gamelib.get_events(): if not event: break if event.type == gamelib.EventType.KeyPress: tecla = event.key if tecla == lista_teclas[1]: juego = tetris.mover(juego, -1) if tecla == lista_teclas[3]: juego = tetris.mover(juego, 1) if tecla == lista_teclas[5]: juego = tetris.mover(juego, 2) if tecla == lista_teclas[0]: return if tecla == lista_teclas[6]: juego = tetris.rotar(juego) if tecla == lista_teclas[4]: guardar_partida(juego) if tecla == lista_teclas[2]: juego = recuperar_partida() # Actualizar el juego, según la tecla presionada timer_bajar -= 1 if timer_bajar == 0: timer_bajar = ESPERA_DESCENDER juego, siguiente_pieza = tetris.avanzar(juego, siguiente) if siguiente_pieza: siguiente = tetris.generar_pieza() if tetris.terminado(juego): gamelib.draw_image('img/perdiste.gif', 50, 200) t2 = time.time() tiempo_final = t2- t1 gamelib.draw_rectangle(0, 0, 595, 60, outline='white', fill='salmon') gamelib.draw_text('Tu tiempo fue de {} segundos'.format(tiempo_final), 10, 17, fill='#000', size=18, anchor='nw') puntuaciones(tiempo_final) break while gamelib.is_alive(): ordenar_puntuaciones() event = gamelib.wait(gamelib.EventType.KeyPress)
def draw_paddle(state, p): W, H = SIZE x = PADDLE_GAP if p == 0 else H - PADDLE_GAP y = state.paddles[p] gamelib.draw_rectangle( x - PADDLE_WIDTH / 2, y - PADDLE_HEIGHT / 2, x + PADDLE_WIDTH / 2, y + PADDLE_HEIGHT / 2, fill='white', )
def tablero(puntaje_actual): ''' Dibuja el tablero relativo al tamanio de la pantalla y el puntaje en el corner derecho ''' gamelib.draw_image("images/tetris_4.gif", 0, 0) gamelib.draw_rectangle( TABLERO_x1 - 5 , TABLERO_y1 - 5,\ TABLERO_x1 + 5 + TAM_PIEZA * tetris.ANCHO_JUEGO, TABLERO_y1 + 5 + TAM_PIEZA * tetris.ALTO_JUEGO, fill = "grey") gamelib.draw_rectangle( TABLERO_x1, TABLERO_y1,\ TABLERO_x1 + TAM_PIEZA * tetris.ANCHO_JUEGO, TABLERO_y1 + TAM_PIEZA * tetris.ALTO_JUEGO, fill = "black") ## Puntaje!: gamelib.draw_text("Puntaje:", TABLERO_x1 - TAM_PIEZA * 2, TABLERO_y1 - TAM_PIEZA) gamelib.draw_text(puntaje_actual, TABLERO_x1 - TAM_PIEZA, TABLERO_y1)
def draw(life): gamelib.draw_begin() for y, row in enumerate(life): for x, cell in enumerate(row): if cell: gamelib.draw_rectangle( x * CELL_SIZE, y * CELL_SIZE, x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE + CELL_SIZE, fill='white', ) gamelib.draw_end()
def dibujar_superficie(juego): '''Se dibujara la grilla del tetris''' gamelib.draw_image('img/fondo6.gif', -80, 10) ancho, alto = tetris.dimensiones(juego) gamelib.draw_line(600, 250, 900, 250, fill='white', width=3) gamelib.draw_text('PRÓXIMA PIEZA:', 630, 10, fill='white', anchor='nw') for x in range (0, 600, 66): gamelib.draw_line(x, 0, x, 900, fill='white', width=2) for y in range (0, 900, 50): gamelib.draw_line(0, y, 595, y, fill='white', width=2) for y in range(alto): for x in range(ancho): if tetris.hay_superficie(juego, x, y): gamelib.draw_rectangle(x*66, y*50, (x+1)*66, (y+1)*50, outline='white', fill='purple')
def ordenar_puntuaciones(): gamelib.draw_rectangle(595, 250, 800, 900, outline='white', fill='#000') gamelib.draw_text('TOP MEJORES TIEMPOS: ', 600, 270, size=13, fill='#fff', anchor='nw') puntuaciones = puntuaciones_lista() tiempos = [] e = 350 a = 0 for i in range(len(puntuaciones)): tiempo = float(puntuaciones[i][1]) tiempos.append(tiempo) tiempos_ordenados = sorted(tiempos) for elemento in tiempos_ordenados: if a >= 10: return gamelib.draw_text(f'{elemento}, segs', 596, e, size=12, fill='#fff', anchor='nw') e+=50 a+=1
def dibujar_game_over(tabla_record): """ Dibuja la pantalla de game over cuando el juego termina """ margen = RES / 10 gamelib.draw_rectangle(0, 0, RES, RES, fill="#f4eee2", outline="#ecdbc4", width=2) gamelib.draw_text("GAME OVER", RES / 2, margen, fill="#4f6dae", size=30) for index, record in enumerate(tabla_record, start=1): nombre, puntos = record gamelib.draw_text( f"{index}- {nombre}: {puntos}", RES / 2, 30 * index + margen + 20, fill="#4f6dae", size=20, )
def dibujar_superficie(juego): """ Recibe el juego y dibuja la superficie """ ancho, alto = tetris.dimensiones(juego) proporcio = alto / ancho grilla_ancho = RES / proporcio lado_bloque = grilla_ancho / ancho for y in range(alto): for x in range(ancho): if tetris.hay_superficie(juego,x,y): gamelib.draw_rectangle( x * lado_bloque, y * lado_bloque, (x + 1) * lado_bloque, (y + 1) * lado_bloque, fill="gray", )
def dibujar_pieza(juego): """ Recibe el juego y dibuja la pieza actual """ ancho, alto = tetris.dimensiones(juego) proporcio = alto / ancho grilla_ancho = RES / proporcio lado_bloque = grilla_ancho / ancho posiciones = tetris.pieza_actual(juego) for posicion in posiciones: x, y = posicion gamelib.draw_rectangle( x * lado_bloque, y * lado_bloque, (x + 1) * lado_bloque, (y + 1) * lado_bloque, fill="red", )
def dibujar_piezas(juego): for i in juego.superficies[0]: inicio = juego.grilla[i][0] final = juego.grilla[i][1] gamelib.draw_rectangle(inicio[0], inicio[1], final[0], final[1], outline='white', fill="red") for n in juego.superficies[1]: inicio = juego.grilla[n][0] final = juego.grilla[n][1] gamelib.draw_rectangle(inicio[0], inicio[1], final[0], final[1], outline='white', fill="red")
def main(): gamelib.resize(300, 300) x, y = 150, 80 dx, dy = 5, 5 while gamelib.loop(): for event in gamelib.get_events(): if event.type == gamelib.EventType.KeyPress and event.key == 'q': return gamelib.draw_begin() gamelib.draw_rectangle(x - 10, y - 10, x + 10, y + 10, fill='red') gamelib.draw_end() x += dx y += dy if x > 300 or x < 0: dx *= -1 if y > 300 or y < 0: dy *= -1
def dibujar_game_over(): """ Esta función dibuja en pantalla un cartel que indica game over en pantalla una vez que ha terminado el juego y le pregunta al usuario si quiere volver a jugar. """ gamelib.draw_rectangle(0, 0, ANCHO_INTERFAZ, ALTO_INTERFAZ, fill='black') gamelib.draw_text('GAME OVER', ANCHO_INTERFAZ // 2, ALTO_INTERFAZ // 2 - MARGEN_SUPERIOR // 2, size=80, fill='red') gamelib.draw_image('media/boton.gif', ANCHO_INTERFAZ // 2 - MARGEN_SUPERIOR, ALTO_INTERFAZ // 2 + 50) gamelib.draw_text('Volver', ANCHO_INTERFAZ // 2, ALTO_INTERFAZ // 2 + 80, size=20) gamelib.draw_text('a jugar', ANCHO_INTERFAZ // 2, ALTO_INTERFAZ // 2 + 110, size=20)
def dibujar_siguiente(juego, pieza): """ Recibe el juego y la siguiente pieza Dibuja un panel para mostrarsela al usuario """ ancho, alto = tetris.dimensiones(juego) proporcio = alto / ancho grilla_ancho = RES / proporcio lado_bloque = grilla_ancho / ancho panel_ancho = RES - grilla_ancho eje_y_pieza = list(zip(*pieza))[0] bloques_fila = max(eje_y_pieza) + 1 centro_panel = grilla_ancho + panel_ancho / 2 - (bloques_fila * lado_bloque) / 2 margen = RES * 2 / 10 for posicion in pieza: x, y = posicion gamelib.draw_rectangle( (x * lado_bloque) + centro_panel, (y * lado_bloque) + margen, ((x + 1) * lado_bloque) + centro_panel, ((y + 1) * lado_bloque) + margen, fill="red", )
def dibujar_siguiente(juego, siguiente): '''Se dibujara la proxima pieza''' for x,y in siguiente: gamelib.draw_rectangle(602+x*66, 45+y*50, 602+(x+1)*66, 45+(y+1)*50, outline='white', fill='salmon')
def dibujar_pieza(juego): '''Se dibujara cada pieza del tetris''' pieza_actual = tetris.pieza_actual(juego) for x, y in pieza_actual: gamelib.draw_rectangle(x*66, y*50, (x+1)*66, (y+1)*50, outline='white', fill='red')