From f53f91083300879012b94696252e96ff81aa37b4 Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Sun, 17 Nov 2019 19:09:07 +0100 Subject: [PATCH] =?UTF-8?q?README=20compl=C3=A9t=C3=A9=20-=20=C3=A9puratio?= =?UTF-8?q?n=20du=20code=20source?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- livrables/1/README.txt | 19 ++++++++++++++++++ livrables/1/pacman.py | 1 - src/graphic.py | 19 +++++++----------- src/pacman.py | 16 +-------------- src/pacmap.py | 1 - src/physic_engine.py | 21 ++++++++++++++++++++ src/physic_motor.py | 45 ------------------------------------------ 7 files changed, 48 insertions(+), 74 deletions(-) create mode 100755 src/physic_engine.py delete mode 100755 src/physic_motor.py diff --git a/livrables/1/README.txt b/livrables/1/README.txt index e69de29..1aef940 100644 --- a/livrables/1/README.txt +++ b/livrables/1/README.txt @@ -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. \ No newline at end of file diff --git a/livrables/1/pacman.py b/livrables/1/pacman.py index 867a261..4411f3e 100755 --- a/livrables/1/pacman.py +++ b/livrables/1/pacman.py @@ -34,7 +34,6 @@ class Pacman: self.super_power = 0 # Counter of super pacdots in effect (> 0 means super power is active) self.ghost_combo = 0 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.map_size = map_size diff --git a/src/graphic.py b/src/graphic.py index e8b092b..d88acc9 100755 --- a/src/graphic.py +++ b/src/graphic.py @@ -3,23 +3,22 @@ import pygame as pg import pacman_sprite 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 sys pg.init() 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.set_caption("Pacman") - self.physic_motor = physic_motor + 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 = 30 + self.max_fps = 40 self.entity_group = pg.sprite.Group(self.pacman_sprite) - self.loop() @@ -33,7 +32,6 @@ class Screen: 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""" @@ -42,10 +40,7 @@ class 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.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) - # print(self.entity_group) def create_maze_surface(self): @@ -59,7 +54,7 @@ class Screen: sys.exit() self.user_events() - self.physic_motor.move_all() + self.physic_engine.move_all() self.refresh() self.clock.tick(self.max_fps) @@ -68,5 +63,5 @@ class Screen: 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) + phys_engine = PhysicEngine(pacmap, pacman) + screen = Screen((560, 620), pacmap, phys_engine, pacman) diff --git a/src/pacman.py b/src/pacman.py index 25d4f5c..0e0a901 100755 --- a/src/pacman.py +++ b/src/pacman.py @@ -17,13 +17,6 @@ pacdot_counter = 88 score = 0 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): 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.ghost_combo = 0 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.map_size = map_size 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) def next_matrix_position(self): 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] - print(self.position, next_x, next_y) return next_x, next_y def has_super_power(self): @@ -75,7 +65,7 @@ class Pacman: self.super_power += 1 # TODO - # Requires UNIX + # Requires UNIX - or async func #pid = os.fork() #if pid == 0: # return @@ -83,7 +73,6 @@ class Pacman: #os.sleep(10) self.super_power -= 1 self.ghost_combo = 0 - #os._exit(0) def eat_fruit(self, fruit, dot_map): global score @@ -129,12 +118,9 @@ class Pacman: """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_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 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[1] = (self.position[1] + self.direction[1]) % (self.resolution * self.map_size[1]) diff --git a/src/pacmap.py b/src/pacmap.py index a5869bd..99a9acf 100755 --- a/src/pacmap.py +++ b/src/pacmap.py @@ -57,7 +57,6 @@ class Map: """ self._surf = pygame.Surface((Map.width * 20, Map.height * 20)) if maze_img_file and os.path.isfile(maze_img_file): - print('coucou') try: self.phys_map = self.decode_map(maze_img_file) except Exception as e: diff --git a/src/physic_engine.py b/src/physic_engine.py new file mode 100755 index 0000000..5947932 --- /dev/null +++ b/src/physic_engine.py @@ -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() \ No newline at end of file diff --git a/src/physic_motor.py b/src/physic_motor.py deleted file mode 100755 index 5037303..0000000 --- a/src/physic_motor.py +++ /dev/null @@ -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() \ No newline at end of file