2023-04-21 01:04:31 +02:00
|
|
|
import bge # Bibliothèque Blender Game Engine (BGE)
|
|
|
|
|
|
|
|
###############################################################################
|
2023-04-22 17:00:43 +02:00
|
|
|
# 2-labyrinthe.py
|
2023-04-28 16:50:21 +02:00
|
|
|
# @title: Module (unique) de la scène 3D du labyrinthe à bille
|
2023-04-30 04:21:49 +02:00
|
|
|
# @project: Blender-EduTech - Tutoriel 2 : Labyrinthe à bille - Passage au Python
|
2023-04-21 01:04:31 +02:00
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
2023-04-21 04:15:08 +02:00
|
|
|
# @copyright: Copyright (C) 2023 Philippe Roy
|
2023-04-21 01:04:31 +02:00
|
|
|
# @license: GNU GPL
|
|
|
|
#
|
2023-04-22 17:00:43 +02:00
|
|
|
# Commandes déclenchées par UPBGE pour le scène du labyrinthe
|
2023-04-21 01:04:31 +02:00
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Récupérer la scène 3D
|
|
|
|
scene = bge.logic.getCurrentScene()
|
2023-04-22 08:41:40 +02:00
|
|
|
# print("Objets de la scene : ", scene.objects) # Lister les objets de la scène
|
2023-04-21 01:04:31 +02:00
|
|
|
|
|
|
|
# Constantes
|
|
|
|
|
|
|
|
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
|
|
|
|
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
|
|
|
|
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Gestion du clavier
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Flèches pour tourner le plateau
|
|
|
|
def clavier(cont):
|
2023-04-22 08:41:40 +02:00
|
|
|
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Plateau'
|
|
|
|
# obj = scene.objects['Plateau']
|
2023-04-21 01:04:31 +02:00
|
|
|
keyboard = bge.logic.keyboard
|
|
|
|
resolution = 0.01
|
|
|
|
|
2023-04-21 04:15:08 +02:00
|
|
|
# Flèche haut - Up arrow
|
2023-04-22 08:41:40 +02:00
|
|
|
if keyboard.inputs[bge.events.UPARROWKEY].status[0] == ACTIVATE:
|
2023-04-30 04:21:49 +02:00
|
|
|
obj.applyRotation((-resolution,0,-obj.worldOrientation.to_euler().z), False)
|
2023-04-21 01:04:31 +02:00
|
|
|
|
2023-04-21 04:15:08 +02:00
|
|
|
# Flèche bas - Down arrow
|
2023-04-22 08:41:40 +02:00
|
|
|
if keyboard.inputs[bge.events.DOWNARROWKEY].status[0] == ACTIVATE:
|
2023-04-30 04:21:49 +02:00
|
|
|
obj.applyRotation((resolution,0,-obj.worldOrientation.to_euler().z), False)
|
2023-04-21 01:04:31 +02:00
|
|
|
|
2023-04-21 04:15:08 +02:00
|
|
|
# Flèche gauche - Left arrow
|
2023-04-22 08:41:40 +02:00
|
|
|
if keyboard.inputs[bge.events.LEFTARROWKEY].status[0] == ACTIVATE:
|
2023-04-30 04:21:49 +02:00
|
|
|
obj.applyRotation((0, -resolution,-obj.worldOrientation.to_euler().z), False)
|
2023-04-21 01:04:31 +02:00
|
|
|
|
2023-04-21 04:15:08 +02:00
|
|
|
# Flèche droit - Right arrow
|
2023-04-22 08:41:40 +02:00
|
|
|
if keyboard.inputs[bge.events.RIGHTARROWKEY].status[0] == ACTIVATE:
|
2023-04-30 04:21:49 +02:00
|
|
|
obj.applyRotation((0, resolution, -obj.worldOrientation.to_euler().z), False)
|
2023-04-21 01:04:31 +02:00
|
|
|
|
2023-04-22 08:41:40 +02:00
|
|
|
###############################################################################
|
|
|
|
# Gameplay
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# Initialisation de la scène
|
|
|
|
def init(cont):
|
|
|
|
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Bille'
|
|
|
|
|
|
|
|
# Mémorisation de la position de départ de la bille
|
|
|
|
obj['init_x']=obj.worldPosition.x
|
|
|
|
obj['init_y']=obj.worldPosition.y
|
|
|
|
obj['init_z']=obj.worldPosition.z
|
|
|
|
|
|
|
|
# Cacher le panneau de la victoire et suspendre la physique du panneau cliquable
|
|
|
|
scene.objects['Panneau victoire'].setVisible(False,True)
|
|
|
|
scene.objects['Panneau victoire - plan'].suspendPhysics (True)
|
2023-04-22 17:00:43 +02:00
|
|
|
scene.objects['Bouton fermer'].color = (0, 0, 0, 1) # Noir
|
2023-04-22 08:41:40 +02:00
|
|
|
|
|
|
|
# Cycle (boucle de contrôle de la bille)
|
|
|
|
def cycle(cont):
|
|
|
|
obj = cont.owner # obj est l'objet associé au contrôleur donc 'Bille'
|
|
|
|
obj['z']=obj.worldPosition.z # la propriété z est mis à jour avec la position globale en z de la bille
|
|
|
|
|
2023-05-13 17:20:24 +02:00
|
|
|
# Si l'altitude de bille < -10 et pas de victoire -> chute
|
|
|
|
if obj['z'] < -10 and obj['victoire'] == False:
|
2023-04-22 08:41:40 +02:00
|
|
|
print ("Chuuuu.....te")
|
2023-05-13 17:20:24 +02:00
|
|
|
depart() # Replacer la bille au départ
|
|
|
|
|
|
|
|
# Départ de la bille
|
|
|
|
def depart():
|
|
|
|
obj_bille = scene.objects['Bille']
|
|
|
|
obj_plateau = scene.objects['Plateau']
|
|
|
|
|
|
|
|
# Replacement du plateau (tous les angles à 0 en plusieurs fois)
|
|
|
|
while obj_plateau.worldOrientation.to_euler().x != 0 and obj_plateau.worldOrientation.to_euler().y !=0 and obj_plateau.worldOrientation.to_euler().z !=0 :
|
|
|
|
obj_plateau.applyRotation((-obj_plateau.worldOrientation.to_euler().x, -obj_plateau.worldOrientation.to_euler().y, -obj_plateau.worldOrientation.to_euler().z), False)
|
|
|
|
|
|
|
|
# Mettre la bille à la position de départ avec une vitesse nulle
|
|
|
|
obj_bille = scene.objects['Bille']
|
|
|
|
obj_bille.worldLinearVelocity=(0, 0, 0)
|
|
|
|
obj_bille.worldAngularVelocity=(0, 0, 0)
|
|
|
|
obj_bille.worldPosition.x = obj_bille['init_x']
|
|
|
|
obj_bille.worldPosition.y = obj_bille['init_y']
|
|
|
|
obj_bille.worldPosition.z = obj_bille['init_z']+0.5 # On repose la bille
|
|
|
|
obj_bille['victoire']=False
|
|
|
|
obj_bille['chute'] = False
|
|
|
|
|
|
|
|
# Victoire (collision de la bille avec l'arrivée)
|
2023-04-22 08:41:40 +02:00
|
|
|
def victoire(cont):
|
2023-05-13 17:20:24 +02:00
|
|
|
scene.objects['Bille']['victoire']=True
|
2023-04-22 08:41:40 +02:00
|
|
|
scene.objects['Panneau victoire'].setVisible(True,True) # Afficher le panneau de la victoire
|
|
|
|
scene.objects['Panneau victoire - plan'].restorePhysics() # Restaurer la physique du panneau cliquable
|
|
|
|
start = 1
|
|
|
|
end = 100
|
|
|
|
layer = 0
|
|
|
|
priority = 1
|
|
|
|
blendin = 1.0
|
|
|
|
mode = bge.logic.KX_ACTION_MODE_PLAY
|
|
|
|
layerWeight = 0.0
|
|
|
|
ipoFlags = 0
|
|
|
|
speed = 1
|
|
|
|
scene.objects['Panneau victoire'].playAction('Panneau victoireAction', start, end, layer, priority, blendin, mode, layerWeight, ipoFlags, speed)
|
|
|
|
|
2023-04-22 17:00:43 +02:00
|
|
|
# Highlight du bouton Fermer
|
|
|
|
def victoire_fermer_hl(cont):
|
|
|
|
obj = cont.owner
|
|
|
|
|
|
|
|
# Activation
|
|
|
|
if cont.sensors['MO'].status == JUST_ACTIVATED:
|
|
|
|
obj.color = (1, 1, 1, 1) # Blanc
|
|
|
|
|
|
|
|
# Désactivation
|
|
|
|
if cont.sensors['MO'].status == JUST_RELEASED:
|
|
|
|
obj.color = (0, 0, 0, 1) # Noir
|
|
|
|
|
2023-04-22 08:41:40 +02:00
|
|
|
# Fermer le panneau de la victoire (clic)
|
|
|
|
def victoire_fermer(cont):
|
2023-04-22 17:00:43 +02:00
|
|
|
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive:
|
2023-04-22 08:41:40 +02:00
|
|
|
scene.objects['Panneau victoire'].setVisible(False,True) # Cacher le panneau de la victoire
|
|
|
|
scene.objects['Panneau victoire - plan'].suspendPhysics (True) # Suspendre la physique du panneau cliquable
|
2023-05-13 17:20:24 +02:00
|
|
|
depart() # Replacer la bille au départ
|
2023-04-22 08:41:40 +02:00
|
|
|
|
2023-04-21 01:04:31 +02:00
|
|
|
###############################################################################
|
2023-05-10 08:10:01 +02:00
|
|
|
# Gestion du Joystick (Joystick USB)
|
|
|
|
# Partie non abordée dans le tutoriel
|
2023-04-21 01:04:31 +02:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
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)
|