Basic random map display

This commit is contained in:
grisel-davy 2020-05-03 22:08:10 -04:00
parent 9a754f88e7
commit c16f439569
3 changed files with 41 additions and 23 deletions

View file

@ -25,11 +25,12 @@ RED=(255,0,0)
carte = Carte('map1')
# possible positions: 300,300: 1400,1000: 2700,400, 2800,1600
# click = 323
player = Player('Alice',660,1570,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png')
camera = Camera(660,1570,screen_width,screen_height,0.3)
enemies = [Enemy('Plop',(300,300),'perso.png')]
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')
camera = Camera(start_pos,screen_width,screen_height,0.3)
#enemies = [Enemy('Plop',(300,300),'perso.png')]
hud = Hud(player)
game = Game(carte,player,camera,enemies,hud)
game = Game(carte,player,camera,[],hud)
clock = pygame.time.Clock()

View file

@ -7,6 +7,8 @@ import pygame
from time import time
from math import atan, degrees,radians, cos, sin
from utils import *
img_path = 'asset/'
max_fire_rate = 10 # in fires per seconds
@ -75,17 +77,27 @@ class Carte():
self.back = pygame.image.load('maps/'+mapname+'_back.png').convert()
self.wall = pygame.image.load('maps/'+mapname+'_wall.png').convert_alpha()
self.mask = pygame.mask.from_surface(self.wall)
self.carte, self.player_start_pos = map_generator(5)
def draw(self,surface,camera):
offsetx,offsety = camera.get_offset()
surface.blit(self.back,(self.posx-offsetx,self.posy-offsety))
surface.blit(self.wall,(self.posx-offsetx,self.posy-offsety))
for room in self.carte:
print(room)
surface.blit(room[0],(room[2][0]-offsetx,room[2][1]-offsety))
#surface.blit(self.back,(self.posx-offsetx,self.posy-offsety))
#surface.blit(self.wall,(self.posx-offsetx,self.posy-offsety))
def collision(self,player):
for room in self.carte:
if room[1].overlap(player.mask, (player.posx-player.player_rect.center[0]-room[2][0],player.posy-player.player_rect.center[1]-room[2][1])):
return True
return False
class Camera():
def __init__(self,start_posx,start_posy,screen_width,screen_height,box_ratio):
self.posx = start_posx
self.posy = start_posy
def __init__(self,start_pos,screen_width,screen_height,box_ratio):
self.posx = start_pos[0]
self.posy = start_pos[1]
self.screen_height = screen_height
self.screen_width = screen_width
self.box_ratio = box_ratio
@ -104,10 +116,10 @@ class Camera():
class Player():
def __init__(self,name,posx,posy,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
def __init__(self,name,start_pos,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
self.name = name
self.posx = posx
self.posy = posy
self.posx = start_pos[0]
self.posy = start_pos[1]
self.speed = [0,0]
self.key_up = key_up
@ -174,7 +186,7 @@ class Player():
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])):
if carte.collision(self):
self.posx = temp_pos
self.speed[0] = 0
@ -187,7 +199,7 @@ class Player():
# Y AXIS
temp_pos = self.posy
self.posy = self.posy+self.speed[1]
if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])):
if carte.collision(self):
self.posy = temp_pos
self.speed[1] = 0
@ -203,7 +215,7 @@ class Player():
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])):
if carte.collision(self):
self.posx = temp_pos
self.speed[0] = 0
# If the player really move, camera follow
@ -213,7 +225,7 @@ class Player():
# Y AXIS
temp_pos = self.posy
self.posy = self.posy+self.speed[1]
if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])):
if carte.collision(self):
self.posy = temp_pos
self.speed[1] = 0
# If the player really move, camera follow

View file

@ -5,6 +5,7 @@
from glob import glob
import random
import pygame
def fetch_rooms(path):
"""Fetch all the basic tiles asset from the path.
@ -39,6 +40,8 @@ def map_generator(n):
"""Map generator generate a map with a main path of n rooms
"""
start_poss = {'T':(500,50),'R':(950,500),'D':(500,950),'L':(50,500)}
room_side = 1000
assembly = fetch_rooms('./maps/rooms/')
@ -47,11 +50,15 @@ def map_generator(n):
start_key = random.choice(list(assembly.keys()))
start_label,start_room = random.choice(assembly[start_key])
# Place the first room in the list
position = [0,0]
carte = [(start_room,position)]
asset = pygame.image.load(start_room).convert_alpha()
mask = pygame.mask.from_surface(asset)
carte = [(asset,mask,position)]
# Select an arbitrary first direction of arrival
dir_from = random.choice(start_label)
start_pos = start_poss[dir_from]
current_label = start_label
for i in range(n):
@ -60,6 +67,8 @@ def map_generator(n):
# Select the next room
next_label,next_room = random.choice(assembly[dir_next])
asset = pygame.image.load(next_room).convert_alpha()
mask = pygame.mask.from_surface(asset)
# Compute the position of the next tile
if dir_next == 'T':
@ -79,17 +88,13 @@ def map_generator(n):
dir_from = 'R'
# Build the map
carte.append((next_room,next_position))
carte.append((asset,mask,next_position))
# Update the label for next turn
current_label = next_label
position = next_position
# Display the result:
for room in carte:
print(f"Room {room[0].split('/')[-1]} at position {room[1]}")
print('Done.')
return(carte,start_pos)