2019-11-13 18:01:59 +01:00
|
|
|
#!/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
|
2019-11-13 20:23:35 +01:00
|
|
|
self.entity_group = pg.sprite.Group(self.pacman_sprite)
|
2019-11-13 18:01:59 +01:00
|
|
|
|
|
|
|
self.loop()
|
|
|
|
|
|
|
|
|
|
|
|
def user_events(self):
|
|
|
|
key = pg.key.get_pressed()
|
|
|
|
if key[pg.K_UP]:
|
2019-11-15 19:03:57 +01:00
|
|
|
pacman.set_next_dir(m_pacman.direction.up)
|
2019-11-13 18:01:59 +01:00
|
|
|
if key[pg.K_DOWN]:
|
2019-11-15 19:03:57 +01:00
|
|
|
pacman.set_next_dir(m_pacman.direction.down)
|
2019-11-13 18:01:59 +01:00
|
|
|
if key[pg.K_LEFT]:
|
2019-11-15 19:03:57 +01:00
|
|
|
pacman.set_next_dir(m_pacman.direction.left)
|
2019-11-13 18:01:59 +01:00
|
|
|
if key[pg.K_RIGHT]:
|
2019-11-15 19:03:57 +01:00
|
|
|
pacman.set_next_dir(m_pacman.direction.right)
|
2019-11-13 20:23:35 +01:00
|
|
|
# print(pacman.direction)
|
2019-11-13 18:01:59 +01:00
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
"""refresh/redraw all"""
|
2019-11-13 20:23:35 +01:00
|
|
|
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)
|
|
|
|
|
2019-11-13 18:01:59 +01:00
|
|
|
|
|
|
|
def create_maze_surface(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
while 1:
|
2019-11-13 20:23:35 +01:00
|
|
|
self.screen.fill((0, 0, 0))
|
2019-11-13 18:01:59 +01:00
|
|
|
for event in pg.event.get():
|
|
|
|
if event.type == pg.QUIT:
|
|
|
|
sys.exit()
|
|
|
|
|
2019-11-13 20:23:35 +01:00
|
|
|
self.user_events()
|
|
|
|
self.physic_motor.move_all()
|
|
|
|
self.refresh()
|
2019-11-13 18:01:59 +01:00
|
|
|
|
2019-11-13 20:23:35 +01:00
|
|
|
self.clock.tick(self.max_fps)
|
|
|
|
pg.display.flip()
|
2019-11-13 18:01:59 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pacman = m_pacman.Pacman((1,1))
|
2019-11-13 20:23:35 +01:00
|
|
|
pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png")
|
2019-11-13 18:01:59 +01:00
|
|
|
phys_motor = PhysicMotor(pacmap, pacman)
|
2019-11-13 20:23:35 +01:00
|
|
|
screen = Screen((560, 620), pacmap, phys_motor, pacman)
|