README complété - épuration du code source

This commit is contained in:
DylanVsn 2019-11-17 19:09:07 +01:00
parent 6190790a28
commit f53f910833
7 changed files with 48 additions and 74 deletions

View File

@ -0,0 +1,19 @@
Livrable 1 - déplacement et affichage du pacman
Prérequis:
- Python 3
modules Python requis:
- Pygame
- PIL (pillow)
On pourra installer ces modules avec
pip3 install pillow pygame
Le module à lancer pour voir l'avancée du projet est graphic.py
python3 graphic.py
Les touches associées au déplacement du joueur sont les flèches directionnelles.
La carte est chargée depuis l'image pacmap_maze1.png, les collisions et les
téléporteurs sont gérés ainsi que la mémoire de la prochaine direction voulue
par l'utilisateur.

View File

@ -34,7 +34,6 @@ class Pacman:
self.super_power = 0 # Counter of super pacdots in effect (> 0 means super power is active) self.super_power = 0 # Counter of super pacdots in effect (> 0 means super power is active)
self.ghost_combo = 0 self.ghost_combo = 0
self.size = (1.8, 1.8) # size related to tile size self.size = (1.8, 1.8) # size related to tile size
self.speed = 0.1
self.resolution = resolution # when pacman in 0:10 he's in the 1st cell if resolution=10 !!! must be even number self.resolution = resolution # when pacman in 0:10 he's in the 1st cell if resolution=10 !!! must be even number
self.map_size = map_size self.map_size = map_size

View File

@ -3,23 +3,22 @@
import pygame as pg import pygame as pg
import pacman_sprite import pacman_sprite
import pacman as m_pacman # m_ for module to avoid conflicts import pacman as m_pacman # m_ for module to avoid conflicts
from physic_motor import PhysicMotor from physic_engine import PhysicEngine
import pacmap as m_pacmap import pacmap as m_pacmap
import sys import sys
pg.init() pg.init()
class Screen: class Screen:
def __init__(self, size, pacmap: m_pacmap.Map, physic_motor: PhysicMotor, pacman: m_pacman.Pacman): def __init__(self, size, pacmap: m_pacmap.Map, physic_engine: PhysicEngine, pacman: m_pacman.Pacman):
self.screen = pg.display.set_mode(size) self.screen = pg.display.set_mode(size)
# self.screen.set_caption("Pacman") # self.screen.set_caption("Pacman")
self.physic_motor = physic_motor self.physic_engine = physic_engine
self.pacman = pacman self.pacman = pacman
self.pacman_sprite = pacman_sprite.PacmanSprite(size[0]/28) self.pacman_sprite = pacman_sprite.PacmanSprite(size[0]/28)
self.clock = pg.time.Clock() self.clock = pg.time.Clock()
self.max_fps = 30 self.max_fps = 40
self.entity_group = pg.sprite.Group(self.pacman_sprite) self.entity_group = pg.sprite.Group(self.pacman_sprite)
self.loop() self.loop()
@ -33,7 +32,6 @@ class Screen:
pacman.set_next_dir(m_pacman.direction.left) pacman.set_next_dir(m_pacman.direction.left)
if key[pg.K_RIGHT]: if key[pg.K_RIGHT]:
pacman.set_next_dir(m_pacman.direction.right) pacman.set_next_dir(m_pacman.direction.right)
# print(pacman.direction)
def refresh(self): def refresh(self):
"""refresh/redraw all""" """refresh/redraw all"""
@ -42,10 +40,7 @@ class Screen:
pacmap.draw(self.screen) pacmap.draw(self.screen)
self.pacman_sprite.rect.x = int(pac_x / 28 / pac_res * self.screen.get_width()) - 10 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.pacman_sprite.rect.y = int(pac_y / 31 / pac_res * self.screen.get_height()) - 10
# print(self.pacman_sprite.rect.x, self.pacman_sprite.rect.y, self.pacman_sprite.rect.size)
self.entity_group.draw(self.screen) self.entity_group.draw(self.screen)
# print(self.entity_group)
def create_maze_surface(self): def create_maze_surface(self):
@ -59,7 +54,7 @@ class Screen:
sys.exit() sys.exit()
self.user_events() self.user_events()
self.physic_motor.move_all() self.physic_engine.move_all()
self.refresh() self.refresh()
self.clock.tick(self.max_fps) self.clock.tick(self.max_fps)
@ -68,5 +63,5 @@ class Screen:
if __name__ == '__main__': if __name__ == '__main__':
pacman = m_pacman.Pacman((1,1)) pacman = m_pacman.Pacman((1,1))
pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png") pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png")
phys_motor = PhysicMotor(pacmap, pacman) phys_engine = PhysicEngine(pacmap, pacman)
screen = Screen((560, 620), pacmap, phys_motor, pacman) screen = Screen((560, 620), pacmap, phys_engine, pacman)

