mirror of
https://forge.apps.education.fr/phroy/codetower.git
synced 2024-01-27 11:35:17 +01:00
ajout des scripts/threads vagues et commandes, la manipulation 3D et le mode construction
This commit is contained in:
parent
562f7e59cf
commit
e43f752a06
BIN
asset/icons/icon_construct.FCStd
Normal file
BIN
asset/icons/icon_construct.FCStd
Normal file
Binary file not shown.
Binary file not shown.
BIN
codetower-07.blend
Normal file
BIN
codetower-07.blend
Normal file
Binary file not shown.
301
ct.py
301
ct.py
@ -19,10 +19,11 @@ import threading # Multithreading
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
# Import dynamique du fichier Python élève
|
||||
# sys.setrecursionlimit(10**5) # Limite sur la récursivité (valeur par défaut : 1000) -> segfault de Blender
|
||||
# importlib.invalidate_caches()
|
||||
# ct_cmd = importlib.import_module('ct_cmd')
|
||||
# Import dynamique des fichiers Python élève et vagues
|
||||
sys.setrecursionlimit(10**5) # Limite sur la récursivité (valeur par défaut : 1000) -> segfault de Blender
|
||||
importlib.invalidate_caches()
|
||||
ct_vg = importlib.import_module('ct_vg')
|
||||
ct_cmd = importlib.import_module('ct_cmd')
|
||||
|
||||
# Récupérer la scène UPBGE
|
||||
scene = bge.logic.getCurrentScene()
|
||||
@ -30,10 +31,10 @@ scene = bge.logic.getCurrentScene()
|
||||
|
||||
# Couleurs
|
||||
|
||||
# couleur_magenta = [0.800, 0.005, 0.315,1] # bouton non activable : magenta
|
||||
# couleur_orange = [0.799, 0.130, 0.063,1] # bouton activable : orange
|
||||
# couleur_blanc = [0.8, 0.8, 0.8, 1] # bouton focus : blanc
|
||||
# couleur_jaune = [0.8, 0.619, 0.021, 1] # bouton activé : jaune
|
||||
couleur_magenta = [0.800, 0.005, 0.315,1] # bouton non activable : magenta
|
||||
couleur_orange = [0.799, 0.130, 0.063,1] # bouton activable : orange
|
||||
couleur_blanc = [0.8, 0.8, 0.8, 1] # bouton focus : blanc
|
||||
couleur_jaune = [0.8, 0.619, 0.021, 1] # bouton activé : jaune
|
||||
|
||||
couleur_cmd = [0.8, 0.8, 0.8, 1] # blanc
|
||||
couleur_cmd_hl = [0.8, 0.619, 0.021, 1] # jaune
|
||||
@ -47,182 +48,52 @@ JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
|
||||
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
|
||||
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
|
||||
|
||||
# ###############################################################################
|
||||
# # Actionneurs
|
||||
# ###############################################################################
|
||||
###############################################################################
|
||||
# Construction
|
||||
###############################################################################
|
||||
|
||||
# # Action du simulateur pour le clignotant
|
||||
# def sml_clignotant (cont):
|
||||
# if scene.objects['Systeme']['run']:
|
||||
# obj = cont.owner
|
||||
# if obj['actif'] and scene.objects['Led allumee'].visible == False:
|
||||
# scene.objects['Led allumee'].setVisible(True,False)
|
||||
# scene.objects['Led'].setVisible(False,False)
|
||||
# # print ("Clignotant allumée")
|
||||
# if obj['actif']==False and scene.objects['Led allumee'].visible == True:
|
||||
# scene.objects['Led'].setVisible(True,False)
|
||||
# scene.objects['Led allumee'].setVisible(False,False)
|
||||
# # print ("Clignotant éteint")
|
||||
# Commande positionnement tour
|
||||
def cmd_construct_tour(cont):
|
||||
obj = cont.owner
|
||||
obj_Hl= scene.objects[obj.name+"-Hl"]
|
||||
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
|
||||
if scene.objects['Terrain']['construct_mode']==True:
|
||||
scene.objects['Terrain']['construct_mode']=False
|
||||
obj.worldScale=[1, 1, 1]
|
||||
obj.color = couleur_cmd
|
||||
obj_Hl.worldScale=[1, 1, 1]
|
||||
obj_Hl.color = couleur_cmd
|
||||
scene.objects['Tour_construc_mode'].setVisible(False,False)
|
||||
scene.objects['Tour_construc_mode'].color = couleur_cmd
|
||||
else:
|
||||
scene.objects['Terrain']['construct_mode']=True
|
||||
obj.worldScale=[1.25, 1.25, 1.25]
|
||||
obj.color = couleur_cmd_hl
|
||||
obj_Hl.worldScale=[1.25, 1.25, 1.25]
|
||||
obj_Hl.color = couleur_cmd_hl
|
||||
scene.objects['Tour_construc_mode'].setVisible(True,False)
|
||||
scene.objects['Tour_construc_mode'].color = couleur_cmd
|
||||
|
||||
# # Action du simulateur pour le moteur
|
||||
# def sml_moteur (cont):
|
||||
# if scene.objects['Systeme']['run']:
|
||||
# obj = cont.owner
|
||||
# pas_rot = math.pi/7 # z = 14
|
||||
# pas = 2.35619/0.3 # pas echelle 1:1 = 2.35619 -> pas à l'échelle de la maquette (0,3) : 2.35619/0.3 = 7,85396
|
||||
# vitesse = 0.05
|
||||
# engrenage_obj = scene.objects['Engrenage']
|
||||
# portail_obj = scene.objects['Portail']
|
||||
# if obj['actif_ouvrir']:
|
||||
# # print (scene.objects['Portail'].worldPosition.x)
|
||||
# engrenage_obj.applyRotation((0, 0, -pas_rot*vitesse), True)
|
||||
# portail_obj.applyMovement((-pas*vitesse, 0, 0), True)
|
||||
# # else:
|
||||
# if obj['actif_fermer']:
|
||||
# # print (scene.objects['Portail'].worldPosition.x)
|
||||
# engrenage_obj.applyRotation((0, 0, pas_rot*vitesse), True)
|
||||
# portail_obj.applyMovement((pas*vitesse, 0, 0), True)
|
||||
|
||||
# ###############################################################################
|
||||
# # Capteurs fin de course
|
||||
# ###############################################################################
|
||||
|
||||
# # Etat capteur fin de course portail ouvert
|
||||
# def sml_fdc_ouvert (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# obj = cont.owner
|
||||
# obj_etat=obj['actif']
|
||||
# obj_microrupteur=scene.objects['Microrupteur fdc ouvert']
|
||||
# # Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
|
||||
# if scene.objects['Portail'].localPosition.x <= 0 and obj['actif'] == False :
|
||||
# obj['actif'] = True
|
||||
# if scene.objects['Portail'].localPosition.x > 0 and obj_microrupteur['actif'] == False and obj['actif'] == True :
|
||||
# obj['actif'] = False
|
||||
# #Forçage
|
||||
# if obj_microrupteur['actif'] == True:
|
||||
# obj['actif'] = True
|
||||
# #Couleurs
|
||||
# if obj['actif'] == True and obj_microrupteur.color !=couleur_jaune:
|
||||
# obj_microrupteur.color =couleur_jaune
|
||||
# if obj['actif'] == False :
|
||||
# if obj_microrupteur['MO'] == True and obj_microrupteur.color !=couleur_blanc:
|
||||
# obj_microrupteur.color =couleur_blanc
|
||||
# if obj_microrupteur['MO'] == False and obj_microrupteur.color !=couleur_orange:
|
||||
# obj_microrupteur.color =couleur_orange
|
||||
|
||||
# # Etat capteur fin de course portail fermé
|
||||
# def sml_fdc_ferme (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# obj = cont.owner
|
||||
# obj_etat=obj['actif']
|
||||
# obj_microrupteur=scene.objects['Microrupteur fdc ferme']
|
||||
# # Etat capteur en fonction de la grille : worldPosition.x : 0 -> 65.5 et localPosition.x : 0 -> 218
|
||||
# if scene.objects['Portail'].localPosition.x >= 218 and obj['actif'] == False :
|
||||
# obj['actif'] = True
|
||||
# if scene.objects['Portail'].localPosition.x < 218 and obj_microrupteur['actif'] == False and obj['actif'] == True :
|
||||
# obj['actif'] = False
|
||||
# #Forçage
|
||||
# if obj_microrupteur['actif'] == True:
|
||||
# obj['actif'] = True
|
||||
# #Couleurs
|
||||
# if obj['actif'] == True and obj_microrupteur.color !=couleur_jaune:
|
||||
# obj_microrupteur.color =couleur_jaune
|
||||
# if obj['actif'] == False :
|
||||
# if obj_microrupteur['MO'] == True and obj_microrupteur.color !=couleur_blanc:
|
||||
# obj_microrupteur.color =couleur_blanc
|
||||
# if obj_microrupteur['MO'] == False and obj_microrupteur.color !=couleur_orange:
|
||||
# obj_microrupteur.color =couleur_orange
|
||||
|
||||
# # Forçage capteur fin de course avec la souris
|
||||
# def sml_microrupteur (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# sml_click(cont, cont.owner)
|
||||
|
||||
# ###############################################################################
|
||||
# # Capteurs barrage
|
||||
# ###############################################################################
|
||||
|
||||
# # Emetteur IR
|
||||
# def sml_emet_ir (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# obj = cont.owner
|
||||
# obj_emetteur_ir=scene.objects['Emetteur IR']
|
||||
# obj_recepteur_ir=scene.objects['Recepteur IR']
|
||||
# if obj['actif'] and scene.objects['Emetteur IR Led allumee'].visible == False:
|
||||
# scene.objects['Emetteur IR Led allumee'].setVisible(True,False)
|
||||
# scene.objects['Emetteur IR Led'].setVisible(False,False)
|
||||
# obj_emetteur_ir.color = couleur_orange
|
||||
# obj_recepteur_ir.color = couleur_orange
|
||||
# if obj['actif']==False and scene.objects['Emetteur IR Led allumee'].visible == True:
|
||||
# scene.objects['Emetteur IR Led'].setVisible(True,False)
|
||||
# scene.objects['Emetteur IR Led allumee'].setVisible(False,False)
|
||||
# obj_emetteur_ir.color = couleur_magenta
|
||||
# obj_recepteur_ir.color = couleur_magenta
|
||||
|
||||
# # Recepteur IR
|
||||
# def sml_recep_ir (cont):
|
||||
# if scene.objects['Systeme']['run'] and scene.objects['Module emetteur IR']['actif'] :
|
||||
# obj = cont.owner
|
||||
# sml_click(cont, scene.objects['Module recepteur IR'])
|
||||
# if scene.objects['Module recepteur IR']['actif']==True :
|
||||
# scene.objects['Emetteur IR'].color = couleur_jaune
|
||||
# scene.objects['Recepteur IR'].color = couleur_jaune
|
||||
|
||||
# ###############################################################################
|
||||
# # Boutons poussoirs
|
||||
# ###############################################################################
|
||||
|
||||
# # Gestion du click sur les éléments cliquables
|
||||
# def sml_click(cont, obj_activation):
|
||||
# obj = cont.owner
|
||||
# if cont.sensors['MO'].status == JUST_ACTIVATED :
|
||||
# obj.color = couleur_blanc
|
||||
# obj['MO']=True
|
||||
# if cont.sensors['Click'].status == JUST_ACTIVATED and scene.objects['Systeme']['manip_mode']==0:
|
||||
# if obj['MO']:
|
||||
# obj_activation['actif'] = True
|
||||
# obj.color = couleur_jaune
|
||||
# if cont.sensors['MO'].status == JUST_RELEASED :
|
||||
# obj['MO']=False
|
||||
# if cont.sensors['Click'].status == ACTIVATE :
|
||||
# obj.color = couleur_jaune
|
||||
# else:
|
||||
# obj.color = couleur_orange
|
||||
# if cont.sensors['Click'].status == JUST_RELEASED:
|
||||
# obj_activation['actif'] = False
|
||||
# obj.color = couleur_blanc
|
||||
# if cont.sensors['MO'].status != ACTIVATE :
|
||||
# obj.color = couleur_orange
|
||||
|
||||
# # Bouton pousssoir coté rue
|
||||
# def sml_bp_rue (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# sml_click(cont, scene.objects['Module bouton cote rue'])
|
||||
|
||||
# # Bouton pousssoir coté cour
|
||||
# def sml_bp_cour (cont):
|
||||
# if scene.objects['Systeme']['run'] :
|
||||
# sml_click(cont, scene.objects['Module bouton cote cour'])
|
||||
# Affiche position
|
||||
def construct_tour_pos(cont):
|
||||
obj = cont.owner
|
||||
if scene.objects['Terrain']['construct_mode']==True:
|
||||
hitObject = cont.sensors['MO'].hitObject
|
||||
hitPosition = cont.sensors['MO'].hitPosition
|
||||
if hitObject is not None :
|
||||
print (hitObject.name)
|
||||
print (hitPosition)
|
||||
scene.objects['Tour_construc_mode'].worldPosition.x=round(hitPosition.x)
|
||||
scene.objects['Tour_construc_mode'].worldPosition.y=round(hitPosition.y)
|
||||
scene.objects['Tour_construc_mode'].worldPosition.z=0.2
|
||||
|
||||
###############################################################################
|
||||
# Cycle
|
||||
###############################################################################
|
||||
|
||||
# Initialisation du cycle de fonctionnement normal
|
||||
# Le moteur est géré en continue.
|
||||
def cycle_init (cont):
|
||||
|
||||
# Clignotant
|
||||
scene.objects['Module led']['actif']=False
|
||||
scene.objects['Led allumee'].setVisible(False,False)
|
||||
scene.objects['Led'].setVisible(True,False)
|
||||
|
||||
# Emetteur IR
|
||||
scene.objects['Module emetteur IR']['actif']=False
|
||||
scene.objects['Emetteur IR Led allumee'].setVisible(False,False)
|
||||
scene.objects['Emetteur IR Led'].setVisible(True,False)
|
||||
scene.objects['Emetteur IR'].color = couleur_magenta
|
||||
scene.objects['Recepteur IR'].color = couleur_magenta
|
||||
# def cycle_init (cont):
|
||||
# pass
|
||||
|
||||
# Mise en route et pause du cycle
|
||||
def cycle_run (cont):
|
||||
@ -242,19 +113,32 @@ def cycle_run (cont):
|
||||
scene.objects['Run-Hl'].setVisible(False,False)
|
||||
scene.objects['Pause-Hl'].setVisible(True,False)
|
||||
if scene.objects['Terrain']['thread_run']==False: # Lancement du thread utilisateur
|
||||
# Supprimer les tours
|
||||
for i in range (len(scene.objects)):
|
||||
if scene.objects[i].name=="Tour":
|
||||
scene.objects[i].endObject()
|
||||
scene.objects['Stop'].setVisible(True,False)
|
||||
# importlib.reload(porcou_cmd) # Lecture dynamique du script python (risque de Segfault de Blender)
|
||||
# porcou_cmd.start()
|
||||
scene.objects['Terrain']['thread_run']=True
|
||||
# importlib.reload(ct_cmd) # Lecture dynamique du script python (risque de Segfault de Blender)
|
||||
# importlib.reload(ct_vg) # Lecture dynamique du script python (risque de Segfault de Blender)
|
||||
ct_cmd.start() # Execution des commandes
|
||||
ct_vg.start() # Lancement des vagues
|
||||
|
||||
# Arrêt et réinitialisation du cycle
|
||||
def cycle_stop (cont):
|
||||
if cont.sensors['Click'].status == JUST_ACTIVATED and cont.sensors['MO'].positive and scene.objects['Terrain']['manip_mode']==0:
|
||||
|
||||
# Arrêt du thread utilisateur
|
||||
# Arrêt des threads utilisateurs
|
||||
scene.objects['Terrain']['run']=False
|
||||
scene.objects['Terrain']['thread_run']=False
|
||||
# porcou_cmd.stop()
|
||||
ct_cmd.stop() # Stop des commandes
|
||||
ct_vg.stop() # Stop des vagues
|
||||
|
||||
# Supprimer les enemis
|
||||
for i in range (len(scene.objects)):
|
||||
if scene.objects[i].name=="Ufo":
|
||||
scene.objects[i].endObject()
|
||||
|
||||
# Commandes
|
||||
scene.objects['Pause'].setVisible(False,False)
|
||||
scene.objects['Run'].setVisible(True,False)
|
||||
@ -262,18 +146,16 @@ def cycle_stop (cont):
|
||||
scene.objects['Stop-Hl'].setVisible(False,False)
|
||||
|
||||
# Fin naturelle du cycle
|
||||
def cycle_fin (cont):
|
||||
def cycle_fin ():
|
||||
|
||||
# Arrêt du thread utilisateur
|
||||
scene.objects['Terrain']['run']=False
|
||||
scene.objects['Terrain']['thread_run']=False
|
||||
# porcou_cmd.stop()
|
||||
|
||||
# Commandes
|
||||
scene.objects['Pause'].setVisible(False,False)
|
||||
scene.objects['Run'].setVisible(True,False)
|
||||
scene.objects['Stop'].setVisible(False,False)
|
||||
scene.objects['Stop-Hl'].setVisible(False,False)
|
||||
scene.objects['Terrain']['run']=False
|
||||
scene.objects['Terrain']['thread_run']=False
|
||||
|
||||
# Commandes
|
||||
scene.objects['Pause'].setVisible(False,False)
|
||||
scene.objects['Run'].setVisible(True,False)
|
||||
scene.objects['Stop'].setVisible(False,False)
|
||||
scene.objects['Stop-Hl'].setVisible(False,False)
|
||||
|
||||
###############################################################################
|
||||
# Commandes
|
||||
@ -287,17 +169,18 @@ def cmd_init():
|
||||
scene.objects['Pause-Hl'].setVisible(False,False)
|
||||
scene.objects['Stop'].setVisible(False,False)
|
||||
scene.objects['Stop-Hl'].setVisible(False,False)
|
||||
# scene.objects['Raz-vue-Hl'].setVisible(False,False)
|
||||
scene.objects['Construc-Hl'].setVisible(False,False)
|
||||
|
||||
scene.objects['Tour_construc_mode'].setVisible(False,False)
|
||||
# scene.objects['Raz-vue-Hl'].setVisible(False,False)
|
||||
# scene.objects['Aide-cmd-Hl'].setVisible(False,False)
|
||||
|
||||
# Le highlight des commandes
|
||||
def cmd_hl(cont):
|
||||
obj = cont.owner
|
||||
|
||||
print ("ok 1")
|
||||
# Activation
|
||||
if cont.sensors['MO'].status == JUST_ACTIVATED and scene.objects['Terrain']['manip_mode']==0:
|
||||
print ("ok 2")
|
||||
if obj.name!="Run" and obj.name!="Pause" and obj.name!="Run-Hl" and obj.name!="Pause-Hl" and obj.name!="Stop" and obj.name!="Stop-Hl":
|
||||
obj.setVisible(False,True)
|
||||
scene.objects[obj.name+'-Hl'].setVisible(True,True)
|
||||
@ -338,12 +221,11 @@ def cmd_hl(cont):
|
||||
scene.objects['Stop-Hl'].setVisible(False,False)
|
||||
scene.objects['Stop'].setVisible(True,False)
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Gestion du clavier
|
||||
###############################################################################
|
||||
|
||||
# Mode : Pan(1) avec Shift, Zoom (2) avec Ctrl, Orbit (0),
|
||||
# Mode : Pan(1) avec Shift, Zoom (2) avec Ctrl
|
||||
def mode(cont):
|
||||
obj = cont.owner
|
||||
keyboard = bge.logic.keyboard
|
||||
@ -387,20 +269,33 @@ def mode(cont):
|
||||
scene.objects['Terrain']['run']=True
|
||||
scene.objects['Run'].setVisible(False,False)
|
||||
scene.objects['Pause'].setVisible(True,False)
|
||||
if scene.objects['Terrain']['thread_run']==False: # Lancement du thread utilisateur
|
||||
if scene.objects['Terrain']['thread_run']==False: # Lancement du thread du jeu
|
||||
# Supprimer les tours
|
||||
for i in range (len(scene.objects)):
|
||||
if scene.objects[i].name=="Tour":
|
||||
scene.objects[i].endObject()
|
||||
scene.objects['Stop'].setVisible(True,False)
|
||||
# importlib.reload(porcou_cmd) # Lecture dynamique du script python
|
||||
# porcou_cmd.start()
|
||||
|
||||
scene.objects['Terrain']['thread_run']=True
|
||||
# importlib.reload(ct_cmd) # Lecture dynamique du script python (risque de Segfault de Blender)
|
||||
# importlib.reload(ct_vg) # Lecture dynamique du script python (risque de Segfault de Blender)
|
||||
ct_cmd.start() # Execution des commandes
|
||||
ct_vg.start() # Lancement des vagues
|
||||
|
||||
# Touche F6 -> Stop / Init
|
||||
if JUST_ACTIVATED in keyboard.inputs[bge.events.F6KEY].queue:
|
||||
if scene.objects['Terrain']['thread_run']==True:
|
||||
|
||||
# Arrêt du thread utilisateur
|
||||
# Arrêt des threads utilisateurs
|
||||
scene.objects['Terrain']['run']=False
|
||||
scene.objects['Terrain']['thread_run']=False
|
||||
# porcou_cmd.stop()
|
||||
|
||||
ct_cmd.stop() # Stop des commandes
|
||||
ct_vg.stop() # Stop des vagues
|
||||
|
||||
# Supprimer les enemis
|
||||
for i in range (len(scene.objects)):
|
||||
if scene.objects[i].name=="Ufo":
|
||||
scene.objects[i].endObject()
|
||||
|
||||
# Commandes
|
||||
scene.objects['Pause'].setVisible(False,False)
|
||||
scene.objects['Run'].setVisible(True,False)
|
||||
|
44
ct_cmd.py
Normal file
44
ct_cmd.py
Normal file
@ -0,0 +1,44 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
from ct_lib import * # Bibliothèque CodeTower
|
||||
import time
|
||||
|
||||
###############################################################################
|
||||
# ct_cmd.py
|
||||
# @title: Commandes pour le jeu codetower
|
||||
# @project: CodeTower
|
||||
# @lang: fr
|
||||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||||
# @copyright: Copyright (C) 2022 Philippe Roy
|
||||
# @license: GNU GPL
|
||||
#
|
||||
# Ce simulateur est un jeu du type tower defense où les tours sont à piloter par la programmation Python.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
###############################################################################
|
||||
# Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
|
||||
###############################################################################
|
||||
|
||||
threads=[]
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
def start():
|
||||
scene.objects['Terrain']['thread_cmd']=True
|
||||
thread_start(threads, "commandes", commandes)
|
||||
|
||||
def stop():
|
||||
thread_stop(threads, "commandes")
|
||||
|
||||
###############################################################################
|
||||
# Instructions élémentaires
|
||||
###############################################################################
|
||||
|
||||
def commandes():
|
||||
|
||||
# Coder vos commandes ici ...
|
||||
|
||||
ct_tour(5,5)
|
||||
|
||||
tempo(2)
|
||||
print ("Thread commandes #", len(threads)-1, "arrivé au bout -> fermeture.") # Tâche close (thread) << NE PAS MODIFIER CETTE LIGNE >>
|
||||
scene.objects['Terrain']['thread_cmd']=False # Fin du cycle << NE PAS MODIFIER CETTE LIGNE >>
|
161
ct_lib.py
Normal file
161
ct_lib.py
Normal file
@ -0,0 +1,161 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
import ct # Bibliothèque CodeTower
|
||||
import threading # Multithreading
|
||||
import trace
|
||||
import sys
|
||||
import time
|
||||
import mathutils
|
||||
|
||||
###############################################################################
|
||||
# ct_lib.py
|
||||
# @title: Bibliothèque utilisateur
|
||||
# @project: CodeTower
|
||||
# @lang: fr
|
||||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||||
# @copyright: Copyright (C) 2022 Philippe Roy
|
||||
# @license: GNU GPL
|
||||
#
|
||||
# Commandes déclenchées par les joueurs : ct_*
|
||||
# Commandes déclenchées par la scene 3D : scn_*
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
# # Couleurs
|
||||
|
||||
# couleur_magenta = [0.800, 0.005, 0.315,1] # bouton non activable : magenta
|
||||
# couleur_orange = [0.799, 0.130, 0.063,1] # bouton activable : orange
|
||||
# couleur_blanc = [0.8, 0.8, 0.8, 1] # bouton focus : blanc
|
||||
# couleur_jaune = [0.8, 0.619, 0.021, 1] # bouton activé : jaune
|
||||
|
||||
# couleur_cmd = [0.8, 0.8, 0.8, 1] # blanc
|
||||
# couleur_cmd_hl = [0.8, 0.619, 0.021, 1] # jaune
|
||||
# couleur_lien = [0.024, 0.006, 0.8, 1] # bleu
|
||||
# couleur_lien_hl = [0.8, 0.005, 0.315, 1] # majenta
|
||||
|
||||
# Récupérer les objets 3D
|
||||
scene = bge.logic.getCurrentScene()
|
||||
# print("Objets de la scene : ", scene.objects)
|
||||
|
||||
###############################################################################
|
||||
# Méthode kill pour les tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
class thread_with_trace(threading.Thread):
|
||||
def __init__(self, *args, **keywords):
|
||||
threading.Thread.__init__(self, *args, **keywords)
|
||||
self.killed = False
|
||||
|
||||
def start(self):
|
||||
self.__run_backup = self.run
|
||||
self.run = self.__run
|
||||
threading.Thread.start(self)
|
||||
|
||||
def __run(self):
|
||||
sys.settrace(self.globaltrace)
|
||||
self.__run_backup()
|
||||
self.run = self.__run_backup
|
||||
|
||||
def globaltrace(self, frame, event, arg):
|
||||
if event == 'call':
|
||||
return self.localtrace
|
||||
else:
|
||||
return None
|
||||
|
||||
def localtrace(self, frame, event, arg):
|
||||
if self.killed:
|
||||
if event == 'line':
|
||||
raise SystemExit()
|
||||
return self.localtrace
|
||||
|
||||
def kill(self):
|
||||
self.killed = True
|
||||
|
||||
###############################################################################
|
||||
# Start et stop des tâches (threads)
|
||||
###############################################################################
|
||||
|
||||
def thread_start(threads, type_txt, fct):
|
||||
threads.append(thread_with_trace(target = fct))
|
||||
threads[len(threads)-1].start()
|
||||
print ("Thread",type_txt, "#", len(threads)-1, "ouvert.")
|
||||
|
||||
def thread_stop(threads, type_txt):
|
||||
i=0
|
||||
zombie_flag=False
|
||||
for t in threads:
|
||||
if not t.is_alive():
|
||||
print ("Thread",type_txt, "#",i,"fermé.")
|
||||
else:
|
||||
print ("Thread",type_txt, "#",i,"encore ouvert ...")
|
||||
t.kill()
|
||||
t.join()
|
||||
if not t.is_alive():
|
||||
print ("Thread",type_txt, "#",i,"tué.")
|
||||
else:
|
||||
print ("Thread",type_txt, "#",i,"zombie ...")
|
||||
zombie_flag=True
|
||||
i +=1
|
||||
if zombie_flag==False:
|
||||
print ("Tous les threads",type_txt, "sont fermés.")
|
||||
else:
|
||||
print ("Il reste des threads",type_txt, "zombies.")
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Vagues
|
||||
###############################################################################
|
||||
|
||||
# Création d'un enemi
|
||||
def ct_ufo(x,y):
|
||||
ufo= scene.addObject("Ufo", scene.objects['Terrain'])
|
||||
ufo.worldPosition=mathutils.Vector((x,y,0.37))
|
||||
ufo.worldScale=mathutils.Vector((0.25,0.25,0.25))
|
||||
ufo.actuators['Steering'].velocity=ufo['vitesse']
|
||||
|
||||
###############################################################################
|
||||
# Tours
|
||||
###############################################################################
|
||||
|
||||
# Création d'une tour
|
||||
def ct_tour(x,y):
|
||||
tour= scene.addObject("Tour", scene.objects['Terrain'])
|
||||
tour.worldPosition=mathutils.Vector((x,y,0.2))
|
||||
tour.worldScale=mathutils.Vector((1,1,1))
|
||||
|
||||
# Réaction d'une tour
|
||||
def scn_tour_near(cont):
|
||||
obj = cont.owner
|
||||
sensor = obj.sensors['Near']
|
||||
|
||||
# Supression enemi
|
||||
if len(sensor.hitObjectList)>0:
|
||||
bullet= scene.addObject("Bullet", scene.objects['Terrain'], 60*2, False)
|
||||
bullet.mass=0.001 # bullet.applyForce=((0,0,9.81),True)
|
||||
bullet.worldPosition=mathutils.Vector((obj.worldPosition.x,obj.worldPosition.y,1.5))
|
||||
# bullet.worldScale=mathutils.Vector((0.5,0.5,0.5))
|
||||
bullet.worldScale=[0.5,0.5,0.5]
|
||||
bullet.worldLinearVelocity.x = (sensor.hitObjectList[0].worldPosition.x-bullet.worldPosition.x)*bullet['vitesse']
|
||||
bullet.worldLinearVelocity.y= (sensor.hitObjectList[0].worldPosition.y-bullet.worldPosition.y)*bullet['vitesse']
|
||||
bullet.worldLinearVelocity.z = (sensor.hitObjectList[0].worldPosition.z+0.1-bullet.worldPosition.z)*bullet['vitesse']
|
||||
|
||||
# Fin de la vague
|
||||
if len(sensor.hitObjectList)==0:
|
||||
print ("fin vague")
|
||||
if scene.objects['Terrain']['thread_vague']==False:
|
||||
print ("fin vague2")
|
||||
ct.cycle_fin ()
|
||||
|
||||
# # Positionnement tour
|
||||
# def scn_tour_construct(cont):
|
||||
# obj = cont.owner
|
||||
# obj.worldScale=[1.25, 1.25, 1.25]
|
||||
# obj.color = couleur_cmd_hl
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Temporisation
|
||||
###############################################################################
|
||||
|
||||
def tempo (duree):
|
||||
time.sleep(duree)
|
||||
|
44
ct_vg.py
Normal file
44
ct_vg.py
Normal file
@ -0,0 +1,44 @@
|
||||
import bge # Bibliothèque Blender Game Engine (UPBGE)
|
||||
from ct_lib import * # Bibliothèque CodeTower
|
||||
|
||||
###############################################################################
|
||||
# ct_cmd.py
|
||||
# @title: Vagues pour le jeu codetower
|
||||
# @project: CodeTower
|
||||
# @lang: fr
|
||||
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
||||
# @copyright: Copyright (C) 2022 Philippe Roy
|
||||
# @license: GNU GPL
|
||||
#
|
||||
# Ce simulateur est un jeu du type tower defense où les tours sont à piloter par la programmation Python.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
###############################################################################
|
||||
# Gestion des tâches (threads) << NE PAS MODIFIER CETTE SECTION >>
|
||||
###############################################################################
|
||||
|
||||
threads=[]
|
||||
scene = bge.logic.getCurrentScene()
|
||||
|
||||
def start():
|
||||
scene.objects['Terrain']['thread_vagues']=True
|
||||
thread_start(threads, "vagues", vagues)
|
||||
|
||||
def stop():
|
||||
thread_stop(threads, "vagues")
|
||||
|
||||
###############################################################################
|
||||
# Instructions élémentaires
|
||||
###############################################################################
|
||||
|
||||
def vagues():
|
||||
|
||||
# Vague 1
|
||||
for i in range (4):
|
||||
ct_ufo(15,3)
|
||||
tempo(1)
|
||||
|
||||
tempo(2)
|
||||
print ("Thread vagues #", len(threads)-1, "arrivé au bout -> fermeture.") # Tâche close (thread) << NE PAS MODIFIER CETTE LIGNE >>
|
||||
scene.objects['Terrain']['thread_vagues']=False # Fin du cycle << NE PAS MODIFIER CETTE LIGNE >>
|
Loading…
x
Reference in New Issue
Block a user