blender-edutech-tutoriels/labyrinthe/1-scene3D/2-labyrinthe.py

169 lines
5.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import bge # Bibliothèque Blender Game Engine (BGE)
###############################################################################
# labyrinthe.py
# @title: Commandes pour le tutotiel Labyrinthe
# @project: Blender-EduTech
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2021 Philippe Roy
# @license: GNU GPL
#
# Commandes déclenchées par UPBGE pour le tutoriel Labyrinthe
#
###############################################################################
# Récupérer la scène 3D
scene = bge.logic.getCurrentScene()
# print("Objets de la scene : ", scene.objects)
# Constantes
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
# Gestion du clavier
###############################################################################
# Flèches pour tourner le plateau
def clavier(cont):
# obj = cont.owner
obj = scene.objects['Plateau']
keyboard = bge.logic.keyboard
resolution = 0.01
# Up
if (ACTIVATE == keyboard.events[bge.events.UPARROWKEY]):
obj.applyRotation((-resolution,0,0), False)
# Down
if (ACTIVATE == keyboard.events[bge.events.DOWNARROWKEY]):
obj.applyRotation((resolution,0,0), False)
# Left
if (ACTIVATE == keyboard.events[bge.events.LEFTARROWKEY]):
obj.applyRotation((0, -resolution,0), False)
# Right
if (ACTIVATE == keyboard.events[bge.events.RIGHTARROWKEY]):
obj.applyRotation((0, resolution,0), False)
###############################################################################
# Joystick
###############################################################################
def joystick(cont):
obj = cont.owner
joystickIndex = 0 #int from 0 to 6
joy = bge.logic.joysticks[joystickIndex]
events = joy.activeButtons
axis = joy.axisValues[0:4]
resolution = 0.01
leftStick_x = axis[0]; leftStick_y = axis[1]
rightStick_x = axis[2]; rightStick_y = axis[3]
#if any button is pressed
# if events:
# print(events) #spit out integer index of pressed buttons
# if 0 in events:
# doSomething()
# Up
if leftStick_y <-0.1 :
obj.applyRotation((-resolution,0,0), False)
# Down
if leftStick_y >0.1 :
obj.applyRotation((resolution,0,0), False)
# Left
if leftStick_x <-0.1 :
obj.applyRotation((0, -resolution,0), False)
# Right
if leftStick_x >0.1 :
obj.applyRotation((0, resolution,0), False)
###############################################################################
# Restart
###############################################################################
# Initialisation
def init(cont):
# Mémorisation de la position de départ du joueur
obj = scene.objects['Joueur']
# obj = cont.owner
obj['init_lx']=obj.worldPosition.x
obj['init_ly']=obj.worldPosition.y
obj['init_lz']=obj.worldPosition.z
# Cacher le texte de victoire
scene.objects['Texte'].setVisible(False,False)
# Appliquer le poids à la balle
# obj.gravity = [0, 0, -10000000]
# obj.mass = 10
# obj. applyForce([0, 0, -100000000])
# obj.setDamping(0.5, 0.5)
# Atteindre une orientation (bas niveau)
def applyRotationTo(obj, rx=None, ry=None, rz=None):
rres=0.001 # resolution rotation
# x
if rx is not None:
while (abs(rx-obj.worldOrientation.to_euler().x) > rres) :
if obj.worldOrientation.to_euler().x-rx > rres:
obj.applyRotation((-rres, 0, 0), True)
if rx-obj.worldOrientation.to_euler().x > rres:
obj.applyRotation((rres, 0, 0), True)
# print ("delta x ",rx-obj.worldOrientation.to_euler().x)
# y
if ry is not None:
while (abs(ry-obj.worldOrientation.to_euler().y) > rres) :
if obj.worldOrientation.to_euler().y-ry > rres:
obj.applyRotation((0, -rres, 0), True)
if ry-obj.worldOrientation.to_euler().y > rres:
obj.applyRotation((0, rres, 0), True)
# print ("delta y ",ry-obj.worldOrientation.to_euler().y)
# z
if rz is not None:
while (abs(rz-obj.worldOrientation.to_euler().z) > rres) :
if obj.worldOrientation.to_euler().z-rz > rres:
obj.applyRotation((0, 0, -rres), True)
if rz-obj.worldOrientation.to_euler().z > rres:
obj.applyRotation((0, 0, rres), True)
# print ("delta z ",rz-obj.worldOrientation.to_euler().z)
# Redémarrage de la partie
def restart(cont):
obj = cont.owner
obj['pos_z'] = obj.worldPosition.z # Affichage de la position en z (debug)
if obj.worldPosition.z <-20 or obj.worldPosition.z >20 :
# Replacement du plateau
applyRotationTo(scene.objects['Plateau'], 0, 0, 0)
# Vitesse initiale nulle du joueur
obj.worldLinearVelocity=(0, 0, 0)
obj.worldAngularVelocity=(0, 0, 0)
# Replacement du joueur au point de départ
obj.worldPosition.x=obj['init_lx']
obj.worldPosition.y=obj['init_ly']
obj.worldPosition.z=obj['init_lz']
###############################################################################
# Gagné
###############################################################################
def gagne(cont):
scene.objects['Texte'].setVisible(True,False)