avancement - plz touchez pas trop c'est la fin de la séance

This commit is contained in:
DylanVsn 2019-11-13 18:01:59 +01:00
parent 13d579fa45
commit ea1e4e6724
5 changed files with 117 additions and 9 deletions

59
src/graphic.py Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env python3
import pygame as pg
import pacman_sprite
import pacman as m_pacman # m_ for module to avoid conflicts
from physic_motor import PhysicMotor
import pacmap as m_pacmap
import sys
pg.init()
class Screen:
def __init__(self, size, pacmap: m_pacmap.Map, physic_motor: PhysicMotor, pacman: m_pacman.Pacman):
self.screen = pg.display.set_mode(size)
# self.screen.set_caption("Pacman")
self.physic_motor = physic_motor
self.pacman = pacman
self.pacman_sprite = pacman_sprite.PacmanSprite(size[0]/28)
self.clock = pg.time.Clock()
self.max_fps = 30
self.loop()
def user_events(self):
key = pg.key.get_pressed()
if key[pg.K_UP]:
pacman.change_dir(m_pacman.direction.up)
if key[pg.K_DOWN]:
pacman.change_dir(m_pacman.direction.down)
if key[pg.K_LEFT]:
pacman.change_dir(m_pacman.direction.left)
if key[pg.K_RIGHT]:
pacman.change_dir(m_pacman.direction.right)
def refresh(self):
"""refresh/redraw all"""
pass
def create_maze_surface(self):
pass
def loop(self):
while 1:
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
self.user_events()
self.physic_motor.move_all()
self.refresh()
self.clock.tick(max_fps)
if __name__ == '__main__':
pacman = m_pacman.Pacman((1,1))
pacmap = m_pacmap.Map(maze_img_file="../pacmap_maze_1.png")
phys_motor = PhysicMotor(pacmap, pacman)
screen = Screen((280, 310), pacmap, phys_motor, pacman)

View File

@ -3,16 +3,25 @@ import pacmap
import os import os
from enum import IntEnum from enum import IntEnum
from collections import namedtuple
_tempdir = namedtuple("Direction", ["up", "down", "left", "right"])
direction = _tempdir(
up= (0, -1),
down=(0, 1),
left=(-1, 0),
right=(1, 0)
)
pacdot_counter = 88 pacdot_counter = 88
score = 0 score = 0
lives = 3 lives = 3
class Direction(IntEnum): # p-e à remplacer par la namedtuple direction plus haut
U = 0 # class Direction(IntEnum):
D = 1 # U = 0
R = 2 # D = 1
L = 3 # R = 2
# L = 3
class FruitType(IntEnum): class FruitType(IntEnum):
A = 0 A = 0
@ -25,13 +34,14 @@ class Fruit:
class Pacman: class Pacman:
def __init__(self): def __init__(self, position=[0, 0]):
self.position = (0.0, 0.0) self.position = list(position)
self.direction = Direction.R self.direction = direction.right
self.next_direction = Direction.R 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
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]))
@ -84,6 +94,14 @@ class Pacman:
if lives < 0: # à vérifier if lives < 0: # à vérifier
game_over() game_over()
def change_dir(self, new_dir):
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
def game_over(status = "lose"): def game_over(status = "lose"):
#TODO #TODO
pass pass

12
src/pacman_sprite.py Executable file
View File

@ -0,0 +1,12 @@
"""Pacman Graphic Object"""
import pygame as pg
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)
# 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
self.rect = self.image.get_rect()

View File

@ -147,6 +147,9 @@ class Map:
return True return True
def get_tile(self, x, y):
return self.phys_map[y][x]
def explore(matrix, x=-1, y=-1): def explore(matrix, x=-1, y=-1):
"""explore the given matrix and change it (GRD and TPT become FIT)""" """explore the given matrix and change it (GRD and TPT become FIT)"""
if x < 0 and y < 0: if x < 0 and y < 0:

16
src/physic_motor.py Executable file
View File

@ -0,0 +1,16 @@
from pacmap import *
from pacman import *
class PhysicMotor:
def __init__(self, c_pacmap: Map, c_pacman: Pacman):
self.pacmap = c_pacmap
self.pacman = c_pacman
self.entities = [] # ghosts
def move_all(self):
# pacman movement
pac_x, pac_y = self.pacman.position
if self.pacman.direction in (direction.up, direction.down):
if pac_y % 1 == 0.5: # 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()