View File

@ -17,13 +17,6 @@ pacdot_counter = 88
score = 0 score = 0
lives = 3 lives = 3
# p-e à remplacer par la namedtuple direction plus haut
# class Direction(IntEnum):
# U = 0
# D = 1
# R = 2
# L = 3
class FruitType(IntEnum): class FruitType(IntEnum):
A = 0 A = 0
@ -42,18 +35,15 @@ class Pacman:
self.super_power = 0 # Counter of super pacdots in effect (> 0 means super power is active) self.super_power = 0 # Counter of super pacdots in effect (> 0 means super power is active)
self.ghost_combo = 0 self.ghost_combo = 0
self.size = (1.8, 1.8) # size related to tile size self.size = (1.8, 1.8) # size related to tile size
self.speed = 0.1
self.resolution = resolution # when pacman in 0:10 he's in the 1st cell if resolution=10 !!! must be odd number self.resolution = resolution # when pacman in 0:10 he's in the 1st cell if resolution=10 !!! must be odd number
self.map_size = map_size self.map_size = map_size
def matrix_position(self): def matrix_position(self):
# return (int(self.position[0]), int(self.position[1]))
return int(self.position[0] / self.resolution), int(self.position[1] / self.resolution) return int(self.position[0] / self.resolution), int(self.position[1] / self.resolution)
def next_matrix_position(self): def next_matrix_position(self):
next_x = int(self.position[0] / self.resolution + self.direction[0]) % self.map_size[0] next_x = int(self.position[0] / self.resolution + self.direction[0]) % self.map_size[0]
next_y = int(self.position[1] / self.resolution + self.direction[1]) % self.map_size[1] next_y = int(self.position[1] / self.resolution + self.direction[1]) % self.map_size[1]
print(self.position, next_x, next_y)
return next_x, next_y return next_x, next_y
def has_super_power(self): def has_super_power(self):
@ -75,7 +65,7 @@ class Pacman:
self.super_power += 1 self.super_power += 1
# TODO # TODO
# Requires UNIX # Requires UNIX - or async func
#pid = os.fork() #pid = os.fork()
#if pid == 0: #if pid == 0:
# return # return
@ -83,7 +73,6 @@ class Pacman:
#os.sleep(10) #os.sleep(10)
self.super_power -= 1 self.super_power -= 1
self.ghost_combo = 0 self.ghost_combo = 0
#os._exit(0)
def eat_fruit(self, fruit, dot_map): def eat_fruit(self, fruit, dot_map):
global score global score
@ -129,12 +118,9 @@ class Pacman:
"""return x, y corresponding to the tile if we move with next_direction""" """return x, y corresponding to the tile if we move with next_direction"""
next_x = int(self.position[0] / self.resolution + self.next_direction[0]) % self.map_size[0] next_x = int(self.position[0] / self.resolution + self.next_direction[0]) % self.map_size[0]
next_y = int(self.position[1] / self.resolution + self.next_direction[1]) % self.map_size[1] next_y = int(self.position[1] / self.resolution + self.next_direction[1]) % self.map_size[1]
print(self.position, next_x, next_y)
return next_x, next_y return next_x, next_y
def move(self): def move(self):
# self.position[0] += self.direction[0] * self.speed
# self.position[1] += self.direction[1] * self.speed
self.position[0] = (self.position[0] + self.direction[0]) % (self.resolution * self.map_size[0]) self.position[0] = (self.position[0] + self.direction[0]) % (self.resolution * self.map_size[0])
self.position[1] = (self.position[1] + self.direction[1]) % (self.resolution * self.map_size[1]) self.position[1] = (self.position[1] + self.direction[1]) % (self.resolution * self.map_size[1])

