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
TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice CRéATION D'UNE BASE DE DONNéE SOUS SQL AZURECRéATION D'UNE BASE DE DONNéE SOUS SQL AZURE par junarnoalg
Sans rentrer dans les détails, je me propose ici de faire un rapide tour de ce que propose SQL Azure.
SQL Azure est avant tout un service d'hébergement de base de données relationnelles construit sur SQL Server. Il permet aux entreprises d...
Cliquez pour lire la suite de l'article par junarnoalg TECHDAYS PARIS 2010 : LES SERVICES D'APPLICATIONS DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LES SERVICES D'APPLICATIONS DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Xavier Moreels et Julien Bakmezdjian Ce sujet est lié au partage des applications comme services dans SharePoint 2010, ceci représente la possibilité de créer sa propre application qui sera utilisable comme ceux en standard : Search...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|