Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianoBardallo authored Jan 4, 2023
1 parent 76d52c6 commit 329948b
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 0 deletions.
58 changes: 58 additions & 0 deletions configuraciones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#VENTANA
ANCHO_VENTANA = 700
ALTO_VENTANA = 400

#CORDENADA DEL PISO
NIVEL_PISO = 552
NIVEL_TECHO = 50

#FOTOGRAMAS POR SEGUNDO
FPS = 120

#RUTA DE IMAGENES
RUTA_IMAGEN = r"C:\Users\lucia\Documents\Castle Defense\recursos\\" #RUTA DE IMAGENES
RUTA_MUSICA = r"C:\Users\lucia\Documents\JUEGO_FINAL\sounds\\" #RUTA DE MUSICA

#DIRECCION DONDE MIRA EL PERSONAJKE
IZQUIERDA = -1
DERECHA = 1

#TAMAÑO DE COLLIDERECT
ALTURA_PIES = 8 # Aprox Gravedad/2 + 1

#MODO DEBUG // DIBUJAR RECTANGULOS
DEBUG = False

#COLORES
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
WHITE = (255,255,255)
YELLOW = (255,255,0)
CYAN = "#d7fcd4"
DORADO = "#b68f40"

#ESTADOS
M_STATE_NORMAL = 1
M_STATE_HOVER = 2
M_STATE_CLICK = 3
M_BRIGHT_HOVER = 1
M_BRIGHT_CLICK = 2

#TILED
PLATAFORMA = "P"
PISO = "O"
MURO = "M"
LOOT = "B"
CAJA = "C"
PINCHO = "S"
FONDO_ACIDO = "A"
TOP_ACIDO = "T"
MOBILE = "U"

CORAZON = RUTA_IMAGEN + r"Menu\Button\Health_Dot.png"
BALA = RUTA_IMAGEN + r"Menu\Button\Armor_Bar_Dot.png"



39 changes: 39 additions & 0 deletions gui_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pygame
from pygame.locals import *
from gui_widget import Widget
from configuraciones import *


class Button(Widget):
def __init__(self,master,x=0,y=0,w=200,h=50,color_background=GREEN,color_border=RED,image_background=None,text="Button",font="Arial",font_size=14,font_color=BLUE,on_click=None,on_click_param=None):
super().__init__(master,x,y,w,h,color_background,color_border,image_background,text,font,font_size,font_color)
self.on_click = on_click
self.on_click_param = on_click_param
self.state = M_STATE_NORMAL
self.render()

def render(self):
super().render()
if self.state == M_STATE_HOVER: # Se aclara la imagen
self.slave_surface.fill(M_BRIGHT_HOVER, special_flags=pygame.BLEND_RGB_ADD)
elif self.state == M_STATE_CLICK: # Se oscurece la imagen
self.slave_surface.fill(M_BRIGHT_CLICK, special_flags=pygame.BLEND_RGB_SUB)

def update(self,lista_eventos):
mousePos = pygame.mouse.get_pos()
self.state = M_STATE_NORMAL
if self.slave_rect_collide.collidepoint(mousePos):
if(pygame.mouse.get_pressed()[0]):
self.state = M_STATE_CLICK
else:
self.state = M_STATE_HOVER

for evento in lista_eventos:
if evento.type == pygame.MOUSEBUTTONDOWN:
if(self.slave_rect_collide.collidepoint(evento.pos)):
self.on_click(self.on_click_param)

self.render()



43 changes: 43 additions & 0 deletions gui_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pygame
from configuraciones import *
from pygame.locals import *

class Form():
forms_dict = {}
def __init__(self,name,master_surface,x,y,w,h,color_background,imagen_background,color_border,active):
self.forms_dict[name] = self
self.master_surface = master_surface
self.x = x
self.y = y
self.w = w
self.h = h
self.color_background = color_background
self.color_border = color_border
self.imagen_background = imagen_background

self.surface = pygame.Surface((w,h))
self.slave_rect = self.surface.get_rect()
self.slave_rect.x = x
self.slave_rect.y = y
self.active = active

if self.imagen_background != None:
self.imagen_background = pygame.image.load(imagen_background)
self.imagen_background = pygame.transform.scale(self.imagen_background,(self.w,self.h)).convert_alpha()
self.surface = self.imagen_background
elif(self.color_background != None):
self.surface.fill(self.color_background)