View File

@ -57,7 +57,6 @@ class Map:
""" """
self._surf = pygame.Surface((Map.width * 20, Map.height * 20)) self._surf = pygame.Surface((Map.width * 20, Map.height * 20))
if maze_img_file and os.path.isfile(maze_img_file): if maze_img_file and os.path.isfile(maze_img_file):
print('coucou')
try: try:
self.phys_map = self.decode_map(maze_img_file) self.phys_map = self.decode_map(maze_img_file)
except Exception as e: except Exception as e:

21
src/physic_engine.py Executable file
View File

@ -0,0 +1,21 @@
from pacmap import *
from pacman import *
class PhysicEngine:
def __init__(self, c_pacmap: Map, c_pacman: Pacman):
self.pacmap = c_pacmap
self.pacman = c_pacman
self.entities = [] # ghosts
def move_all(self):
# pacman movement
next_pac_tile = self.pacman.next_matrix_position()
pac_res = self.pacman.resolution
if self.pacman.is_at_center_tile():
if self.pacman.next_direction != direction.none and self.pacmap.get_tile(*self.pacman.get_next_dir_tile()) in (PhysTile.GRD, PhysTile.TPT):
self.pacman.change_to_next_dir()
self.pacman.move()
elif self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT):
self.pacman.move()
else:
self.pacman.move()

View File

@ -1,45 +0,0 @@
from pacmap import *
from pacman import *
class PhysicMotor:
def __init__(self, c_pacmap: Map, c_pacman: Pacman):
self.pacmap = c_pacmap
self.pacman = c_pacman
self.entities = [] # ghosts
def move_all(self):
# pacman movement
print(self.pacman.position)
# pac_x, pac_y = self.pacman.matrix_position()
next_pac_tile = self.pacman.next_matrix_position()
pac_res = self.pacman.resolution
# print(pac_x, pac_y, self.pacmap.get_tile(pac_x, pac_y))
# print("next:", self.pacmap.get_tile(pac_x + self.pacman.direction[0], pac_y + self.pacman.direction[1]))
# if self.pacman.direction in (direction.up, direction.down):
# if self.pacman.position[1] % pac_res == pac_res / 2:
# if self.pacmap.get_tile(self.pacman.get_next_dir_tile()) in (PhysTile.GRD, PhysTile.TPT):
# self.pacman.change_to_next_dir()
# self.pacman.move()
# elif self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT):
# self.pacman.move()
# else:
# self.pacman.move()
# else:
# if self.pacman.position[0] % pac_res == pac_res / 2:
# if self.pacmap.get_tile(self.pacman.get_next_dir_tile()) in (PhysTile.GRD, PhysTile.TPT):
# self.pacman.change_to_next_dir()
# self.pacman.move()
# elif self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT):
# self.pacman.move()
# else:
# self.pacman.move()
if self.pacman.is_at_center_tile():
if self.pacman.next_direction != direction.none and self.pacmap.get_tile(*self.pacman.get_next_dir_tile()) in (PhysTile.GRD, PhysTile.TPT):
self.pacman.change_to_next_dir()
self.pacman.move()
elif self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT):
self.pacman.move()
else:
self.pacman.move()