From b6622fc8d5ca7673ffacaa3573494547dbbf725e Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Wed, 13 Nov 2019 20:23:35 +0100 Subject: [PATCH] =?UTF-8?q?bon=20collision=20mur=20ok=20-=20faudra=20g?= =?UTF-8?q?=C3=A9rer=20la=20case=20temp=20de=20dplct=20de=20pacman=20pour?= =?UTF-8?q?=20le=20prochain=20croisement=20-=20graphique=20faut=20afficher?= =?UTF-8?q?=20la=20carte=20lel=20et=20voil=C3=A0=20pour=20aujourd'hui?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graphic.py | 26 +++++++++++++++++++------- src/pacman.py | 15 +++++++++------ src/pacman_sprite.py | 3 ++- src/pacmap.py | 6 ++++-- src/physic_motor.py | 18 +++++++++++++++--- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/graphic.py b/src/graphic.py index 4337e34..8fa4276 100755 --- a/src/graphic.py +++ b/src/graphic.py @@ -18,6 +18,7 @@ class Screen: 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() @@ -32,28 +33,39 @@ class Screen: pacman.change_dir(m_pacman.direction.left) if key[pg.K_RIGHT]: pacman.change_dir(m_pacman.direction.right) + # print(pacman.direction) def refresh(self): """refresh/redraw all""" - pass + 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.user_events() + self.physic_motor.move_all() + self.refresh() - self.clock.tick(max_fps) + 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_maze_1.png") + pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png") phys_motor = PhysicMotor(pacmap, pacman) - screen = Screen((280, 310), pacmap, phys_motor, pacman) \ No newline at end of file + screen = Screen((560, 620), pacmap, phys_motor, pacman) \ No newline at end of file diff --git a/src/pacman.py b/src/pacman.py index eedb643..f3589e5 100755 --- a/src/pacman.py +++ b/src/pacman.py @@ -34,17 +34,19 @@ class Fruit: class Pacman: - def __init__(self, position=[0, 0]): - self.position = list(position) + def __init__(self, position=[0, 0], resolution=10): + self.position = [position[0]+resolution, position[1]+resolution] self.direction = direction.right self.next_direction = direction.right 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 def matrix_position(self): - return (int(self.position[0]), int(self.position[1])) + # return (int(self.position[0]), int(self.position[1])) + return int(self.position[0] / self.resolution), int(self.position[1] / self.resolution) def has_super_power(self): return self.super_power > 0 @@ -98,9 +100,10 @@ class Pacman: self.direction = new_dir def move(self): - self.position[0] = self.position[0] + self.direction[0] * self.speed - self.position[1] = self.position[1] + self.direction[1] * self.speed - + # self.position[0] += self.direction[0] * self.speed + # self.position[1] += self.direction[1] * self.speed + self.position[0] += self.direction[0] + self.position[1] += self.direction[1] def game_over(status = "lose"): #TODO diff --git a/src/pacman_sprite.py b/src/pacman_sprite.py index a3aeb6c..4ecfe76 100755 --- a/src/pacman_sprite.py +++ b/src/pacman_sprite.py @@ -5,7 +5,8 @@ class PacmanSprite(pg.sprite.Sprite): def __init__(self, size): super().__init__() self.image = pg.Surface([size, size]) - pg.draw.circle(self.image, (255, 255, 0), (size/2, size/2), size/2, width=0) + half_size = int(size / 2) + pg.draw.circle(self.image, (255, 255, 0), (half_size, half_size), half_size) # Fetch the rectangle object that has the dimensions of the image # Update the position of this object by setting the values of rect.x and rect.y diff --git a/src/pacmap.py b/src/pacmap.py index 209b1da..a48ef2e 100755 --- a/src/pacmap.py +++ b/src/pacmap.py @@ -46,12 +46,14 @@ class Map: 2: big pac-dot """ if maze_img_file and os.path.isfile(maze_img_file): + print('coucou') try: self.phys_map = decode_map(maze_img_file) except Exception as e: raise e - self.phys_map = phys_map # in the first part we assume phys_map is correct and no need to verify - self.dots_map = dots_map + else: + self.phys_map = phys_map # in the first part we assume phys_map is correct and no need to verify + self.dots_map = dots_map self.intersect_map = [] # TODO - the layer which contains intersections pre-calculated diff --git a/src/physic_motor.py b/src/physic_motor.py index 2a80a6b..54c2916 100755 --- a/src/physic_motor.py +++ b/src/physic_motor.py @@ -9,8 +9,20 @@ class PhysicMotor: def move_all(self): # pacman movement - pac_x, pac_y = self.pacman.position + print(self.pacman.position) + pac_x, pac_y = self.pacman.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 pac_y % 1 == 0.5: # change this in the future + if self.pacman.position[1] % pac_res == pac_res / 2: # change this in the future if self.pacmap.get_tile(pac_x, pac_y + self.pacman.direction[1]) in (PhysTile.GRD, PhysTile.TPT): - self.pacman.move() \ No newline at end of file + self.pacman.move() + else: + self.pacman.move() + else: + if self.pacman.position[0] % pac_res == pac_res / 2: # change this in the future + if self.pacmap.get_tile(pac_x + self.pacman.direction[0], pac_y) in (PhysTile.GRD, PhysTile.TPT): + self.pacman.move() + else: + self.pacman.move() \ No newline at end of file