Advertisement
acidxp4in

main.py

May 30th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.01 KB | None | 0 0
  1. #!usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import pygame, sys, time
  5.  
  6. from pygame.locals import *
  7. from random import choice, randint
  8. from configuracion import *
  9. from constantes import *
  10. from utilidades import *
  11.  
  12. #Se importan las funciones del modulo funciones.py
  13. from funciones import *
  14.  
  15. # Se inicializan los modulos de pygame.
  16. pygame.init()
  17.  
  18. # Se crea la ventana de juego.
  19. ventana = pygame.display.set_mode(DIMENSIONES)
  20. pygame.display.set_caption(TITULO_VENTANA)
  21. pygame.mouse.set_visible(False)
  22.  
  23. # Se crean y se cargan los graficos del juego.
  24. fondo_juego = pygame.image.load("img/"+FONDO_JUEGO).convert()
  25. if fondo_juego.get_size() != DIMENSIONES:
  26.     fondo_juego = pygame.transform.scale(fondo_juego, DIMENSIONES)
  27.  
  28. #se cargan los graficos de los barcos
  29.  
  30. imagenes_barcos = dict()
  31.  
  32. for tipo, archivo in IMAGENES_BARCOS.items():
  33.     imagen = pygame.image.load("img/"+archivo).convert_alpha()
  34.  
  35.     if imagen.get_size() != (100, 50):
  36.         imagen = pygame.transform.scale(imagen, (100, 50))
  37.        
  38.     imagenes_barcos[tipo] = imagen
  39.  
  40. # Creacion del cursor.
  41.  
  42. imagen_cursor = pygame.Surface((RADIO_CURSOR * 2, RADIO_CURSOR * 2))
  43. imagen_cursor.fill(NEGRO)
  44. imagen_cursor.set_colorkey(NEGRO)
  45. pygame.draw.circle(imagen_cursor, ROJO, (RADIO_CURSOR, RADIO_CURSOR), RADIO_CURSOR, 2)
  46. pygame.draw.line(imagen_cursor, ROJO, (0, RADIO_CURSOR), (RADIO_CURSOR - 5, RADIO_CURSOR), 2)
  47. pygame.draw.line(imagen_cursor, ROJO, (RADIO_CURSOR + 5, RADIO_CURSOR), (RADIO_CURSOR * 2, RADIO_CURSOR), 2)
  48. pygame.draw.line(imagen_cursor, ROJO, (RADIO_CURSOR, 0), (RADIO_CURSOR, RADIO_CURSOR - 5), 2)
  49. pygame.draw.line(imagen_cursor, ROJO, (RADIO_CURSOR, RADIO_CURSOR + 5), (RADIO_CURSOR, RADIO_CURSOR * 2), 2)
  50.  
  51.  
  52. # Se cargan las fuentes de escritura y se renderizan algunos textos.
  53. textos = []
  54. cargar_textos(textos)
  55.  
  56. # Se crea el hud (zona que indica los datos de la partida).
  57.  
  58. fondo_hud = pygame.Surface((800, 100))
  59. fondo_hud.fill(NEGRO)
  60. hud = pygame.Surface((800, 100))
  61. hud.fill(NEGRO)
  62. hud.blit(textos[4],(10, 10))
  63. hud.blit(textos[3], (10, 50))
  64. hud.set_colorkey(NEGRO)
  65.  
  66.  
  67. sonidos = []
  68. #cargamos los sonidos
  69. cargar_sonidos(sonidos)
  70.  
  71. # Inicializacion y parametros de juego.
  72. tipos_barcos = sorted(list(IMAGENES_BARCOS))
  73.  
  74. # Se muestra la pantalla de titulo.
  75. ventana.blit(fondo_juego, (0, 0))
  76. pygame.display.flip()
  77.  
  78. # Se espera hasta que el usuario presione la tecla Enter para comenzar.
  79. tiempo_de_juego = -(pygame.time.get_ticks() / 1000.0)
  80. esperando_click = True
  81.  
  82. while esperando_click:
  83.     for event in pygame.event.get():
  84.         if event.type == QUIT:
  85.             sys.exit()
  86.         elif event.type == KEYDOWN and event.key == K_ESCAPE:
  87.             sys.exit()
  88.         elif event.type == MOUSEBUTTONDOWN and event.button == 1:
  89.             esperando_click = False
  90.             break
  91.  
  92. # Lo usamos para establecer cuan rapido se refresca la pantalla.
  93. reloj = pygame.time.Clock()
  94.  
  95. # Establecemos el título de la pantalla.
  96. pygame.display.set_caption("Combate Naval - Programación UTFSM")
  97.  
  98. #Iteramos hasta que el usuario pulse el botón de salir.
  99. hecho = False
  100.  
  101. # Lo usamos para establecer cuán rápido de refresca la pantalla.
  102. reloj = pygame.time.Clock()
  103.  
  104. #sonido del videojuego
  105. sonidos[1].play(-1)
  106.  
  107. #barcos jugador
  108. tablero_jugador = []  
  109.  
  110. #barcos oponente
  111. tablero_cpu = []
  112.  
  113. #auxiliar para disparos del jugador
  114. disparos_jugador = []
  115.  
  116. #auxiliar para disparos oponente
  117. disparos_cpu = []
  118.  
  119.  
  120.  
  121. #Inicializamos las 4 listas vacias
  122.  
  123. inicializar_tablero(tablero_jugador)
  124. inicializar_tablero(tablero_cpu)
  125. inicializar_tablero(disparos_cpu)
  126. inicializar_tablero(disparos_jugador)
  127.  
  128. #################### CONFIGURACIONE INICIALES ####################
  129. flag = True
  130. barco_actual = 1
  131.  
  132. comenzar = False
  133. salir = False
  134.  
  135. fila, columna = (-1,-1)
  136. ##################################################################
  137.  
  138.  
  139. #Ciclo que permite posicionar tus barcos en el tablero
  140.  
  141. while not comenzar:
  142.    
  143.     #FPS
  144.     reloj.tick(30)
  145.  
  146.     if (flag == True):
  147.         sentido = NORTE
  148.  
  149.     for event in pygame.event.get():
  150.         if event.type == QUIT:
  151.             pygame.quit()
  152.             sys.exit()
  153.            
  154.         if event.type == KEYDOWN:
  155.             if event.key == K_ESCAPE:
  156.                 salir = True
  157.                 break
  158.  
  159.     if event.type == pygame.KEYDOWN:
  160.         if event.key == pygame.K_SPACE:
  161.             if (flag == True):
  162.             sentido = OESTE
  163.             flag = False
  164.             else:
  165.             sentido = NORTE
  166.             flag = True
  167.  
  168.         if event.type == MOUSEBUTTONDOWN:
  169.             if event.button == 1:
  170.         #Obtenemos los datos de la posicion clickeada en la pantalla
  171.                 cursor = (event.pos, RADIO_CURSOR)
  172.  
  173.         # Funciones de la tarea.
  174.         fila, columna = completar_tablero(cursor, tablero_jugador, barco_actual, sentido)          
  175.                 flag = actualizar_info_posicion(fila,columna,barco_actual)
  176.  
  177.         if (flag):  
  178.                 barco_actual+= 1
  179.             flag = True
  180.        
  181.         #Hemos posicionado todos los barcos
  182.             if (barco_actual > 5):
  183.                 comenzar = True
  184.  
  185.     if salir == True:
  186.     break
  187.  
  188.     # Se actualiza el hud.    
  189.     fondo_hud.fill(NEGRO)
  190.     fondo_hud.blit(hud, (0, 0))
  191.      
  192.     dibujar_barcos(fondo_hud, imagenes_barcos)
  193.    
  194.     # Establecemos el fondo de pantalla.
  195.     ventana.fill(NEGRO)    
  196.  
  197.     # Dibujamos el tablero
  198.     dibujar_tablero (ventana, tablero_jugador, fila, columna, DES_X, DES_Y, MORADO)
  199.          
  200.     # Obtencion de posicion y dibujado del cursor.
  201.     x, y = pygame.mouse.get_pos()
  202.     x -= RADIO_CURSOR
  203.     y -= RADIO_CURSOR
  204.     ventana.blit(imagen_cursor, (x, y))
  205.      
  206.     # Dibujado del hud.
  207.     ventana.blit(fondo_hud, (0, 500))
  208.  
  209.     # Se refrezca la pantalla.
  210.     pygame.display.update()
  211.  
  212.  
  213.  
  214. #Aca llamamos a la funcion que posiciona los barcos enemigos en su tablero
  215. posicionar_barcos_enemigos(tablero_cpu)
  216.  
  217. #Aca actualizamos la informacion de los diccionarios para que podamos seguir considerandolos activos.
  218. reiniciar_info()
  219.  
  220.  
  221. fin_partida = False
  222.  
  223. #Creamos un hud para la informacion del tablero del jugador
  224. tablero1_hud = pygame.Surface((340, 340))
  225. tablero1_hud.fill(NEGRO)
  226.  
  227. tab1 = pygame.Surface((0, 0))
  228. tab1.fill(NEGRO)
  229. tab1.set_colorkey(NEGRO)
  230.  
  231. turnos = TOTAL_TURNOS
  232. jugadas = 0
  233.  
  234. #lista para almacenar los disparos del jugador
  235. disparos = []
  236.  
  237. #lista para almacenar los disparos del oponente
  238. disparos_random = []
  239.  
  240. ventana.fill(NEGRO)
  241. pygame.display.update()
  242.  
  243. #Cantidad de barcos en juego
  244. restantes_jugador = N_BARCOS
  245. restantes_maquina = N_BARCOS
  246.  
  247.  
  248. #IMPORTANTE, copiamos la informacion del tablero para poder visualizar tus propios barcos al comienzo de la partida
  249.  
  250. disparos_jugador = tablero_jugador
  251.  
  252. terminado = False
  253. gandor = -1
  254.  
  255. #cantidad total de disparos del jugador 1
  256. restantes = 200
  257. while not terminado:
  258.    
  259.     #FPS
  260.     reloj.tick(20)
  261.  
  262.     for event in pygame.event.get():
  263.         if event.type == QUIT:
  264.             pygame.quit()
  265.             sys.exit()
  266.            
  267.         if event.type == KEYDOWN:
  268.             if event.key == K_ESCAPE:
  269.                 terminado = True
  270.                 break
  271.  
  272.     if (jugadas % 2 == 0):
  273.             if event.type == MOUSEBUTTONDOWN:
  274.                 if event.button == 1:
  275.                     cursor = (event.pos, RADIO_CURSOR)
  276.  
  277.             #procesamos el disparo del jugador
  278.                 disparo = procesar_disparo(cursor, tablero_cpu, disparos_cpu, DX, DY)          
  279.            
  280.             #si el disparo es valido
  281.             if (disparo[0] != -1):
  282.                 disparos.append(disparo)
  283.                 turnos-= 1
  284.                 fila, columna = disparo[1]
  285.         else:
  286.         if event.type == KEYDOWN:
  287.                 if event.key == K_SPACE:
  288.                 while (1):
  289.             #procesamos el disparo de tu oponente
  290.                 random_disparo = disparo_enemigo(tablero_jugador, disparos_jugador)
  291.                  
  292.                 #si el disparo es valido  
  293.             if (random_disparo[0] != -1):
  294.                 break
  295.  
  296.                     disparos_random.append(random_disparo)
  297.                     turnos-= 1
  298.                     fila, columna = disparo[1]
  299.  
  300.     #cargamos los elementos visuales
  301.    
  302.     if (terminado):
  303.     break
  304.  
  305.     ventana.fill(NEGRO)
  306.    
  307.     fondo_hud.fill(NEGRO)
  308.     fondo_hud.blit(hud, (0, 0))
  309.    
  310.     dibujar_barcos(fondo_hud, imagenes_barcos)
  311.     actualizar_textos(fondo_hud, textos, restantes_jugador, restantes_maquina)
  312.  
  313.     #Dibujamos lo ocurrido con el disparo
  314.     dibujar_tablero(ventana,disparos_cpu,fila,columna,DX,DY,AMARILLO)  
  315.     dibujar_tablero(tablero1_hud,disparos_jugador,fila,columna,0,0,AMARILLO)
  316.    
  317.     #cargamos el cursor
  318.     x, y = pygame.mouse.get_pos()
  319.     x -= RADIO_CURSOR
  320.     y -= RADIO_CURSOR
  321.     ventana.blit(imagen_cursor, (x, y))
  322.  
  323.     # Dibujado del hud.
  324.     ventana.blit(tablero1_hud, (30,30))
  325.     ventana.blit(fondo_hud, (0, 500))
  326.  
  327.     # Se refrezca la pantalla.
  328.     pygame.display.update()
  329.    
  330.     #si es que hemos realizado los 3 disparos
  331.     if (turnos == 0):
  332.         if (jugadas % 2 == 0):
  333.             for i in disparos:     
  334.                 restantes-= 1
  335.                 bajas = actualizar_tablero(disparos_cpu, i, jugadas)
  336.             sonido_disparos(ventana, tablero1_hud, imagen_cursor, disparos_cpu, i, sonidos, 0)
  337.  
  338.             if (bajas == 1):
  339.             sonidos[4].play()
  340.             sonidos[5].play()
  341.                 time.sleep(2)
  342.             restantes_maquina-= 1
  343.  
  344.         if (restantes_maquina == 0):
  345.             ganador = 0
  346.             terminado = True
  347.             break
  348.  
  349.         #limpiamos la informacion de los disparos
  350.         disparos  = [] 
  351.     else:
  352.        
  353.             for i in disparos_random:      
  354.                 restantes-= 1
  355.                 bajas = actualizar_tablero(disparos_jugador, i, jugadas)
  356.             sonido_disparos(ventana, tablero1_hud, imagen_cursor, disparos_jugador, i, sonidos, 1)
  357.  
  358.             if (bajas == 1):
  359.             sonidos[4].play()
  360.             sonidos[5].play()
  361.                 time.sleep(2)
  362.             restantes_jugador-= 1
  363.    
  364.         if (restantes_jugador == 0):
  365.             ganador = 1
  366.             terminado = True
  367.             break
  368.  
  369.         #limpiamos la informacion de los disparos          
  370.         disparos_random = []  
  371.  
  372.     #cargamos la informacion de los disparos
  373.         if (restantes == 2):
  374.         turnos = 1
  375.         else:
  376.             turnos = 3
  377.     jugadas+=1
  378.    
  379.  
  380. ventana.fill(NEGRO)
  381.  
  382. if (ganador == 0):
  383.     img_ganador = pygame.image.load("img/"+IMAGEN_GANADOR).convert()
  384.     if img_ganador.get_size() != DIMENSIONES:
  385.         img_ganador = pygame.transform.scale(img_ganador, DIMENSIONES)
  386.     ventana.blit(img_ganador, (0, 0))
  387.  
  388. else:
  389.     img_perdedor = pygame.image.load("img/"+IMAGEN_PERDEDOR).convert()
  390.     if img_perdedor.get_size() != DIMENSIONES:
  391.         img_perdedor = pygame.transform.scale(img_perdedor, DIMENSIONES)
  392.     ventana.blit(img_perdedor, (0, 0))
  393.  
  394.  
  395. pygame.display.flip()
  396.  
  397. esperando_click = True
  398.  
  399. while esperando_click:
  400.     for event in pygame.event.get():
  401.         if event.type == QUIT:
  402.             sys.exit()
  403.         elif event.type == KEYDOWN and event.key == K_ESCAPE:
  404.             sys.exit()
  405.         elif event.type == MOUSEBUTTONDOWN and event.button == 1:
  406.             esperando_click = False
  407.             break
  408.  
  409. # Se finalizan los modulos de pygame.
  410. pygame.quit()
  411. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement