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/src/graphic.py

71 lines
2.2 KiB
Python
Raw 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_motor import PhysicMotor
import pacmap as m_pacmap
import sys
pg.init()
class Screen:
def __init__(self, size, pacmap: m_pacmap.Map, physic_motor: PhysicMotor, pacman: m_pacman.Pacman):
self.screen = pg.display.set_mode(size)
# self.screen.set_caption("Pacman")
self.physic_motor = physic_motor
self.pacman = pacman
self.pacman_sprite = pacman_sprite.PacmanSprite(size[0]/28)
self.clock = pg.time.Clock()
self.max_fps = 30
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)
# print(pacman.direction)
def refresh(self):
"""refresh/redraw all"""
pac_x, pac_y = self.pacman.position
pac_res = self.pacman.resolution
self.pacman_sprite.rect.x = int(pac_x / 28 / pac_res * self.screen.get_width())
self.pacman_sprite.rect.y = int(pac_y / 31 / pac_res * self.screen.get_height())
# print(self.pacman_sprite.rect.x, self.pacman_sprite.rect.y, self.pacman_sprite.rect.size)
self.entity_group.draw(self.screen)
# print(self.entity_group)
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_motor.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_motor = PhysicMotor(pacmap, pacman)
screen = Screen((560, 620), pacmap, phys_motor, pacman)