From ea1e4e6724bd01074fc74eaa5fde029d11c84391 Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Wed, 13 Nov 2019 18:01:59 +0100 Subject: [PATCH] =?UTF-8?q?avancement=20-=20plz=20touchez=20pas=20trop=20c?= =?UTF-8?q?'est=20la=20fin=20de=20la=20s=C3=A9ance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/graphic.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/pacman.py | 36 ++++++++++++++++++++------- src/pacman_sprite.py | 12 +++++++++ src/pacmap.py | 3 +++ src/physic_motor.py | 16 ++++++++++++ 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100755 src/graphic.py create mode 100755 src/pacman_sprite.py create mode 100755 src/physic_motor.py diff --git a/src/graphic.py b/src/graphic.py new file mode 100755 index 0000000..4337e34 --- /dev/null +++ b/src/graphic.py @@ -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) \ No newline at end of file diff --git a/src/pacman.py b/src/pacman.py index d3dabe1..eedb643 100755 --- a/src/pacman.py +++ b/src/pacman.py @@ -3,16 +3,25 @@ import pacmap import os 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 score = 0 lives = 3 -class Direction(IntEnum): - U = 0 - D = 1 - R = 2 - L = 3 +# p-e à remplacer par la namedtuple direction plus haut +# class Direction(IntEnum): +# U = 0 +# D = 1 +# R = 2 +# L = 3 class FruitType(IntEnum): A = 0 @@ -25,13 +34,14 @@ class Fruit: class Pacman: - def __init__(self): - self.position = (0.0, 0.0) - self.direction = Direction.R - self.next_direction = Direction.R + def __init__(self, position=[0, 0]): + self.position = list(position) + self.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.ghost_combo = 0 self.size = (1.8, 1.8) # size related to tile size + self.speed = 0.1 def matrix_position(self): return (int(self.position[0]), int(self.position[1])) @@ -84,6 +94,14 @@ class Pacman: if lives < 0: # à vérifier 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"): #TODO pass diff --git a/src/pacman_sprite.py b/src/pacman_sprite.py new file mode 100755 index 0000000..a3aeb6c --- /dev/null +++ b/src/pacman_sprite.py @@ -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() \ No newline at end of file diff --git a/src/pacmap.py b/src/pacmap.py index afc93f3..209b1da 100755 --- a/src/pacmap.py +++ b/src/pacmap.py @@ -147,6 +147,9 @@ class Map: return True + def get_tile(self, x, y): + return self.phys_map[y][x] + 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: diff --git a/src/physic_motor.py b/src/physic_motor.py new file mode 100755 index 0000000..2a80a6b --- /dev/null +++ b/src/physic_motor.py @@ -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() \ No newline at end of file