Ajout d'une grille simpliste

This commit is contained in:
theo@manjaro 2021-06-25 16:13:52 +02:00
parent 32e33947fe
commit 92e3ac2a25
18 changed files with 121 additions and 59 deletions

BIN
gamedata/assets/solid.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -4,14 +4,15 @@ class BaseObject():
def __init__(self,x,y,game,w=10,h=10):
self.rect = [x,y,w,h]
self.baserect = self.rect.copy()
self.game = game
self.sprite = game.sprite_lib["icon.png"]
self.spriteoffset = 0,0
self.depth = 1 # Sa "profondeur", déterminera l'odre d'affichage des objets
def step(self,game):
def step(self):
pass
def draw(self,game):
def draw(self):
if self.sprite:
game.window.blit(self.sprite,self.rect[:2])
self.game.window.blit(self.sprite,self.rect[:2])

View File

@ -6,9 +6,10 @@ class BackgroundDrawer(BaseObject):
super().__init__(x,y,game,w,h)
self.ratio = ratio
self.depth = -1
self.fill(sprite,game) # Générer notre sprite de l'objet, le remplissant avec le sprite argument
self.fill(sprite) # Générer notre sprite de l'objet, le remplissant avec le sprite argument
def fill(self,sprite,game):
def fill(self,sprite):
game = self.game
self.tilew = sprite.get_width()
self.tileh = sprite.get_height()
nbw = round(self.rect[2]/self.tilew+1.5)
@ -21,7 +22,8 @@ class BackgroundDrawer(BaseObject):
for j in range(nbh):
self.sprite.blit(sprite,(i*self.tilew,j*self.tileh))
def draw(self,game):
def draw(self):
game = self.game
# Affichage du sprite de mannière répétée
px = (self.rect[0]-game.globals["camerax"]*self.ratio)%self.tilew
py = (self.rect[1]-game.globals["cameray"]*self.ratio)%self.tileh

View File

@ -9,13 +9,15 @@ class MenuBackground(BackgroundDrawer):
shadow = game.sprite_lib["shadow.png"]
self.shadow = game.pygame.transform.scale(shadow,(1500,1500)) # J'augmente la taille de l'ombre
def step(self,game):
super().step(game)
def step(self):
game = self.game
super().step()
destx = (game.inputs["mouse"]["pos"][0]-game.DISPLAY_WIDTH/2)
desty = (game.inputs["mouse"]["pos"][1]-game.DISPLAY_HEIGHT/2)
game.globals["camerax"] += (destx-game.globals["camerax"])/6
game.globals["cameray"] += (desty-game.globals["cameray"])/6
def draw(self,game):
super().draw(game)
def draw(self):
game = self.game
super().draw()
game.lib.drawcenter(game,self.shadow,game.DISPLAY_WIDTH/2,game.DISPLAY_HEIGHT*3/5)

View File

@ -7,6 +7,6 @@ class MovingBackground(BackgroundDrawer):
self.spdx = spdx
self.spdy = spdy
def step(self,game):
def step(self):
self.rect[0]+=self.spdx
self.rect[1]+=self.spdy

View File

@ -5,22 +5,21 @@ class Button(base.BaseObject):
super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments
self.text = "Hello"
self.spritenon = self.gensprite(game,w,h,folder)
self.spriteact = self.gensprite(game,w,h,folderact)
self.spritenon = self.gensprite(w,h,folder)
self.spriteact = self.gensprite(w,h,folderact)
self.sfx = game.sound_lib["sfx/click.wav"]
self.trigger = False
self.collision = False
self.game = game
def click(self,game):
print("hello world ! ")
self.click = click
self.skip = False # Si je passe l'effet sonore de base
def gensprite(self,game,w,h,foldername):
def gensprite(self,w,h,foldername):
game = self.game
# Initialisation des parties de sprites
sprites = {}
for i in ("middle","left","right","up","down","corner_ul","corner_ur","corner_dl","corner_dr"):
@ -54,7 +53,8 @@ class Button(base.BaseObject):
# Sprite du milieu
finalsprite.blit(base.pygame.transform.scale(sprites["middle"],(basewidth,baseheight)),(sprites["left"].get_width(),sprites["up"].get_height()))
return finalsprite
def step(self,game):
def step(self):
game = self.game
# Je vérifie si mon curseur de souris est dans le rectangle de l'objet
self.collision = True
nb = len(self.rect)//2 # Le nombre de dimensions, dans ce cas 2
@ -72,12 +72,13 @@ class Button(base.BaseObject):
self.sfx.play()
self.trigger = False
def draw(self,game):
def draw(self):
game = self.game
# J'utilise le sprite special selection quand j'ai la souris dessus
self.sprite = self.spritenon
if self.collision:
self.sprite = self.spriteact
super().draw(game)
super().draw()
# Affichage du texte centré
value = 150
if self.collision: # Eclaircissement du texte
@ -85,4 +86,4 @@ class Button(base.BaseObject):
img = game.fontfile.render(self.text, False, [value]*4)
posx = self.rect[0]+self.rect[2]/2-img.get_width()/2
posy = self.rect[1]+self.rect[3]/2-img.get_height()/2
game.window.blit(img, (posx, posy))
self.game.window.blit(img, (posx, posy))

