This repository has been archived on 2019-12-09. You can view files and clone it, but cannot push or open issues or pull requests.
pacman/livrables/1/pacman.py

135 lines
4.4 KiB
Python
Executable File

import pacmap
import os
from enum import IntEnum
from collections import namedtuple
_tempdir = namedtuple("Direction", ["up", "down", "left", "right", "none"])
direction = _tempdir(
up= (0, -1),
down=(0, 1),
left=(-1, 0),
right=(1, 0),
none=(0, 0)
)
pacdot_counter = 88
score = 0
lives = 3
class FruitType(IntEnum):
A = 0
class Fruit:
def __init__(self, fruit_type, score, position = (0, 0)):
self.fruit_type = fruit_type
self.score = score
self.position = position
class Pacman:
def __init__(self, position=[0, 0], map_size=(28, 31), resolution=10):
self.position = [position[0]*resolution+int(resolution/2), position[1]*resolution+int(resolution/2)]
self.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.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
def matrix_position(self):
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]
return next_x, next_y
def has_super_power(self):
return self.super_power > 0
def eat_pacdot(self, dot_map):
global score
global pacdot_counter
dot_map.dots_map[self.matrix_position()[0]][self.matrix_position()[1]] = pacmap.DotTile.NDT
pacdot_counter -= 1
score += 10
if pacdot_counter == 0:
game_over("win")
def eat_super_pacdot(self, dot_map):
global score
dot_map.dots_map[self.matrix_position()[0]][self.matrix_position()[1]] = pacmap.DotTile.NDT
score += 50
self.super_power += 1
# TODO
# Requires UNIX - or use of async
# pid = os.fork()
#if pid == 0:
# return
#os.sleep(10)
self.super_power -= 1
self.ghost_combo = 0
def eat_fruit(self, fruit, dot_map):
global score
dot_map.dots_map[self.matrix_position()[0]][self.matrix_position()[1]] = pacmap.DotTile.NDT
score += fruit.score
def eat_ghost(self, ghost):
global score
ghost.despawn_and_respawn()
self.ghost_combo += 1
score += (2 ** self.ghost_combo) * 100
def get_eaten(self, dot_map):
global lives
#TODO score loss ?
self.position = dot_map.spawn_point #TODO
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]
return next_x, next_y
def move(self):
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])
def game_over(status = "lose"):
#TODO
pass