Add pause screen as transp overlay

This commit is contained in:
grisel-davy 2020-05-06 19:41:34 -04:00
parent 9c71bd0312
commit ca7d32cb3e
3 changed files with 55 additions and 22 deletions

View file

@ -13,3 +13,11 @@ deceleration : 1.1
# Projectile # Projectile
max_fire_rate : 10 max_fire_rate : 10
# Controls (not used yet)
up : z
right : d
down : s
left : q
fire :
pause : escape

44
main.py
View file

@ -4,6 +4,7 @@
### Author: Arthur 'Grizzly' Grisel-Davy ### Author: Arthur 'Grizzly' Grisel-Davy
import pygame import pygame
import yaml
from models import Game, Player, Carte, Camera, Enemy, Hud from models import Game, Player, Carte, Camera, Enemy, Hud
@ -14,7 +15,6 @@ screen_width = 1200
screen_height = 800 screen_height = 800
center_screen = (int(screen_width/2),int(screen_height/2)) center_screen = (int(screen_width/2),int(screen_height/2))
screen = pygame.display.set_mode((screen_width, screen_height)) screen = pygame.display.set_mode((screen_width, screen_height))
done = False
BACKGROUND=(200,200,200) BACKGROUND=(200,200,200)
@ -22,9 +22,22 @@ 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)
pause_screen = pygame.Surface((screen_width,screen_height))
pause_screen.set_alpha(128)
pause_screen.fill((255,255,255))
carte = Carte(n=5) carte = Carte(n=5)
# possible positions: 300,300: 1400,1000: 2700,400, 2800,1600
# click = 323
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,'perso.png','canon.png','projectile1_right.png')
camera = Camera(start_pos,screen_width,screen_height,0.3) camera = Camera(start_pos,screen_width,screen_height,0.3)
@ -34,21 +47,30 @@ game = Game(carte,player,camera,[],hud)
clock = pygame.time.Clock() clock = pygame.time.Clock()
done = False
while not done: while not done:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
done = True done = True
keystate = pygame.key.get_pressed() + pygame.mouse.get_pressed() keystate = pygame.key.get_pressed() + pygame.mouse.get_pressed()
if keystate[pygame.K_ESCAPE]:
game.pause = not game.pause
if not game.pause:
player.check_keys(keystate,screen_width,screen_height,carte,camera) player.check_keys(keystate,screen_width,screen_height,carte,camera)
screen.fill(BACKGROUND) screen.fill(BACKGROUND)
game.draw(screen) game.draw(screen)
pygame.display.flip()
if game.pause:
screen.blit(pause_screen, (0,0))
pygame.display.flip()
clock.tick(60) clock.tick(60)

View file

@ -24,6 +24,7 @@ with open(r'./config.yaml') as file:
class Game(): class Game():
def __init__(self,carte,player,camera,enemies,hud): def __init__(self,carte,player,camera,enemies,hud):
self.pause = False
self.carte = carte self.carte = carte
self.player = player self.player = player
self.camera = camera self.camera = camera
@ -38,7 +39,8 @@ class Game():
to_remove = [] to_remove = []
for k,proj in enumerate(self.player.projectiles): for k,proj in enumerate(self.player.projectiles):
if not proj.is_out(self.carte,self): if not proj.is_out(self.carte,self):
proj.move() if not self.pause:
proj.move()
proj.draw(surface,self.camera) proj.draw(surface,self.camera)
else: else:
to_remove.append(k) to_remove.append(k)
@ -49,7 +51,7 @@ class Game():
for enemy in self.enemies: for enemy in self.enemies:
enemy.draw(surface,self.camera) enemy.draw(surface,self.camera)
self.player.draw(surface,self.camera) self.player.draw(surface,self.camera,self)
self.camera.draw(surface) self.camera.draw(surface)
self.hud.draw(surface) self.hud.draw(surface)
@ -234,7 +236,7 @@ class Player():
self.fire('bullet','asset/projectile') self.fire('bullet','asset/projectile')
def draw(self,surface,camera): def draw(self,surface,camera,game):
#Get offset and compute player position in screen #Get offset and compute player position in screen
offsetx,offsety = camera.get_offset() offsetx,offsety = camera.get_offset()
@ -242,13 +244,14 @@ class Player():
posy_screen = self.posy-offsety posy_screen = self.posy-offsety
#Calculate canon rotation: #Calculate canon rotation:
x_mouse, y_mouse = pygame.mouse.get_pos() if not game.pause:
if x_mouse==posx_screen: x_mouse, y_mouse = pygame.mouse.get_pos()
x_mouse+=0.1 if x_mouse==posx_screen:
self.degres_canon = -1*degrees(atan((y_mouse-posy_screen)/(x_mouse-posx_screen))) x_mouse+=0.1
if x_mouse < posx_screen: self.degres_canon = -1*degrees(atan((y_mouse-posy_screen)/(x_mouse-posx_screen)))
self.degres_canon = 180+self.degres_canon if x_mouse < posx_screen:
self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon) self.degres_canon = 180+self.degres_canon
self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon)
# Get rects # Get rects
self.rect = self.player.get_rect() self.rect = self.player.get_rect()