Lemming control

This commit is contained in:
theo@manjaro 2021-09-10 21:33:24 +02:00
parent 74becc583c
commit 56db421672
5 changed files with 83 additions and 41 deletions

View File

@ -3,3 +3,7 @@
## Tileset
[Buch - OpenGameArt](https://opengameart.org/content/the-field-of-the-floating-islands)
## Little characters
[GraphxKid - OpenGameArt](https://opengameart.org/content/arcade-platformer-assets)

View File

@ -61,7 +61,7 @@ class Game():
self.pasttime = time.time()
# Je charge la scene de base
scenes.main(self)
scenes.ingame(self)
def set_camera(self,posx,posy):
self.globals["camerax"], self.globals["cameray"] = posx,posy
@ -114,44 +114,7 @@ class Game():
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_ESCAPE # Code pygame de la touche en question
},
"backspace":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_BACKSPACE # Code pygame de la touche en question
},
"enter":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_RETURN # Code pygame de la touche en question
},
"left":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_q # Code pygame de la touche en question
},
"right":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_d # Code pygame de la touche en question
},
"up":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_z # Code pygame de la touche en question
},
"down":{
"timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False,
"keycode" : pygame.K_s # Code pygame de la touche en question
}
}
self.no_unicode = [pygame.K_ESCAPE,pygame.K_BACKSPACE,pygame.K_RETURN]

View File

@ -0,0 +1,70 @@
from gamedata.objects.base import BaseObject
import random,math
class Lemming(BaseObject):
def __init__(self,x,y,game):
super().__init__(x,y,game)
self.direction = random.randint(0,360)
self.radius = 30 # Radius in which the mouse can click
self.basespeed = 80 # Speed that he normally walks by
self.normalspeed = self.basespeed # Speed "objective"
self.speed = self.normalspeed # Current speed, leaning towards objective speed
self.selected = False # If beeing redirected
self.cachedrel = [] # Storing relative movement of mouse
self.cachedrelsize = 10
self.mincachedsize = 5
# Used for movement
self.restx = 0
self.resty = 0
def step(self):
# Lean towards the normal speed
if self.selected:
self.normalspeed = 0
else:
self.normalspeed = self.basespeed
self.speed += (self.normalspeed-self.speed)*self.game.dt*3
# Getting x and y velocity
diffx = math.cos(math.radians(self.direction))*self.speed
diffy = math.sin(math.radians(self.direction))*self.speed
self.move(diffx*self.game.dt,diffy*self.game.dt)
# Mouse selection
mouse=self.game.inputs["mouse"]
distance = math.sqrt((mouse["pos"][0]-self.rect.center[0])**2+(mouse["pos"][1]-self.rect.center[1])**2)
if self.selected:
# Caching mouse relative movement
self.cachedrel.append(mouse["rel"])
if len(self.cachedrel)>self.cachedrelsize:
self.cachedrel.pop(0)
# Releasing it
if mouse["click"]==0 or distance>4*self.radius:
self.launch()
if self.game.inputs["mouse"]["click"]==1:
if distance<=self.radius:
self.selected = True
def launch(self):
# Launch itself in the mouse direction
if len(self.cachedrel)>self.mincachedsize:
averagerel = [sum([x[i] for x in self.cachedrel])/len(self.cachedrel) for i in range(2)]
self.direction = math.degrees(math.atan2(averagerel[1],averagerel[0]))
self.speed = self.basespeed*2
self.cachedrel = []
self.selected = False
def move(self,x,y):
# Remember the digits, pygame rects only move with integers
velx = x+self.restx
vely = y+self.resty
self.restx = velx-int(velx)
self.resty = vely-int(vely)
self.rect[0]+=int(velx)
self.rect[1]+=int(vely)

View File

@ -3,9 +3,9 @@ import gamedata.objects.menu.menu as menu
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
# Buttons dictionnaries
def fnPlay(self,game):
pass
game.scene = game.scenes.ingame
btnPlay = {"name":"Play","function":fnPlay}
def fnOptions(self,game):
game.scene = game.scenes.options

View File

@ -4,15 +4,20 @@ 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.ingame.lemmings import Lemming
def main(game):
game.scaleCamera()
game.gameloop.reinit() # Je réinitialis la boucle
game.gameloop.reinit()
bg = MenuBackground(game)
menu = MainMenu(game.DISPLAY_WIDTH/4,round(game.DISPLAY_HEIGHT*3/8),game,game.DISPLAY_WIDTH//2,game.DISPLAY_HEIGHT//2)
game.gameloop.summon(bg)
game.gameloop.summon(menu)
def ingame(game):
game.gameloop.reinit()
game.gameloop.summon(Lemming(200,200,game))
def options(game):
game.gameloop.reinit()
game.globals["camerax"] = 0