Skip to content
Snippets Groups Projects
Commit 94c44f17 authored by Boris OUYA's avatar Boris OUYA
Browse files

Merge branch 'master' into 'main'

Editeur de niveaux et classes de bases

See merge request !1
parents 21ec8202 f2397286
Branches Boris
No related tags found
1 merge request!1Editeur de niveaux et classes de bases
Game.py 0 → 100644
import pygame
import random
from constants import *
import math
from Tile import Tile
class Game:
def __init__(self,title):
pygame.display.set_caption(title)
self.width = DISPLAY_WIDTH
self.heigth = DISPLAY_HEIGHT
self.screen = pygame.display.set_mode((self.width,self.heigth))
self.tiles = []
for i in range(10):
self.tiles.append(Tile(self.screen,i*(TILE_WIDTH+GAP),100,3));
self.clock = pygame.time.Clock()
self.state = GAME_PHASE
def getState(self):
return self.state
def processInputs(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.state = GAME_STOP
def update_game(self):
self.screen.fill((0,0,0))
for tile in self.tiles:
tile.draw();
self.clock.tick(FPS)
pygame.display.flip()
import pygame as pg
import numpy as np
from constants import *
from Tile import Tile
from math import floor
class LevelEditor:
def __init__(self,title):
pg.display.set_caption(title)
self.width = DISPLAY_WIDTH
self.heigth = DISPLAY_HEIGHT
self.screen = pg.display.set_mode((self.width,self.heigth)) #écran principal
self.tiles = [] #L'ensemble des tuiles rendues à l'écran
self.reprTiles = [] #le tableau représentatif des tuiles. Servira lors de la sauvegarde
#Initialisation des tableaux
for _ in range(MAX_TILES_HOR):
line = []
line2 = []
for _ in range(MAX_TILES_VERT):
line.append(None)
line2.append(0)
self.tiles.append(line)
self.reprTiles.append(line2)
#l'holorge du jeu
self.clock = pg.time.Clock()
#Phase de jeu au début
self.state = GAME_PHASE
#Tableau des choix de tuiles disponibles
self.choices = []
for i in range(1,NUMBER_OF_TYPES_OF_TILES + 1):
self.choices.append(Tile(self.screen,(i-1)*(TILE_WIDTH+GAP),self.heigth - TILE_HEIGHT,i))
#Au début, aucun type de tuiles sélectionné
self.selected = 0
def getState(self):
"""Retourne l'état du jeu actuel"""
return self.state
def processInputs(self):
"""Gestion des évènements"""
for event in pg.event.get():
if event.type == pg.QUIT:
"""Quitter l'éditeur"""
self.state = GAME_STOP
if event.type == pg.MOUSEBUTTONDOWN:
"""Relâchement d'un bouton de la souris"""
pos = pg.mouse.get_pos() #Position du clic
x = pos[0] // (TILE_WIDTH + GAP)
y = pos[1] // (TILE_HEIGHT + GAP)
mouseStates = pg.mouse.get_pressed() #Etats de la souris (gauche,milieu,droit)
if mouseStates[0] and not mouseStates[2]:
"""Gestion du clic gauche"""
if y == TOTAL_ROW - 1:
"""Clic sur la ligne du sélecteur"""
if x < 4 :
"""Selection du type de tuile"""
self.selected = self.choices[x].getType()
elif self.selected != 0 and y < MAX_TILES_VERT and self.reprTiles[x][y] == 0:
"""Placement d'une tuile"""
self.tiles[x][y] = Tile(self.screen,x * (TILE_WIDTH + GAP),y*(TILE_HEIGHT+GAP),self.selected);
self.reprTiles[x][y] = self.selected
elif mouseStates[2] and not mouseStates[0]:
"""Clic droit pour enlever des tuiles"""
if y < MAX_TILES_VERT and self.reprTiles[x][y] != 0:
self.tiles[x][y] = None
self.reprTiles[x][y] = 0
def update_game(self):
self.screen.fill((0,0,0))
for line in self.tiles:
for tile in line:
if tile != None:
tile.draw()
#Affichage des choix possibles
for choice in self.choices:
choice.draw()
if self.selected != 0:
"""Mise en valeur du type de tuile sélectionné"""
selectRect = pg.Rect((self.selected-1)*(TILE_WIDTH+GAP),self.heigth - TILE_HEIGHT - 5,TILE_WIDTH,TILE_HEIGHT + 10)
pg.draw.rect(self.screen,COLORS[self.selected-1],selectRect)
#Ligne de séparation
pg.draw.line(self.screen,(255,255,255),(0,(MAX_TILES_VERT ) * (TILE_HEIGHT + GAP)),(self.width,(MAX_TILES_VERT) * (TILE_HEIGHT + GAP)))
self.clock.tick(FPS)
pg.display.flip()
\ No newline at end of file
Tile.py 0 → 100644
import pygame as pg
from constants import *
class Tile(pg.sprite.Sprite):
def __init__(self,screen,x,y,type):
pg.sprite.Sprite.__init__(self);
self.screen = screen
#self.image =
self.width = TILE_WIDTH
self.height = TILE_HEIGHT
#self.rect = self.image.get_rect()
self.x = x
self.y = y
self.type = type
self.color = COLORS[self.type - 1]
self.life = LIFE[self.type - 1]
def draw(self):
pg.draw.rect(self.screen,self.color,pg.Rect(self.x,self.y,self.width,self.height))
def getType(self):
return self.type
\ No newline at end of file
File added
File added
File added
File added
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
NUMBER_OF_TYPES_OF_TILES = 4
COLORS = [RED,BLUE,GREEN,YELLOW]
LIFE = [3,5,6,7]
TILE_WIDTH = 50
TILE_HEIGHT = 25
EXTRA_ROW = 7
GAP = 5
MAX_TILES_HOR = 11
MAX_TILES_VERT = 7
TOTAL_ROW = MAX_TILES_VERT + EXTRA_ROW
DISPLAY_WIDTH = (TILE_WIDTH + GAP)* MAX_TILES_HOR
DISPLAY_HEIGHT = (TILE_HEIGHT + GAP) * TOTAL_ROW
GAME_PHASE = 1
GAME_STOP = 3
EDITING_PHASE = 4
FPS = 60
\ No newline at end of file
test.py 0 → 100644
import pygame as pg
import random
import math
from Game import Game
from constants import *
from LevelEditor import LevelEditor
pg.init()
lEdi = LevelEditor("BreakOut!")
while lEdi.getState() != GAME_STOP:
lEdi.processInputs()
lEdi.update_game()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment