This repository has been archived on 2019-12-09. You can view files and clone it, but cannot push or open issues or pull requests.
pacman/livrables/1/graphic.py

68 lines
2.1 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import pygame as pg
import pacman_sprite
import pacman as m_pacman # m_ for module to avoid conflicts
from physic_engine import PhysicEngine
import pacmap as m_pacmap
import sys
pg.init()
class Screen:
def __init__(self, size, pacmap: m_pacmap.Map, physic_engine: PhysicEngine, pacman: m_pacman.Pacman):
self.screen = pg.display.set_mode(size)
# self.screen.set_caption("Pacman")
self.physic_engine = physic_engine
self.pacman = pacman
self.pacman_sprite = pacman_sprite.PacmanSprite(size[0]/28)
self.clock = pg.time.Clock()
self.max_fps = 40
self.entity_group = pg.sprite.Group(self.pacman_sprite)
self.loop()
def user_events(self):
key = pg.key.get_pressed()
if key[pg.K_UP]:
pacman.set_next_dir(m_pacman.direction.up)
if key[pg.K_DOWN]:
pacman.set_next_dir(m_pacman.direction.down)
if key[pg.K_LEFT]:
pacman.set_next_dir(m_pacman.direction.left)
if key[pg.K_RIGHT]:
pacman.set_next_dir(m_pacman.direction.right)
def refresh(self):
"""refresh/redraw all"""
pac_x, pac_y = self.pacman.position
pac_res = self.pacman.resolution
pacmap.draw(self.screen)
self.pacman_sprite.rect.x = int(pac_x / 28 / pac_res * self.screen.get_width()) - 10
self.pacman_sprite.rect.y = int(pac_y / 31 / pac_res * self.screen.get_height()) - 10
self.entity_group.draw(self.screen)
def create_maze_surface(self):
pass
def loop(self):
while 1:
self.screen.fill((0, 0, 0))
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
self.user_events()
self.physic_engine.move_all()
self.refresh()
self.clock.tick(self.max_fps)
pg.display.flip()
if __name__ == '__main__':
pacman = m_pacman.Pacman((1,1))
pacmap = m_pacmap.Map(maze_img_file="pacmap_maze1.png")
phys_engine = PhysicEngine(pacmap, pacman)
screen = Screen((560, 620), pacmap, phys_engine, pacman)