View File

@ -0,0 +1,7 @@
from gamedata.objects.base import BaseObject
class Player(BaseObject):
def step(self):
keys = self.game.inputs["keys"]
self.rect[0]+=keys["right"]["pressed"]-keys["left"]["pressed"]

View File

@ -0,0 +1,30 @@
from gamedata.objects.base import BaseObject
class TilesetRenderer(BaseObject):
def __init__(self,x,y,game):
super().__init__(x,y,game)
self.tilew = 64
self.tileh = 64
self.sprite = game.sprite_lib["solid.png"]
self.collisiongrid = [
[0,0,0,0,0,0,0],
[0,0,0,0,1,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,1,1],
[1,1,0,1,1,1,0],
]
def draw(self):
for y in range(len(self.collisiongrid)):
for x in range(len(self.collisiongrid[y])):
data = self.collisiongrid[y][x]
if data==1:
self.game.window.blit(self.sprite,(self.rect[0]+x*self.tilew,self.rect[1]+y*self.tileh))

View File

@ -40,7 +40,7 @@ class GameLoop():
objs = list(self.objects.values())
for i in objs:
i.step(game)
i.step()
def draw(self,game):
#Je secoue ma caméra
@ -55,7 +55,7 @@ class GameLoop():
tri = lambda x: x.depth # Donne la profondeur de l'objet
values.sort(key=tri)
for i in values:
i.draw(game) # Lancer le script d'affichage de chaques objets
i.draw() # Lancer le script d'affichage de chaques objets
# Je stabilise la caméra
game.globals["camerax"]-=rx
game.globals["cameray"]-=ry

View File

@ -15,14 +15,14 @@ class Info(BaseObject):
self.blackSurface.fill([0]*3)
self.blackSurface.set_alpha(100)
def step(self,game):
def step(self):
game = self.game
self.timer.tick(game.dt)
if self.timer.getloops()>1:
game.gameloop.delid(self.id)
def draw(self,game):
def draw(self):
game = self.game
self.txtSurface.set_alpha(255)
self.blackSurface.set_alpha(255)

View File

@ -4,6 +4,9 @@ class MainMenu(menu.Menu):
def __init__(self,x,y,game,w=100,h=100):
super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments
# Les dicos des boutons
def fnPlay(self,game):
game.scene = game.scenes.fight
btnPlay = {"name":"Jouer","function":fnPlay}
def fnOptions(self,game):
game.scene = game.scenes.options
btnOptions= {"name":"Options","function": fnOptions}
@ -17,12 +20,13 @@ class MainMenu(menu.Menu):
# Initialisation de la grille du menu
self.grid = [
[btnPlay],
[btnOptions],
[btnQuitter]
]
self.create(self.grid,self.rect,game)
self.create(self.grid,self.rect)
def draw(self,game):
def draw(self):
# affiche le titre du jeu
game.window.blit(game.sprite_lib["gui/title.png"],(0,self.baserect[1]-game.DISPLAY_HEIGHT/8))
self.game.window.blit(self.game.sprite_lib["gui/title.png"],(0,self.baserect[1]-self.game.DISPLAY_HEIGHT/8))

View File

@ -26,10 +26,9 @@ class Menu(base.BaseObject):
[btnExemple],
[btnExemple,None]
]
#self.create(self.grid,self.rect,game)
def create(self,grid,rect,game):
def create(self,grid,rect):
game = self.game
elementheight = (rect[3]-(len(grid)-1)*self.vpadding)//len(grid)
for i in range(len(grid)):
elementwidth = (rect[2]-(len(grid[i])-1)*self.hpadding)//len(grid[i])

View File

@ -13,4 +13,4 @@ class OptionMenu(menu.Menu):
[btnRetour]
]
self.create(self.grid,self.rect,game)
self.create(self.grid,self.rect)

View File

@ -18,7 +18,9 @@ class Particle(BaseObject):
self.alpha = 1
def step(self,game):
def step(self):
game = self.game
for j in range(2):
self.rect[j]+=self.vel[j]*game.dt*60
@ -31,7 +33,9 @@ class Particle(BaseObject):
self.alpha = max(1-(((self.timer.getloops()+1-self.timer.getratio())/len(self.sprites)))**3,0)
def draw(self,game):
def draw(self):
game = self.game
spriteindex = self.timer.getloops()
posx,posy = self.rect[:2]

View File

