From fe90a5a145d66adaf43d44bbe7ada603ae23df06 Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Fri, 15 Nov 2019 19:03:57 +0100 Subject: [PATCH] =?UTF-8?q?bon,=20pacman=20peut=20retenir=20son=20prochain?= =?UTF-8?q?=20changement=20de=20direction=20-=20collisions=20avec=20les=20?= =?UTF-8?q?murs=20bien=20g=C3=A9r=C3=A9es=20+=20r=C3=A9glage=202-3=20souci?= =?UTF-8?q?s=20|=20TODO:=20affichage=20carte=20en=20fond=20et=20s=C3=BBrem?= =?UTF-8?q?ent=20modifier=20l'endroit=20o=C3=B9=20pacman=20est=20dessin?= =?UTF-8?q?=C3=A9=20en=20prenant=20en=20compte=20la=20taille=20de=20sa=20R?= =?UTF-8?q?ect,=20=C3=A0=20voir=20avec=20le=20fond?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graphic.py | 8 ++++---- src/pacman.py | 36 +++++++++++++++++++++++++++++++----- src/pacmap.py | 40 +++++++++++++++++++++++++++++++++++++++- src/physic_motor.py | 35 +++++++++++++++++++++++++---------- 4 files changed, 99 insertions(+), 20 deletions(-) diff --git a/src/graphic.py b/src/graphic.py index 8fa4276..2aa147b 100755 --- a/src/graphic.py +++ b/src/graphic.py @@ -26,13 +26,13 @@ class Screen: def user_events(self): key = pg.key.get_pressed() if key[pg.K_UP]: - pacman.change_dir(m_pacman.direction.up) + pacman.set_next_dir(m_pacman.direction.up) if key[pg.K_DOWN]: - pacman.change_dir(m_pacman.direction.down) + pacman.set_next_dir(m_pacman.direction.down) if key[pg.K_LEFT]: - pacman.change_dir(m_pacman.direction.left) + pacman.set_next_dir(m_pacman.direction.left) if key[pg.K_RIGHT]: - pacman.change_dir(m_pacman.direction.right) + pacman.set_next_dir(m_pacman.direction.right) # print(pacman.direction) def refresh(self): diff --git a/src/pacman.py b/src/pacman.py index 568d038..25d4f5c 100755 --- a/src/pacman.py +++ b/src/pacman.py @@ -5,12 +5,13 @@ import os from enum import IntEnum from collections import namedtuple -_tempdir = namedtuple("Direction", ["up", "down", "left", "right"]) +_tempdir = namedtuple("Direction", ["up", "down", "left", "right", "none"]) direction = _tempdir( up= (0, -1), down=(0, 1), left=(-1, 0), - right=(1, 0) + right=(1, 0), + none=(0, 0) ) pacdot_counter = 88 score = 0 @@ -35,14 +36,14 @@ class Fruit: class Pacman: def __init__(self, position=[0, 0], map_size=(28, 31), resolution=10): - self.position = [position[0]+int(resolution/2), position[1]+int(resolution/2)] + self.position = [position[0]*resolution+int(resolution/2), position[1]*resolution+int(resolution/2)] self.direction = direction.right - self.next_direction = direction.right + self.next_direction = direction.none 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 + 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): @@ -102,10 +103,35 @@ class Pacman: lives -= 1 if lives < 0: # à vérifier game_over() + + def is_at_center_tile(self): + """return True if pacman is at the center of a tile else False""" + clause_1 = not self.position[0] % (self.resolution/2) and self.position[0] % self.resolution + clause_2 = not self.position[1] % (self.resolution/2) and self.position[1] % self.resolution + return clause_1 and clause_2 def change_dir(self, new_dir): self.direction = new_dir + def set_next_dir(self, next_dir): + if next_dir[0] and self.direction[0]: + self.change_dir(next_dir) + elif next_dir[1] and self.direction[1]: + self.change_dir(next_dir) + else: + self.next_direction = next_dir + + def change_to_next_dir(self): + self.direction = self.next_direction + self.next_direction = direction.none + + def get_next_dir_tile(self): + """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 diff --git a/src/pacmap.py b/src/pacmap.py index 7bf10cb..2eff264 100755 --- a/src/pacmap.py +++ b/src/pacmap.py @@ -20,6 +20,15 @@ class PhysTile(IntEnum): FIT = 6 # fully inaccessible tile # ghost-cell ground as FIT ? verify no pac-dot here +class CrossTile(IntEnum): + # code where we can find another GRD/TPT Tile from a given Tile + # with binary masks + UP = 1 # 0001 + DOWN = 2 # 0010 + LEFT = 4 # 0100 + RIGHT = 8 # 1000 + # direction changement if != 3 or != 12 + class Map: """ Pacman maps size is 28×31 @@ -151,6 +160,28 @@ class Map: def get_tile(self, x, y): return self.phys_map[y][x] + + def create_cross_layer(self): + dictionnary = { + (0, 1):CrossTile.DOWN, + (0, -1): CrossTile.UP, + (1, 0): CrossTile.RIGHT, + (-1, 0): CrossTile.LEFT + } + for row in range(self.height): + self.intersect_map.append([]) + for col in range(self.width): + if not self.get_tile(col, row) in (PhysTile.GRD, PhysTile.TPT): + self.intersect_map[-1].append(0) + if self.get_tile(col, row) in (PhysTile.GRD, PhysTile.TPT): + cpt = 0 + for dir in dictionnary: + test_col = (col + dir[0]) % self.width + test_row = (row + dir[1]) % self.height + if self.get_tile(test_col, test_row) in (PhysTile.GRD, PhysTile.TPT): + cpt |= dictionnary[dir] + self.intersect_map[-1].append(cpt) + def explore(matrix, x=-1, y=-1): """explore the given matrix and change it (GRD and TPT become FIT)""" @@ -195,4 +226,11 @@ def decode_map(img_file): except: raise ValueError("Pixel " + str(col) + "," + str(row) + " is invalid") matrix[-1].append(tile) - return matrix \ No newline at end of file + return matrix + +def get_one_in_digit(number): + cpt = 0 + while number: + cpt += number & 1 + number //= 2 + return cpt \ No newline at end of file diff --git a/src/physic_motor.py b/src/physic_motor.py index 782b6bd..5037303 100755 --- a/src/physic_motor.py +++ b/src/physic_motor.py @@ -16,15 +16,30 @@ class PhysicMotor: # 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: # change this in the future - if self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT): - self.pacman.move() - else: + # 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() - else: - if self.pacman.position[0] % pac_res == pac_res / 2: # change this in the future - if self.pacmap.get_tile(*next_pac_tile) in (PhysTile.GRD, PhysTile.TPT): + 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 + else: + self.pacman.move() \ No newline at end of file