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
|
Derniers Blogs
ENUMERABLECOLLECTIONENUMERABLECOLLECTION par Matthieu MEZIL
Prenons le scénario suivant. On utilise MVVM. On a les deux classes suivantes dans le model : public class Child { } public class Parent { private ObservableCollection < Child > _children; public ObservableCollection < Child > Children { get {...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [HS] CHROME 6 + MOI = COUP DE GUEULE ![HS] CHROME 6 + MOI = COUP DE GUEULE ! par JeremyJeanson
Attention, le poste qui suit n'est pas la complainte d'une personne : Qui n'aime pas Chrome. D'un anti Google. D'un développeur qui a un poil énorme dans la main. Ceux qui me fréquentent savent que je change de navigateur favori tous les 2 ou 3 mois afin ...
Cliquez pour lire la suite de l'article par JeremyJeanson [WP7] UTILISER UN WRAPPANEL DANS UNE APPLICATION WINDOWS PHONE 7[WP7] UTILISER UN WRAPPANEL DANS UNE APPLICATION WINDOWS PHONE 7 par Audrey
Lors de la réalisation de ma 2ème application Windows Phone 7, j'ai souhaité utiliser un WrapPanel pour afficher plusieurs photos. Mais le contrôle WrapPanel ne fait pas parti de la liste des contrôles inclus dans le SDK de la version Beta des outils pour...
Cliquez pour lire la suite de l'article par Audrey [WP7] BESOIN D'AVOIR DES DONNéES EN CACHE[WP7] BESOIN D'AVOIR DES DONNéES EN CACHE par Nicolas
Les développeurs ASP.NET ont l'habitude de mettre des données en cache pour éviter de requêter a chaque fois la base de données. Et il est toujours utilie de penser que vos utilisateurs mobiles n'ont pas troujours une super connexion 3G/WIFI et un for...
Cliquez pour lire la suite de l'article par Nicolas [TFS] COMMENT FORCER LA SAISIE D'UN AREA OU ITERATION[TFS] COMMENT FORCER LA SAISIE D'UN AREA OU ITERATION par cyril
Lorsque l'on créé un Work Item dans TFS, il est possible de le classer dans un "area" et dans une "iteration". Dans la plupart des types de projet, un "area" correspond à une catégorie, une "iteration" à un numéro de version. Il est possible de cré...
Cliquez pour lire la suite de l'article par cyril
Forum
RE : PYTHON 3.0RE : PYTHON 3.0 par aera group
Cliquez pour lire la suite par aera group RE : PYTHON 3.0RE : PYTHON 3.0 par xeolin
Cliquez pour lire la suite par xeolin RE : PYTHON 3.0RE : PYTHON 3.0 par aera group
Cliquez pour lire la suite par aera group
Logiciels
Bureau de Gestion - ERP Devis Facturation (2.02)BUREAU DE GESTION - ERP DEVIS FACTURATION (2.02)
- Version gratuite du 10/06/2010
Le Bureau de Gestion est un logiciel dédié à la gestion de l'en...
Cliquez pour télécharger Bureau de Gestion - ERP Devis Facturation sDEVIS-FACTURES vlPRO (3.8.0)SDEVIS-FACTURES VLPRO (3.8.0)sDEVIS-FACTURES vlPRO a été mis au point pour permettre besoins des particuliers, créateurs, entr... Cliquez pour télécharger sDEVIS-FACTURES vlPRO LettresFaciles (5.6.0)LETTRESFACILES (5.6.0)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles MyPlanning 2010 (5.6.0)MYPLANNING 2010 (5.6.0)MyPlanning 2010 permet de créer des plannings sous la représentation de diagrammes. Plannings pré... Cliquez pour télécharger MyPlanning 2010 Emicsoft Mac DVD en iPad Convertisseur (3.1.16)EMICSOFT MAC DVD EN IPAD CONVERTISSEUR (3.1.16)Emicsoft Mac DVD en iPad Convertisseur, logiciel professionnel de convertir les fichiers DVD en i... Cliquez pour télécharger Emicsoft Mac DVD en iPad Convertisseur
|