Add enemies in rooms
This commit is contained in:
parent
eda890d5e6
commit
d1e3187a12
4 changed files with 72 additions and 15 deletions
12
config.yaml
12
config.yaml
|
@ -4,13 +4,25 @@
|
||||||
|
|
||||||
# Map
|
# Map
|
||||||
room_side : 1000
|
room_side : 1000
|
||||||
|
wall_size : 150
|
||||||
|
|
||||||
|
|
||||||
|
# general
|
||||||
|
texture_path : 'asset/'
|
||||||
|
|
||||||
# Player
|
# Player
|
||||||
|
path_player : 'perso.png'
|
||||||
|
path_canon : 'canon.png'
|
||||||
cap_speed : 10
|
cap_speed : 10
|
||||||
acceleration : 2
|
acceleration : 2
|
||||||
deceleration : 1.1
|
deceleration : 1.1
|
||||||
|
|
||||||
|
# Ennemy
|
||||||
|
path_enemy : 'perso.png'
|
||||||
|
|
||||||
|
# Projectile
|
||||||
|
path_projectile : 'projectile1_right.png'
|
||||||
|
|
||||||
# Projectile
|
# Projectile
|
||||||
max_fire_rate : 10
|
max_fire_rate : 10
|
||||||
|
|
||||||
|
|
26
main.py
26
main.py
|
@ -7,6 +7,7 @@ import pygame
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from models import Game, Player, Carte, Camera, Enemy, Hud
|
from models import Game, Player, Carte, Camera, Enemy, Hud
|
||||||
|
from utils import enemy_placement
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
|
@ -22,26 +23,29 @@ WHITE=(255,255,255)
|
||||||
BLUE=(0,0,255)
|
BLUE=(0,0,255)
|
||||||
RED=(255,0,0)
|
RED=(255,0,0)
|
||||||
|
|
||||||
# with open(r'./config.yaml') as file:
|
|
||||||
# data = yaml.load(file,Loader=yaml.FullLoader)
|
|
||||||
# up = pygame.key.key_code(data['up'])
|
|
||||||
# right = pygame.key.key_code(data['right'])
|
|
||||||
# down = pygame.key.key_code(data['down'])
|
|
||||||
# left = pygame.key.key_code(data['left'])
|
|
||||||
# pause = pygame.key.key_code(data['pause'])
|
|
||||||
# print(up,right,down,left,pause)
|
|
||||||
|
|
||||||
|
|
||||||
|
with open(r'./config.yaml') as file:
|
||||||
|
data = yaml.load(file,Loader=yaml.FullLoader)
|
||||||
|
path_player = data['path_player']
|
||||||
|
path_canon = data['path_canon']
|
||||||
|
path_enemy = data['path_enemy']
|
||||||
|
path_projectile = data['path_projectile']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
carte = Carte(n=5)
|
carte = Carte(n=5)
|
||||||
|
|
||||||
|
enemies = []
|
||||||
|
for positions in enemy_placement(carte.carte):
|
||||||
|
for position in positions:
|
||||||
|
enemies.append(Enemy('Plop',position,path_enemy))
|
||||||
|
|
||||||
start_pos = carte.player_start_pos
|
start_pos = carte.player_start_pos
|
||||||
player = Player('Alice',start_pos,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png')
|
player = Player('Alice',start_pos,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,path_player,path_canon,path_projectile)
|
||||||
camera = Camera(start_pos,screen_width,screen_height,0.3)
|
camera = Camera(start_pos,screen_width,screen_height,0.3)
|
||||||
#enemies = [Enemy('Plop',(300,300),'perso.png')]
|
#enemies = [Enemy('Plop',(300,300),'perso.png')]
|
||||||
hud = Hud(player)
|
hud = Hud(player)
|
||||||
game = Game(carte,player,camera,[],hud)
|
game = Game(carte,player,camera,enemies,hud)
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,6 @@ from math import atan, degrees,radians, cos, sin
|
||||||
|
|
||||||
from utils import *
|
from utils import *
|
||||||
|
|
||||||
img_path = 'asset/'
|
|
||||||
|
|
||||||
|
|
||||||
with open(r'./config.yaml') as file:
|
with open(r'./config.yaml') as file:
|
||||||
data = yaml.load(file,Loader=yaml.FullLoader)
|
data = yaml.load(file,Loader=yaml.FullLoader)
|
||||||
|
@ -19,6 +17,7 @@ with open(r'./config.yaml') as file:
|
||||||
cap_speed = data['cap_speed']
|
cap_speed = data['cap_speed']
|
||||||
acceleration = data['acceleration']
|
acceleration = data['acceleration']
|
||||||
deceleration = data['deceleration']
|
deceleration = data['deceleration']
|
||||||
|
img_path = data['texture_path']
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -67,6 +66,7 @@ class Game():
|
||||||
#Return the position of the elements on the screen (camera)
|
#Return the position of the elements on the screen (camera)
|
||||||
return (self.player.posx-self.camera.posx,self.player.posy-self.camera.posy)
|
return (self.player.posx-self.camera.posx,self.player.posy-self.camera.posy)
|
||||||
|
|
||||||
|
|
||||||
class Hud():
|
class Hud():
|
||||||
def __init__(self,player):
|
def __init__(self,player):
|
||||||
self.player = player
|
self.player = player
|
||||||
|
@ -281,6 +281,7 @@ class Player():
|
||||||
|
|
||||||
class Enemy():
|
class Enemy():
|
||||||
def __init__(self,name,position,texture):
|
def __init__(self,name,position,texture):
|
||||||
|
self.life = 100
|
||||||
self.name = name
|
self.name = name
|
||||||
self.posx = position[0]
|
self.posx = position[0]
|
||||||
self.posy = position[1]
|
self.posy = position[1]
|
||||||
|
|
42
utils.py
42
utils.py
|
@ -7,6 +7,7 @@ from glob import glob
|
||||||
import random
|
import random
|
||||||
import pygame
|
import pygame
|
||||||
import yaml
|
import yaml
|
||||||
|
from random import randint
|
||||||
|
|
||||||
def fetch_rooms(path):
|
def fetch_rooms(path):
|
||||||
"""Fetch all the basic tiles asset from the path.
|
"""Fetch all the basic tiles asset from the path.
|
||||||
|
@ -50,6 +51,45 @@ def check_map(carte):
|
||||||
positions.append(room[2])
|
positions.append(room[2])
|
||||||
return(True)
|
return(True)
|
||||||
|
|
||||||
|
def enemy_placement(carte):
|
||||||
|
""" Generate positions of ennemies in each room by picking random positions and testing
|
||||||
|
collisions with the room. Exclude the first room.
|
||||||
|
"""
|
||||||
|
# Load data
|
||||||
|
with open(r'./config.yaml') as file:
|
||||||
|
data = yaml.load(file,Loader=yaml.FullLoader)
|
||||||
|
room_side = data['room_side']
|
||||||
|
wall_size = data['wall_size']
|
||||||
|
path_enemy = data['path_enemy']
|
||||||
|
img_path = data['texture_path']
|
||||||
|
|
||||||
|
# Generate usefull values and variables
|
||||||
|
carte_positions = [[]]
|
||||||
|
positions = []
|
||||||
|
enemy_mask = pygame.mask.from_surface(pygame.image.load(img_path+path_enemy).convert_alpha())
|
||||||
|
enemy_size = enemy_mask.get_size()
|
||||||
|
enemy_center = [int(enemy_size[0]/2),int(enemy_size[1]/2)]
|
||||||
|
padding_h = wall_size + int(enemy_size[0]/2)
|
||||||
|
padding_v = wall_size + int(enemy_size[1]/2)
|
||||||
|
retry = 0
|
||||||
|
|
||||||
|
for room in carte[1:]:
|
||||||
|
retry = 0
|
||||||
|
while len(positions) < 3 and retry < 20:
|
||||||
|
posx = room[2][0] + randint(padding_h, room_side-padding_h)
|
||||||
|
posy = room[2][1] + randint(padding_v, room_side-padding_v)
|
||||||
|
|
||||||
|
if room[1].overlap(enemy_mask, (posx-enemy_center[0],posy-enemy_center[1])):
|
||||||
|
retry+=1
|
||||||
|
else:
|
||||||
|
#print(f'New enemy at {posx,posy}')
|
||||||
|
positions.append((posx,posy))
|
||||||
|
carte_positions.append(positions)
|
||||||
|
positions = []
|
||||||
|
|
||||||
|
return carte_positions
|
||||||
|
|
||||||
|
|
||||||
def map_generator(n):
|
def map_generator(n):
|
||||||
"""Map generator generate a map with a main path of n rooms
|
"""Map generator generate a map with a main path of n rooms
|
||||||
"""
|
"""
|
||||||
|
@ -143,7 +183,7 @@ def map_generator(n):
|
||||||
if not check_map(carte):
|
if not check_map(carte):
|
||||||
raise ValueError("Invalid Map after closing paths.")
|
raise ValueError("Invalid Map after closing paths.")
|
||||||
|
|
||||||
return(carte,(int(room_side/2),int(room_side/2)),background)
|
return(carte,(int(room_side/2),int(room_side/2)),background,)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue