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
|
Derniers Blogs
[RIA SERVICES] MAITRE - DéTAIL ET DOMAINDATASOURCE[RIA SERVICES] MAITRE - DéTAIL ET DOMAINDATASOURCE par Audrey
A l'occasion d'un projet client, j'ai utilisé RIA Services avec Silverlight 3 (mais cela fonctionne aussi avec la version 4), et je l'ai utilisé pour une interface façon Maitre / Détail. Voici comment j'ai procédé pour arriver à mes fins. Nous allons pren...
Cliquez pour lire la suite de l'article par Audrey CSDL FUNCTIONCSDL FUNCTION par Matthieu MEZIL
Dans mon post précédent , j'ai utilisé une CSDL Function afin de générer une requête SQL avec un DateDiff utilisant la date courante sur la BD à partir d'une requête LINQ. Dans le cadre de ce post , vous avez probablement remarqué que dans le cadre de plu...
Cliquez pour lire la suite de l'article par Matthieu MEZIL LINQ TO ENTITIESLINQ TO ENTITIES par Matthieu MEZIL
Cette semaine je suis à Montréal en tant que speaker sur Entity Framework pour l'évènement confoo . J'en profite pour remercier les organisateurs de cet évènement de m'avoir fait confiance et Access-IT de m'avoir permis d'y participer. En parallèle, j'ai ...
Cliquez pour lire la suite de l'article par Matthieu MEZIL FAIRE APPARAITRE L'ONGLET 'DéVELOPPEUR' DANS OFFICE 2010FAIRE APPARAITRE L'ONGLET 'DéVELOPPEUR' DANS OFFICE 2010 par neodante
La nouvelle interface d'Office 2010 à amener quelques modifications par rapport à celle de 2007. Certes mineures, ces modifications ont fait disparaître la case à cocher de l'onglet 'Développeur' en première page du panneau du 'bouton Office' (dans Office...
Cliquez pour lire la suite de l'article par neodante [ASTUCE] PATCH POUR MICROSOFT FORUMS NNTP BRIDGE V1[ASTUCE] PATCH POUR MICROSOFT FORUMS NNTP BRIDGE V1 par pierre
Si vous avez téléchargé comme moi Microsoft Forums NNTP Bridge V1 avant le 11 mars 2010 (voir [Astuce] Disponibilité de Microsoft Forum NNTP Bridge Version 1.0), un problème de date localisée pour les non anglais était présent. Un patch est disponibl...
Cliquez pour lire la suite de l'article par pierre
Logiciels
Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor
|