Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
PONG : DÉVELOPPEMENT
Information sur la source
Description
L'idée est de faire un pong assez basique, avec une interface GUI sommaire. Le développement pourra ensuite porter sur l'amélioration du jeux : - choix des options (difficultés, niveau de l'IA...) - possibilité de jouer à 2 joueurs sur le meme PC ou en réseau - sauvegarde des meilleurs scores - améliorations graphiques ...
Source
- #!/usr/bin/python
- # -*- coding: UTF-8 -*-
-
- from Tkinter import *
- from random import randint
-
- class Fenetre(Tk):
- def __init__(self, width=600, height=400):
- Tk.__init__(self)
- self.flag = 0
-
- self.quit= Button(self,text="Quit Game",command=self.destroy)
- self.quit.grid(column=4,row=0,sticky="NE")
-
- self.new= Button(self,text="New Game",command=self.new_game)
- self.new.grid(column=0,row=0,sticky="NW")
-
-
- self.can = Canvas(self,width=width,height=height,bg="black")
- self.can.grid(column=0,row=1,sticky="SW",columnspan=5)
-
- def new_game(self):
- if self.flag == 0 :
- self.flag=1
- self.pads = Pad(self.can,self.flag)
- self.ball = Ball(self.can,self.pads,self.flag)
-
- class Pad:
- def __init__(self,canvas,flag):
- self.canvas = canvas
- self.flag = flag
- self.height = canvas.winfo_height()
- self.width = canvas.winfo_width()
- self.x1,self.y1 = 10,self.height/2-30
- self.x2,self.y2 = self.width-25,self.height/2-30
-
- self.Pad1 = canvas.create_rectangle(self.x1,self.y1,self.x1+15,self.y1+60,fill="white")
- self.Pad2 = canvas.create_rectangle(self.x2,self.y2,self.x2+15,self.y2+60,fill="white")
-
- canvas.bind_all("<Up>",self.mouv_up)
- canvas.bind_all("<Down>", self.mouv_down)
- self.dy2 = 25
- self.ia()
-
- def mouv_up(self,event):
- if self.y1>5 :
- self.y1=self.y1-10
- self.canvas.coords(self.Pad1,self.x1,self.y1,self.x1+15,self.y1+60)
-
- def mouv_down(self,event):
- if self.y1+60<(self.height-5):
- self.y1=self.y1+10
- self.canvas.coords(self.Pad1,self.x1,self.y1,self.x1+15,self.y1+60)
-
- def ia (self):
- self.y2=self.y2 + self.dy2
- if self.y2+60 > self.height-10 :
- self.dy2=-50
-
- if self.y2 < 5 :
- self.dy2=50
-
- self.canvas.coords(self.Pad2,self.x2,self.y2,self.x2+15,self.y2+60)
- if self.flag > 0:
- self.canvas.after(70,self.ia)
-
-
- class Ball:
- def __init__(self,canvas,pad,flag):
- self.canvas = canvas
- self.pad = pad
- self.height = canvas.winfo_height()
- self.width = canvas.winfo_width()
- self.flag = flag
- self.x1,self.y1 = self.width/2,self.height/2
- self.dx,self.dy = 30,30
- self.Ball = canvas.create_oval(self.x1, self.y1, self.x1+25, self.y1+25, width=2, fill='white')
- self.pointA, self.pointB = 0,0
-
- self.ready()
-
- def ready(self):
- self.starter=0
- self.score= Label(app,text="%d : %d" % (self.pointA,self.pointB), bg="black",fg="white")
- self.score.grid(column =2,row=0,sticky="S")
- self.x1,self.y1 = self.height/2,self.width/2
- app.titre = Label(app,text="PRESS ANY KEY TO START", bg="black",fg="white")
- app.titre.grid(column =2,row=2,sticky="S")
- self.canvas.bind_all("<Key>",self.start)
-
-
- def start(self,event):
- self.starter=1
- self.move()
-
- def move(self):
- if self.starter==1:
- self.x1, self.y1 = self.x1 +self.dx, self.y1 + self.dy
-
- if self.y1 >self.height-26:
- self.dx, self.dy = self.dx, -20
-
- if self.y1 <2:
- self.dx, self.dy = self.dx, 20
-
- if self.x1 < self.pad.x1+20:
- if self.pad.y1 < self.y1 < self.pad.y1+60:
- self.dx, self.dy = 20, self.dy
-
- if self.x1+30 > self.pad.x2-1:
- if self.pad.y2<self.y1+12.5<self.pad.y2+60:
- self.dx, self.dy = -20, self.dy
-
- if self.x1 < 0:
- self.starter = 0
- self.pointB = self.pointB+1
- self.ready()
-
-
- if self.x1+25 > self.width:
- self.starter=0
- self.pointA = self.pointA+1
- self.ready()
-
- self.canvas.coords(self.Ball,self.x1,self.y1,self.x1+30,self.y1+30)
- if self.flag > 0:
- self.canvas.after(70,self.move)
-
-
-
-
- if __name__ == "__main__":
- app = Fenetre()
- app.mainloop()
-
-
-
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from Tkinter import *
from random import randint
class Fenetre(Tk):
def __init__(self, width=600, height=400):
Tk.__init__(self)
self.flag = 0
self.quit= Button(self,text="Quit Game",command=self.destroy)
self.quit.grid(column=4,row=0,sticky="NE")
self.new= Button(self,text="New Game",command=self.new_game)
self.new.grid(column=0,row=0,sticky="NW")
self.can = Canvas(self,width=width,height=height,bg="black")
self.can.grid(column=0,row=1,sticky="SW",columnspan=5)
def new_game(self):
if self.flag == 0 :
self.flag=1
self.pads = Pad(self.can,self.flag)
self.ball = Ball(self.can,self.pads,self.flag)
class Pad:
def __init__(self,canvas,flag):
self.canvas = canvas
self.flag = flag
self.height = canvas.winfo_height()
self.width = canvas.winfo_width()
self.x1,self.y1 = 10,self.height/2-30
self.x2,self.y2 = self.width-25,self.height/2-30
self.Pad1 = canvas.create_rectangle(self.x1,self.y1,self.x1+15,self.y1+60,fill="white")
self.Pad2 = canvas.create_rectangle(self.x2,self.y2,self.x2+15,self.y2+60,fill="white")
canvas.bind_all("<Up>",self.mouv_up)
canvas.bind_all("<Down>", self.mouv_down)
self.dy2 = 25
self.ia()
def mouv_up(self,event):
if self.y1>5 :
self.y1=self.y1-10
self.canvas.coords(self.Pad1,self.x1,self.y1,self.x1+15,self.y1+60)
def mouv_down(self,event):
if self.y1+60<(self.height-5):
self.y1=self.y1+10
self.canvas.coords(self.Pad1,self.x1,self.y1,self.x1+15,self.y1+60)
def ia (self):
self.y2=self.y2 + self.dy2
if self.y2+60 > self.height-10 :
self.dy2=-50
if self.y2 < 5 :
self.dy2=50
self.canvas.coords(self.Pad2,self.x2,self.y2,self.x2+15,self.y2+60)
if self.flag > 0:
self.canvas.after(70,self.ia)
class Ball:
def __init__(self,canvas,pad,flag):
self.canvas = canvas
self.pad = pad
self.height = canvas.winfo_height()
self.width = canvas.winfo_width()
self.flag = flag
self.x1,self.y1 = self.width/2,self.height/2
self.dx,self.dy = 30,30
self.Ball = canvas.create_oval(self.x1, self.y1, self.x1+25, self.y1+25, width=2, fill='white')
self.pointA, self.pointB = 0,0
self.ready()
def ready(self):
self.starter=0
self.score= Label(app,text="%d : %d" % (self.pointA,self.pointB), bg="black",fg="white")
self.score.grid(column =2,row=0,sticky="S")
self.x1,self.y1 = self.height/2,self.width/2
app.titre = Label(app,text="PRESS ANY KEY TO START", bg="black",fg="white")
app.titre.grid(column =2,row=2,sticky="S")
self.canvas.bind_all("<Key>",self.start)
def start(self,event):
self.starter=1
self.move()
def move(self):
if self.starter==1:
self.x1, self.y1 = self.x1 +self.dx, self.y1 + self.dy
if self.y1 >self.height-26:
self.dx, self.dy = self.dx, -20
if self.y1 <2:
self.dx, self.dy = self.dx, 20
if self.x1 < self.pad.x1+20:
if self.pad.y1 < self.y1 < self.pad.y1+60:
self.dx, self.dy = 20, self.dy
if self.x1+30 > self.pad.x2-1:
if self.pad.y2<self.y1+12.5<self.pad.y2+60:
self.dx, self.dy = -20, self.dy
if self.x1 < 0:
self.starter = 0
self.pointB = self.pointB+1
self.ready()
if self.x1+25 > self.width:
self.starter=0
self.pointA = self.pointA+1
self.ready()
self.canvas.coords(self.Ball,self.x1,self.y1,self.x1+30,self.y1+30)
if self.flag > 0:
self.canvas.after(70,self.move)
if __name__ == "__main__":
app = Fenetre()
app.mainloop()
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Historique
- 06 juillet 2007 15:35:18 :
- v0.1
- 10 juillet 2007 14:45:54 :
- v0.2
- 10 juillet 2007 17:42:40 :
- v0.3
- 10 juillet 2007 17:43:23 :
- v0.4
- 11 juillet 2007 09:28:53 :
- v0.4
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | | | |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|