Skip to content

Commit

Permalink
Ajustement de la redimension de la fenêtre:
Browse files Browse the repository at this point in the history
- La fenêtre garde systématiquement un format 16:9
- Les valeurs de taille des monstres, vitesse et offset sont relative à 
la taille de la fenêtre
- Temps minimum d'un tick définit à 10 milliseconde (100 fps max)
- Ajustement des tailles de fenêtres et taille de map (16x9)
- L'image de fond n'est plus recalculée au redimensionnement.
  • Loading branch information
antoinech2 committed Mar 20, 2020
1 parent 16cb053 commit d52f801
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
24 changes: 17 additions & 7 deletions src/enemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
import math
import random

def Init(path, box_pixel):
global path_coords, box_size_pixel
#TODO: remplacer les variables globales par les varibles de Class

def Init(path, box_pixel, ratio):
global path_coords, box_size_pixel, global_ratio
path_coords = path
box_size_pixel = box_pixel
global_ratio = ratio

class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.max_health = 100
self.current_health = self.max_health
self.resistance = 0.95
self.speed = 0.25
self.image = pygame.Surface((30,30))
self.speed = 1.5*global_ratio
self.image = pygame.Surface((30*global_ratio,30*global_ratio))
self.image.fill(pygame.Color(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.rect = self.image.get_rect()
self.offset = (0.5*(1+self.rect.width/box_size_pixel[0]),0.5*(1+self.rect.height/box_size_pixel[1]))
self.coords = (box_size_pixel[0]*(path_coords[0][0]-self.offset[0]),box_size_pixel[1]*(path_coords[0][1]-self.offset[1]))
self.rect.x = self.coords[0]
self.rect.y = self.coords[1]
self.current_case_number = 0
self.position_precision = 15
self.position_precision = 15*global_ratio
self.destination_offset = 0.15
self.has_finished = False
self.NewDestination()
Expand All @@ -32,16 +35,23 @@ def Move(self):
self.rect.x = self.coords[0]
self.rect.y = self.coords[1]
if math.sqrt((self.coords[0]-self.coords_arrivee[0])**2+(self.coords[1]-self.coords_arrivee[1])**2) < self.position_precision:
self.current_case_number += 1
if self.current_case_number >= len(path_coords)-1:
if self.current_case_number >= len(path_coords)-2:
self.EndPath()
else:
self.current_case_number += 1
self.NewDestination()
def NewDestination(self):
coords_depart = (self.rect.x, self.rect.y)
self.coords_arrivee = (box_size_pixel[0]*(path_coords[self.current_case_number+1][0]-self.offset[0]+random.uniform(-self.destination_offset,self.destination_offset)) , box_size_pixel[1]*(path_coords[self.current_case_number+1][1]-self.offset[1]+random.uniform(-self.destination_offset,self.destination_offset)))
direction_angle = math.atan2(self.coords_arrivee[1]-coords_depart[1], self.coords_arrivee[0]-coords_depart[0])
self.avance = (math.cos(direction_angle)*self.speed, math.sin(direction_angle)*self.speed)
def UpdatePosition(self, ratio):
self.image = pygame.transform.scale(self.image,(int(self.rect.w*ratio),int(self.rect.h*ratio)))
self.rect = self.image.get_rect()
self.coords = (self.coords[0]*ratio,self.coords[1]*ratio)
self.rect.x = self.coords[0]
self.rect.y = self.coords[1]
self.speed *= ratio
def EndPath(self):
self.has_finished = True
def HasFinished(self):
Expand Down
35 changes: 28 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import map_generator
import map_drawing
import enemy
import time
############################################

############################################
#Définition des constantes générales du jeu:
screen_size = (900,600)
map_size = (10,10)
screen_format = 9/16
default_screen_size = (992,558)
screen_size = default_screen_size
min_screen_size = (750,422)
map_size = (16,9)
############################################

############################################
Expand All @@ -46,25 +50,42 @@ def __main__():

path_coords = map_generator.CalculateNewPath(map_size)
map_surface, box_size_pixel = map_drawing.CreateMapSurface(map_size,path_coords, screen_size)
enemy.Init(path_coords, box_size_pixel)
enemy.Init(path_coords, box_size_pixel, 1)
all_enemies = pygame.sprite.Group()
all_enemies.add(enemy.Enemy())
current_tick = 0
#Boucle principale
while is_game_running:
current_tick += 1
time.sleep(0.01)

#EVENTS
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_game_running = False
elif event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
screen_size = (event.w, event.h)
map_surface, box_size_pixel = map_drawing.CreateMapSurface(map_size,path_coords, screen_size)
#Nouveau: event.w,event.h
#Ancien : screen_size = (992,558)
diff = (abs(event.w-screen_size[0]),abs(event.h-screen_size[1]))
if diff[0] > diff[1]:
w = max(min_screen_size[0],event.w)
new_width, new_height = w, int(w*screen_format)
else:
h = max(min_screen_size[1],event.h)
new_width, new_height = int(h/screen_format), h

changed_ratio = new_width/screen_size[0]
global_ratio = new_width/default_screen_size[0]
pygame.display.set_mode((new_width,new_height), pygame.RESIZABLE)
screen_size = (new_width, new_height)
map_surface, box_size_pixel = map_drawing.ResizeMapSurface(map_size, screen_size, map_surface)
enemy.Init(path_coords, box_size_pixel, global_ratio)
for current_enemy in all_enemies:
current_enemy.UpdatePosition(changed_ratio)
current_enemy.NewDestination()

#CALCULS
if current_tick%1000 == 0:
if current_tick%100 == 0:
all_enemies.add(enemy.Enemy())
for current_enemy in all_enemies:
if current_enemy.HasFinished():
Expand Down
6 changes: 6 additions & 0 deletions src/map_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ def CreateMapSurface(map_size,path_coords, screen_size):
image = random.choice([img_herbv2,img_herbv2,img_herbv2,img_herbv2,img_herbv2,img_plant,img_arbr])
map_surface.blit(image,((column-1)*box_size_pixel[0],(row-1)*box_size_pixel[1]))
return map_surface, box_size_pixel

def ResizeMapSurface(map_size, screen_size, map_surface):
map_size_pixel = (0.8*screen_size[0],0.8*screen_size[1])
box_size_pixel = (math.floor(map_size_pixel[0]/map_size[0]),math.floor(map_size_pixel[1]/map_size[1]))
map_surface = pygame.transform.scale(map_surface,(int(map_size_pixel[0]),int(map_size_pixel[1])))
return map_surface, box_size_pixel
2 changes: 1 addition & 1 deletion src/map_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Définition des constantes
force_space_between_path = True
path_coords = []
min_path_length = 30
min_path_length = 37

#Constantes de direction:
DIR_NORTH = 1
Expand Down

0 comments on commit d52f801

Please sign in to comment.