bon, pacman peut retenir son prochain changement de direction - collisions avec les murs bien gérées + réglage 2-3 soucis | TODO: affichage carte en fond et sûrement modifier l'endroit où pacman est dessiné en prenant en compte la taille de sa Rect, à voir avec le fond

This commit is contained in:
DylanVsn 2019-11-15 19:03:57 +01:00
parent 86e56be3e5
commit fe90a5a145
4 changed files with 99 additions and 20 deletions

View File

@ -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):

View File

@ -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):
@ -103,9 +104,34 @@ class Pacman:
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

View File

@ -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
@ -152,6 +161,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)"""
if x < 0 and y < 0:
@ -196,3 +227,10 @@ def decode_map(img_file):
raise ValueError("Pixel " + str(col) + "," + str(row) + " is invalid")
matrix[-1].append(tile)
return matrix
def get_one_in_digit(number):
cpt = 0
while number:
cpt += number & 1
number //= 2
return cpt

View File

@ -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:
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):
# 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()