@ -4,13 +4,13 @@ class BGMSlider(Slider):
def __init__(self,x,y,game,w=100,h=64,folder="base"):
super().__init__(x,y,game,w,h,folder) # initialise l'objet de base avec les bons arguments
self.currentvalue = self.getvalue(game)
self.currentvalue = self.getvalue()
self.posttext = " % Musique"
def change(self,game):
game.globals["bgmvolume"] = self.currentvalue/100
game.reinit_volumes()
def change(self):
self.game.globals["bgmvolume"] = self.currentvalue/100
self.game.reinit_volumes()
def getvalue(self,game):
return game.globals["bgmvolume"]*100
def getvalue(self):
return self.game.globals["bgmvolume"]*100

View File

@ -4,13 +4,13 @@ class SFXSlider(Slider):
def __init__(self,x,y,game,w=100,h=64,folder="base"):
super().__init__(x,y,game,w,h,folder) # initialise l'objet de base avec les bons arguments
self.currentvalue = self.getvalue(game)
self.currentvalue = self.getvalue()
self.posttext = " % Effets Sonores"
def change(self,game):
game.globals["sfxvolume"] = self.currentvalue/100
game.reinit_volumes()
def change(self):
self.game.globals["sfxvolume"] = self.currentvalue/100
self.game.reinit_volumes()
def getvalue(self,game):
return game.globals["sfxvolume"]*100
def getvalue(self):
return self.game.globals["sfxvolume"]*100

View File

@ -8,30 +8,30 @@ class Slider(base.BaseObject):
self.max = 100
self.currentvalue = self.getvalue(game)
self.currentvalue = self.getvalue()
self.bgsprite = self.gensprite(game,w,h,folder)
self.bgsprite = self.gensprite(w,h,folder)
self.sprite = game.pygame.transform.scale(game.sprite_lib["gui/slider/"+folder+"/circle.png"],(h,h))
self.maxwidth = w-h
self.rect = [self.getvalue(game)/self.max*(self.maxwidth)+self.baserect[0],self.baserect[1],h,h]
self.rect = [self.getvalue()/self.max*(self.maxwidth)+self.baserect[0],self.baserect[1],h,h]
self.sfx = game.sound_lib["sfx/click.wav"]
self.dragged = False
def change(self,game):
def change(self):
print("Value : "+self.currentvalue)
def getvalue(self,game):
return game.globals["sfxvolume"]*100
def getvalue(self):
return self.game.globals["sfxvolume"]*100
def postovalue(self):
self.currentvalue = (self.rect[0]-self.baserect[0])/(self.maxwidth)*self.max
def gensprite(self,game,w,h,folder):
def gensprite(self,w,h,folder):
game = self.game
left = game.sprite_lib["gui/slider/"+folder+"/left.png"]
left = game.pygame.transform.scale(left,(round(h/left.get_height()*left.get_width()),h))
right = game.sprite_lib["gui/slider/"+folder+"/right.png"]
@ -48,7 +48,8 @@ class Slider(base.BaseObject):
return surface
def step(self,game):
def step(self):
game = self.game
if self.rect[0]<=game.inputs["mouse"]["pos"][0]+game.globals["camerax"]<=self.rect[0]+self.rect[2]:
if self.rect[1]<=game.inputs["mouse"]["pos"][1]+game.globals["cameray"]<=self.rect[1]+self.rect[3]:
if game.inputs["mouse"]["click"]>1:
@ -61,10 +62,12 @@ class Slider(base.BaseObject):
if self.dragged and game.inputs["mouse"]["click"]==0:
# Le slider a été laché
self.change(game)
self.change()
self.dragged = False
def draw(self,game):
def draw(self):
game = self.game
game.window.blit(self.bgsprite,(self.baserect[0]-game.globals["camerax"],self.baserect[1]-game.globals["cameray"]))
game.window.blit(self.sprite,(self.rect[0]-game.globals["camerax"],self.rect[1]-game.globals["cameray"]))

View File

@ -4,6 +4,8 @@ from gamedata.objects.bg.menubackground import MenuBackground
from gamedata.objects.menu.optionmenu import OptionMenu
from gamedata.objects.sliders.bgmslider import BGMSlider
from gamedata.objects.sliders.sfxslider import SFXSlider
from gamedata.objects.combat.player import Player
from gamedata.objects.combat.tileset import TilesetRenderer
def main(game):
game.gameloop.reinit() # Je réinitialis la boucle
@ -12,6 +14,13 @@ def main(game):
game.gameloop.summon(bg)
game.gameloop.summon(menu)
def fight(game):
game.gameloop.reinit()
p1 = Player(50,50,game)
tileset = TilesetRenderer(0,0,game)
game.gameloop.summon(p1)
game.gameloop.summon(tileset)
def options(game):
game.gameloop.reinit()
game.globals["camerax"] = 0