Accueil > > > VPYTHON ET L'ANIMATION : JEUX
VPYTHON ET L'ANIMATION : JEUX
Information sur la source
Description
La guerre des étoiles en environ de 200 lignes de code. Ce source vous montre toute la puissance de Vpython pour réaliser des animations aussi fluides que réalistes. A vous d'ajouter le contrôle du joystick...
Source
- #!/usr/bin/env python
- # -*- coding: ISO-8859-15 -*-
- #Utiliser au moins Python 2.5 et Vpython 2.5-3.2.11
- from visual import *
- from random import Random, randint, sample, uniform
- from math import radians, degrees
-
- sourceRandom = Random()
- scene.fullscreen = True
- scene.scale =(.01, .01, .01)
-
- class TieWing(object):
- """
- Aile du chasseur
- """
- def __init__( self, center = ( 0., 0., 0. ) ,
- dimension = ( 1., 2., 3.),
- objColor = color.white,
- frame = None ):
- """
- center = center of the wing ( x, y, z )
- dimension = dimension of the wing ( thickness, size, height )
- objColor = ( R, G, B )
- """
- [ self.x0, self.y0, self.z0 ] = center
- [ self.thickness, self.size, self.height ] = dimension
- self.vObj = convex( color = objColor , frame = frame)
- self.Tir_1 = sphere(frame = frame,
- pos=(self.x0,self.y0, self.z0 +0.7 ),
- radius = 0.1)
- def draw( self ):
- """
- Drawing of the wing.
- """
- y1 = self.y0 + self.size * 0.5
- z1 = self.z0 + self.height * 0.25
- y2 = self.y0
- z2 = self.z0 + self.height * 0.5
- y3 = self.y0 - self.size * 0.5
- z3 = self.z0 + self.height * 0.25
- y4 = self.y0 - self.size * 0.5
- z4 = self.z0 - self.height * 0.25
- y5 = self.y0
- z5 = self.z0 - self.height * 0.5
- y6 = self.y0 + self.size * 0.5
- z6 = self.z0 - self.height * 0.25
- self.vObj.pos =[ ( self.x0, y1, z1 ), ( self.x0, y2, z2 ),
- ( self.x0, y3, z3 ), ( self.x0, y4, z4 ),
- ( self.x0, y5, z5 ),( self.x0, y6, z6 ) ]
-
- class Etoile :
- """
- Represente une etoile qui scintille
- """
- def __init__(self, Position):
- """
- Position : position absolue de l'étoile dans le ciel
- """
- sphere (color = (1,1,1), pos = Position, radius = 1)
- x,y,z = Position
- L = 1.5
- #Rayons etoiles
- self.curve = curve (pos =[(x,y,z),(L+x,y,z),(x,y,z),(-L+x,y,z),
- (x,y,z),(x,L+y,z),(x,y,z),(x,-L+y,z),
- (x,y,z),(x,y,L+z),(x,y,z),(x,y,-L+z)])
-
- def move (self):
- """
- Modification de l'etoile dans le temps
- """
- #On fait clignoter chaque etoile
- self.curve.visible = 1 - self.curve.visible
-
- class Tir:
- def __init__( self, position = ( 0., 0., 0. )):
- self.form = frame (pos=position)
- trait = (position [0]*0.90,position [1]*0.90,position [2]*0.90)
- self.theCurve = curve (pos = [position,trait],
- frame = self.form, color = (1,1,1))
- self.compteurVie = 0
- def move(self):
- self.form.pos = (self.form.pos[0]*0.55,
- self.form.pos[1]*0.55,
- self.form.pos[2]*0.55)
- self.compteurVie +=1
- if self.compteurVie > 10 :
- self.theCurve.visible = False
- del self
-
- class TieFighter:
- """
- Represente un vaisseau
- """
- def __init__( self, position = ( 0., 0., 0. )):
- self.CalculateurPosition = None
- self.tie = frame( name = "TIE" )
- self.tieColor = (sourceRandom.random() ,
- sourceRandom.random(),
- sourceRandom.random())
- self.centerTieFighter = sphere( frame = self.tie,
- pos = position,
- radius = 0.5,
- color = self.tieColor )
-
- leftWing = TieWing(frame = self.tie,
- center = ( position[0] - 0.5,
- position[1],
- position[2] ),
- dimension = ( 1., 3., 5.),
- objColor = self.tieColor )
-
- leftWing.draw()
- rightWing = TieWing(frame = self.tie,
- center = ( position[0] + 0.5, position[1], position[2] ),
- dimension = ( 1., 3., 5.),
- objColor = self.tieColor )
- rightWing.draw()
- self.tie.pos = position
- self.directionXPredilection = sourceRandom.choice([-1,1])
- self.SensZ = 1
- self.angleRotation = 0.1*sourceRandom.random()*sourceRandom.choice([-1,1])
- self.angleRotationPropreY = 0.01*sourceRandom.random()*sourceRandom.choice([-1,1])
- self.Tirs = []
-
- def move(self,dt):
- X,Y,Z = self.tie.pos
- if Z > 0:
- newX = X + (.5*self.directionXPredilection)
- else:
- newX = X - (.5*self.directionXPredilection)
- newY = Y
- newZ = Z - self.SensZ
- self.tie.pos = (newX, newY, newZ)
- self.tie.rotate( angle = self.angleRotation )
- axeY = self.tie.axis [1] + self.directionXPredilection *self.angleRotationPropreY
- self.tie.axis = (self.tie.axis [0],axeY,self.tie.axis [2])
- NtotalTir = 100
- chance_Tir = sourceRandom.randint(0,NtotalTir)
- if chance_Tir > NtotalTir-1:
- self.Tirs.append (Tir (self.tie.pos))
- for unTir in self.Tirs:
- unTir.move ()
-
- class FighterSpace (object):
- ETOILES_NUMBER = 500
- def __init__(self,FighterNumber):
- self.FighterNumber = FighterNumber
- self.FighterList = []
- self.etoiles = []
-
- def drawEarth(self):
- L = []
- Color=[0.5*sourceRandom.random() ,
- 0.5*sourceRandom.random(),
- 0.5*sourceRandom.random()]
- c = convex(color=Color)
- for i in range(100):
- L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
- c.pos = L
- L = []
- d = convex(color=(0,0.5,0.5))
- for i in range(100):
- L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
- d.pos = L
- L = []
- Color=[0.5*sourceRandom.random() ,
- 0.5*sourceRandom.random(),
- 0.5*sourceRandom.random()]
- e = convex(color=Color)
- for i in range(100):
- L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
- e.pos = L
-
- def create_Fighter (self):
- for aTieFighter in range(self.FighterNumber):
- Position = array((20*sourceRandom.random()*sourceRandom.choice([-1,1]),
- 20*sourceRandom.random()*sourceRandom.choice([-1,1]),
- randint (10,200)))
- theTieFighter = TieFighter(position=Position)
- self.FighterList.append(theTieFighter)
-
- def goSimu (self):
- while True:
- framePerSecond = 50
- rate(framePerSecond)
- for aTieFighter in self.FighterList:
- aTieFighter.move(dt = 1.0/framePerSecond)
-
- # Selection de 10% des etoiles à faire clignoter: realisation d'un echantillonage
- nbEtoiles = len(self.etoiles)
- numeros_etoiles_a_faire_clignoter = sample(xrange(nbEtoiles), int(nbEtoiles*0.1))
- for numEtoiles in numeros_etoiles_a_faire_clignoter:
- self.etoiles[numEtoiles].move()
-
- def draw_space (self):
- #Dessin de beaucoup d'etoiles un peu partour dans l'espace
- RayonUnivers = 2000
- for i in range (FighterSpace.ETOILES_NUMBER):
- positionEtoile =array((RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1]),
- RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1]),
- RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1])))
- self.etoiles.append(Etoile(positionEtoile))
-
- if __name__ == '__main__':
- FighterNumber = 100
- theFighterSpace= FighterSpace(FighterNumber)
- theFighterSpace.create_Fighter()
- theFighterSpace.drawEarth()
- theFighterSpace.draw_space ()
- theFighterSpace.goSimu()
-
-
#!/usr/bin/env python
# -*- coding: ISO-8859-15 -*-
#Utiliser au moins Python 2.5 et Vpython 2.5-3.2.11
from visual import *
from random import Random, randint, sample, uniform
from math import radians, degrees
sourceRandom = Random()
scene.fullscreen = True
scene.scale =(.01, .01, .01)
class TieWing(object):
"""
Aile du chasseur
"""
def __init__( self, center = ( 0., 0., 0. ) ,
dimension = ( 1., 2., 3.),
objColor = color.white,
frame = None ):
"""
center = center of the wing ( x, y, z )
dimension = dimension of the wing ( thickness, size, height )
objColor = ( R, G, B )
"""
[ self.x0, self.y0, self.z0 ] = center
[ self.thickness, self.size, self.height ] = dimension
self.vObj = convex( color = objColor , frame = frame)
self.Tir_1 = sphere(frame = frame,
pos=(self.x0,self.y0, self.z0 +0.7 ),
radius = 0.1)
def draw( self ):
"""
Drawing of the wing.
"""
y1 = self.y0 + self.size * 0.5
z1 = self.z0 + self.height * 0.25
y2 = self.y0
z2 = self.z0 + self.height * 0.5
y3 = self.y0 - self.size * 0.5
z3 = self.z0 + self.height * 0.25
y4 = self.y0 - self.size * 0.5
z4 = self.z0 - self.height * 0.25
y5 = self.y0
z5 = self.z0 - self.height * 0.5
y6 = self.y0 + self.size * 0.5
z6 = self.z0 - self.height * 0.25
self.vObj.pos =[ ( self.x0, y1, z1 ), ( self.x0, y2, z2 ),
( self.x0, y3, z3 ), ( self.x0, y4, z4 ),
( self.x0, y5, z5 ),( self.x0, y6, z6 ) ]
class Etoile :
"""
Represente une etoile qui scintille
"""
def __init__(self, Position):
"""
Position : position absolue de l'étoile dans le ciel
"""
sphere (color = (1,1,1), pos = Position, radius = 1)
x,y,z = Position
L = 1.5
#Rayons etoiles
self.curve = curve (pos =[(x,y,z),(L+x,y,z),(x,y,z),(-L+x,y,z),
(x,y,z),(x,L+y,z),(x,y,z),(x,-L+y,z),
(x,y,z),(x,y,L+z),(x,y,z),(x,y,-L+z)])
def move (self):
"""
Modification de l'etoile dans le temps
"""
#On fait clignoter chaque etoile
self.curve.visible = 1 - self.curve.visible
class Tir:
def __init__( self, position = ( 0., 0., 0. )):
self.form = frame (pos=position)
trait = (position [0]*0.90,position [1]*0.90,position [2]*0.90)
self.theCurve = curve (pos = [position,trait],
frame = self.form, color = (1,1,1))
self.compteurVie = 0
def move(self):
self.form.pos = (self.form.pos[0]*0.55,
self.form.pos[1]*0.55,
self.form.pos[2]*0.55)
self.compteurVie +=1
if self.compteurVie > 10 :
self.theCurve.visible = False
del self
class TieFighter:
"""
Represente un vaisseau
"""
def __init__( self, position = ( 0., 0., 0. )):
self.CalculateurPosition = None
self.tie = frame( name = "TIE" )
self.tieColor = (sourceRandom.random() ,
sourceRandom.random(),
sourceRandom.random())
self.centerTieFighter = sphere( frame = self.tie,
pos = position,
radius = 0.5,
color = self.tieColor )
leftWing = TieWing(frame = self.tie,
center = ( position[0] - 0.5,
position[1],
position[2] ),
dimension = ( 1., 3., 5.),
objColor = self.tieColor )
leftWing.draw()
rightWing = TieWing(frame = self.tie,
center = ( position[0] + 0.5, position[1], position[2] ),
dimension = ( 1., 3., 5.),
objColor = self.tieColor )
rightWing.draw()
self.tie.pos = position
self.directionXPredilection = sourceRandom.choice([-1,1])
self.SensZ = 1
self.angleRotation = 0.1*sourceRandom.random()*sourceRandom.choice([-1,1])
self.angleRotationPropreY = 0.01*sourceRandom.random()*sourceRandom.choice([-1,1])
self.Tirs = []
def move(self,dt):
X,Y,Z = self.tie.pos
if Z > 0:
newX = X + (.5*self.directionXPredilection)
else:
newX = X - (.5*self.directionXPredilection)
newY = Y
newZ = Z - self.SensZ
self.tie.pos = (newX, newY, newZ)
self.tie.rotate( angle = self.angleRotation )
axeY = self.tie.axis [1] + self.directionXPredilection *self.angleRotationPropreY
self.tie.axis = (self.tie.axis [0],axeY,self.tie.axis [2])
NtotalTir = 100
chance_Tir = sourceRandom.randint(0,NtotalTir)
if chance_Tir > NtotalTir-1:
self.Tirs.append (Tir (self.tie.pos))
for unTir in self.Tirs:
unTir.move ()
class FighterSpace (object):
ETOILES_NUMBER = 500
def __init__(self,FighterNumber):
self.FighterNumber = FighterNumber
self.FighterList = []
self.etoiles = []
def drawEarth(self):
L = []
Color=[0.5*sourceRandom.random() ,
0.5*sourceRandom.random(),
0.5*sourceRandom.random()]
c = convex(color=Color)
for i in range(100):
L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
c.pos = L
L = []
d = convex(color=(0,0.5,0.5))
for i in range(100):
L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
d.pos = L
L = []
Color=[0.5*sourceRandom.random() ,
0.5*sourceRandom.random(),
0.5*sourceRandom.random()]
e = convex(color=Color)
for i in range(100):
L.append(20*(vector(0,1) + norm((uniform(-1,1),uniform(-1,1),uniform(-1,1)))))
e.pos = L
def create_Fighter (self):
for aTieFighter in range(self.FighterNumber):
Position = array((20*sourceRandom.random()*sourceRandom.choice([-1,1]),
20*sourceRandom.random()*sourceRandom.choice([-1,1]),
randint (10,200)))
theTieFighter = TieFighter(position=Position)
self.FighterList.append(theTieFighter)
def goSimu (self):
while True:
framePerSecond = 50
rate(framePerSecond)
for aTieFighter in self.FighterList:
aTieFighter.move(dt = 1.0/framePerSecond)
# Selection de 10% des etoiles à faire clignoter: realisation d'un echantillonage
nbEtoiles = len(self.etoiles)
numeros_etoiles_a_faire_clignoter = sample(xrange(nbEtoiles), int(nbEtoiles*0.1))
for numEtoiles in numeros_etoiles_a_faire_clignoter:
self.etoiles[numEtoiles].move()
def draw_space (self):
#Dessin de beaucoup d'etoiles un peu partour dans l'espace
RayonUnivers = 2000
for i in range (FighterSpace.ETOILES_NUMBER):
positionEtoile =array((RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1]),
RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1]),
RayonUnivers*sourceRandom.random()*sourceRandom.choice([-1,1])))
self.etoiles.append(Etoile(positionEtoile))
if __name__ == '__main__':
FighterNumber = 100
theFighterSpace= FighterSpace(FighterNumber)
theFighterSpace.create_Fighter()
theFighterSpace.drawEarth()
theFighterSpace.draw_space ()
theFighterSpace.goSimu()
Conclusion
Quelques classes et le tour est joué. Avec un peu d'imagination et de travail, on peut faire un vrai jeux.
Historique
- 30 octobre 2007 23:07:24 :
- correction otho.
- 01 novembre 2007 20:34:24 :
- correction de code mal copié, et modification du code "a= x if y else z" valable qu'a partir de Python 2.5
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Qu'est ce que le python permet de réaliser!! [ par Mansuz ]
Mansuz Salut! J'aurais aimé savoir ce qui peut être réalisé en Python. Par exemple Flash c'est les petites anims, des jeux funs. Le C pour fair des a
charger une image 3D [ par ghadroud ]
Bonjour,Je suis sensée développer une application en python qui charge un image 3D et qui soit inclue dans la page web.Comment procéder a ca?merci
CHERCHE FORMATEUR PYTHON orienté 3D (XSI) [ par potoche ]
Bonjour, Je cherche un spécialiste python appliqué aux logiciels 3D style XSI. Il s'agit d'aider les gros studio à développer leurs outils à l'aide d
[clos] un morpion 3D ( Débutant) [ par heeeeeeeelp ]
Bonsoir ; [size=300][u][b]Alerte[/b][/u] [/size]a tous les geek et/ou fanatique d'informatique & de programmation ,, j'ai besoin de votre génie . Je s
Morpion 3D [ par lola413 ]
Bonjour, Dans le cadre de mon cours de programmation j'ai pour exercice (a rendre pour le 23, youpi!) de réaliser un morpion 4*4 en 3D (alignement de
|
Derniers Blogs
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Forum
PYVISA PROBLèMEPYVISA PROBLèME par sandrine44
Cliquez pour lire la suite par sandrine44
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|