def set_active(self,name):
for aux_form in self.forms_dict.values():
aux_form.active = False
self.forms_dict[name].active = True

def render(self):
pass

def update(self,lista_eventos):
pass

def draw(self):
self.master_surface.blit(self.surface,self.slave_rect)
32 changes: 32 additions & 0 deletions gui_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pygame.locals import *
from configuraciones import *
from gui_nivel import FormNivel


class FormManager():
"""
Formulario manager que crea los demas formularios, los actualiza y dibuj, asi como tambien se fija cual mostrar en pantalla
"""
def __init__(self,pantalla) -> None:
self.pantalla = pantalla
self.form_nivel_1 = self.crear_nivel("nivel_1",RUTA_IMAGEN + "fondo\MetalSlug-Mission2.jpg")
self.niveles = [self.form_nivel_1]

def crear_nivel(self,nivel,fondo):
"""
Este metodo se encarga de crear el formulario nivel
Parametros: recibe un str que representa la clave del nivel a crear
Retorna: un objeto que representa al formulario del nivel
"""
return FormNivel(name=nivel,master_surface = self.pantalla,x=0,y=0,w=ANCHO_VENTANA,h=ALTO_VENTANA,imagen_background=fondo,color_background=BLACK,color_border=BLACK,active=True)


def actualizar_forms(self,eventos):
"""
Este metodo se encarga de actualizar y dibujar el formulario que este activo
Parametros: una lista de eventos, teclas, sonidos, tambien un valor delta_ms, y un evento de usuario de tiempo
"""

if(self.form_nivel_1.active):
self.form_nivel_1.update(eventos)
self.form_nivel_1.draw()
44 changes: 44 additions & 0 deletions gui_nivel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pygame
from pygame.locals import *
from configuraciones import *
from gui_form import Form
from gui_progressbar import ProgressBar
from gui_widget import Widget

class FormNivel(Form):
"""
Formulario que maneja el nivel, dependiendo de cual se cargue
"""
def __init__(self, name, master_surface, x=0, y=0, w=ANCHO_VENTANA, h=ALTO_VENTANA, imagen_background=None, color_background=None, color_border=None, active=False):
self.name = name
self.tiempo_juego = 0
self.puntuacion = 0
super().__init__(name, master_surface, x, y, w, h, color_background, imagen_background, color_border, active)

