bon collision mur ok - faudra gérer la case temp de dplct de pacman pour le prochain croisement - graphique faut afficher la carte lel et voilà pour aujourd'hui

This commit is contained in:
DylanVsn 2019-11-13 20:23:35 +01:00
parent ea1e4e6724
commit b6622fc8d5
5 changed files with 49 additions and 19 deletions

View File

@ -18,6 +18,7 @@ class Screen:
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 = 30
self.entity_group = pg.sprite.Group(self.pacman_sprite)
self.loop() self.loop()
@ -32,28 +33,39 @@ class Screen:
pacman.change_dir(m_pacman.direction.left) pacman.change_dir(m_pacman.direction.left)
if key[pg.K_RIGHT]: if key[pg.K_RIGHT]:
pacman.change_dir(m_pacman.direction.right) pacman.change_dir(m_pacman.direction.right)
# print(pacman.direction)
def refresh(self): def refresh(self):
"""refresh/redraw all""" """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): def create_maze_surface(self):
pass pass
def loop(self): def loop(self):
while 1: while 1:
self.screen.fill((0, 0, 0))
for event in pg.event.get(): for event in pg.event.get():
if event.type == pg.QUIT: if event.type == pg.QUIT:
sys.exit() sys.exit()
self.user_events() self.user_events()
self.physic_motor.move_all() self.physic_motor.move_all()
self.refresh() self.refresh()
self.clock.tick(max_fps) self.clock.tick(self.max_fps)
pg.display.flip()
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_maze_1.png") pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze1.png")
phys_motor = PhysicMotor(pacmap, pacman) phys_motor = PhysicMotor(pacmap, pacman)
screen = Screen((280, 310), pacmap, phys_motor, pacman) screen = Screen((560, 620), pacmap, phys_motor, pacman)

View File

@ -34,17 +34,19 @@ class Fruit:
class Pacman: class Pacman:
def __init__(self, position=[0, 0]): def __init__(self, position=[0, 0], resolution=10):
self.position = list(position) self.position = [position[0]+resolution, position[1]+resolution]
self.direction = direction.right self.direction = direction.right
self.next_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.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.speed = 0.1
self.resolution = resolution # when pacman in 0:10 he's in the 1st cell if resolution=10
def matrix_position(self): 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): def has_super_power(self):
return self.super_power > 0 return self.super_power > 0
@ -98,9 +100,10 @@ class Pacman:
self.direction = new_dir self.direction = new_dir
def move(self): def move(self):
self.position[0] = self.position[0] + self.direction[0] * self.speed # self.position[0] += self.direction[0] * self.speed
self.position[1] = self.position[1] + self.direction[1] * 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"): def game_over(status = "lose"):
#TODO #TODO

View File

@ -5,7 +5,8 @@ class PacmanSprite(pg.sprite.Sprite):
def __init__(self, size): def __init__(self, size):
super().__init__() super().__init__()
self.image = pg.Surface([size, size]) 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 # 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 # Update the position of this object by setting the values of rect.x and rect.y

View File

@ -46,12 +46,14 @@ class Map:
2: big pac-dot 2: big pac-dot
""" """
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 = decode_map(maze_img_file) self.phys_map = decode_map(maze_img_file)
except Exception as e: except Exception as e:
raise e raise e
self.phys_map = phys_map # in the first part we assume phys_map is correct and no need to verify else:
self.dots_map = dots_map 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 self.intersect_map = [] # TODO - the layer which contains intersections pre-calculated

View File

@ -9,8 +9,20 @@ class PhysicMotor:
def move_all(self): def move_all(self):
# pacman movement # 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 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): if self.pacmap.get_tile(pac_x, pac_y + self.pacman.direction[1]) in (PhysTile.GRD, PhysTile.TPT):
self.pacman.move() 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()