self.text1 = Widget(master=self,x=450,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text2 = Widget(master=self,x=480,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text3 = Widget(master=self,x=510,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text4 = Widget(master=self,x=540,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text5 = Widget(master=self,x=570,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text6 = Widget(master=self,x=600,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text7 = Widget(master=self,x=630,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text8 = Widget(master=self,x=660,y=45,w=25,h=25,color_background=None,color_border=DORADO,image_background=RUTA_IMAGEN + "interface\Marco.png",text=None,font=None,font_size=None,font_color=None)
self.text9 = Widget(master=self,x=600,y=10,w=100,h=25,color_background=None,color_border=None,image_background=None,text="{0}".format(self.tiempo_juego),font="IMPACT",font_size=30,font_color=WHITE)
self.text10 = Widget(master=self,x=300,y=10,w=100,h=25,color_background=None,color_border=None,image_background=None,text="{0}".format(self.puntuacion),font="IMPACT",font_size=30,font_color=WHITE)

self.lista_widget = [self.text1,self.text2,self.text3,self.text4,self.text5,self.text6,self.text7,self.text8,self.text9,self.text10]


def update(self, lista_eventos):
for aux_widget in self.lista_widget:
aux_widget.update(lista_eventos)


def draw(self):
super().draw()
for aux_widget in self.lista_widget:
aux_widget.draw()




29 changes: 29 additions & 0 deletions gui_progressbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pygame
from pygame.locals import *
from gui_widget import Widget
from configuraciones import *


class ProgressBar(Widget):
def __init__(self,master,x=0,y=0,w=200,h=50,color_background=None,color_border=None,image_background=None,image_progress=None,value=1,value_max=5):
super().__init__(master,x,y,w,h,color_background,color_border,image_background,None,None,None,None)


self.surface_element = pygame.image.load(image_progress)
self.surface_element = pygame.transform.scale(self.surface_element,(w/value_max, h)).convert_alpha()

self.value=value
self.value_max=value_max
self.render()

def render(self):
super().render()
for x in range(self.value):
self.slave_surface.blit(self.surface_element, (x*self.w/self.value_max, 0))

def update(self,lista_eventos):

self.render()



46 changes: 46 additions & 0 deletions gui_textbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pygame
from pygame.locals import *
from gui_widget import Widget
from configuraciones import *


class TextBox(Widget):
def __init__(self,master,x=0,y=0,w=200,h=50,color_background=GREEN,color_border=RED,image_background=None,text="Button",font="Arial",font_size=14,font_color=BLUE,on_click=None,on_click_param=None):
super().__init__(master,x,y,w,h,color_background,color_border,image_background,text,font,font_size,font_color)
self.on_click = on_click
self.on_click_param = on_click_param
self.state = M_STATE_NORMAL
self.writing_flag = False
self.render()

def render(self):
super().render()
if self.state == M_STATE_HOVER: # Se aclara la imagen
self.slave_surface.fill(M_BRIGHT_HOVER, special_flags=pygame.BLEND_RGB_ADD)
elif self.state == M_STATE_CLICK: # Se oscurece la imagen
self.slave_surface.fill(M_BRIGHT_CLICK, special_flags=pygame.BLEND_RGB_SUB)

def update(self,lista_eventos):
mousePos = pygame.mouse.get_pos()
self.state = M_STATE_NORMAL
if self.slave_rect_collide.collidepoint(mousePos):
if(self.writing_flag):
self.state = M_STATE_CLICK
else:
self.state = M_STATE_HOVER

for evento in lista_eventos:
if evento.type == pygame.MOUSEBUTTONDOWN :
self.writing_flag = self.slave_rect_collide.collidepoint(evento.pos)
if evento.type == pygame.KEYDOWN and self.writing_flag:
if evento.key == pygame.K_RETURN:
self.writing_flag = False
elif evento.key == pygame.K_BACKSPACE:
self._text = self._text[:-1]
else:
self._text += evento.unicode

self.render()



55 changes: 55 additions & 0 deletions gui_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pygame
from pygame.locals import *
from configuraciones import *

class Widget:
def __init__(self,master,x,y,w,h,color_background,color_border,image_background,text,font,font_size,font_color):
self.master = master
self.x = x
self.y = y
self.w = w
self.h = h
self.color_background = color_background
self.color_border = color_border
if image_background != None:
self.image_background = pygame.image.load(image_background)
self.image_background = pygame.transform.scale(self.image_background,(w, h)).convert_alpha()
else:
self.image_background = None
self._text = text
if(self._text != None):
pygame.font.init()
self._font_sys = pygame.font.SysFont(font,font_size)
self._font_color = font_color

def render(self):

self.slave_surface = pygame.Surface((self.w,self.h), pygame.SRCALPHA)
self.slave_rect = self.slave_surface.get_rect()
self.slave_rect.x = self.x
self.slave_rect.y = self.y
self.slave_rect_collide = pygame.Rect(self.slave_rect)
self.slave_rect_collide.x += self.master.x
self.slave_rect_collide.y += self.master.y

if self.color_background:
self.slave_surface.fill(self.color_background)

if self.image_background:
self.slave_surface.blit(self.image_background,(0,0))

if(self._text != None):
image_text = self._font_sys.render(self._text,True,self._font_color,self.color_background)
self.slave_surface.blit(image_text,[
self.slave_rect.width/2 - image_text.get_rect().width/2,
self.slave_rect.height/2 - image_text.get_rect().height/2
])

if self.color_border:
pygame.draw.rect(self.slave_surface, self.color_border, self.slave_surface.get_rect(), 2)

def update(self,lista_eventos):
self.render()

def draw(self):
self.master.surface.blit(self.slave_surface,self.slave_rect)
42 changes: 42 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pygame
from pygame.locals import *
import sys
from configuraciones import *
from gui_manager import *


flags = DOUBLEBUF
PANTALLA = pygame.display.set_mode((ANCHO_VENTANA,ALTO_VENTANA), flags, 16)
pygame.init()
clock = pygame.time.Clock()

juego = FormManager(PANTALLA)

timer_1s = pygame.USEREVENT + 0
pygame.time.set_timer(timer_1s,1000)

while True:
eventos = pygame.event.get()
for event in eventos:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

delta_ms = clock.tick(FPS)

juego.actualizar_forms(eventos)

pygame.display.flip()













0 comments on commit 329948b

Please sign in to comment.