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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
from tkinter import *
from math import *
from random import *
def serpent():
global S,apple,i
destruct()
S.append([x,y])
can.create_oval(S[len(S)-1][0],S[len(S)-1][1],S[len(S)-1][0],S[len(S)-1][1],width=10,outline='violet')
#can.create_oval(x-r,y-r,x+r,y+r,outline='yellow',width=...)
def destruct():
global apple2
if apple2!=0:
can.create_oval(S[0][0],S[0][1],S[0][0],S[0][1],width=10,outline='green')
S.pop(0)
apple2=1
def dirnb1(event):
global dir2
if dir2!=1:
dir2=-1
avancer()
def dirnb2(event):
global dir2
if dir2!=-1:
dir2=1
avancer()
def dirnb3(event):
global dir2
if dir2!=2:
dir2=-2
avancer()
def dirnb4(event):
global dir2
if dir2!=-2:
dir2=2
avancer()
def tete():
global x,y
if dir2==1:
x=x+10
if dir2==-1:
x=x-10
if dir2==2:
y=y+10
if dir2==-2:
y=y-10
def pomme():
global a,b,apple,apple2
if 'a' in globals() and 'b' in globals():
can.create_oval(a,b,a,b,width=10,outline='green')
a=randint(5,495)
b=randint(5,495)
can.create_oval(a,b,a,b,width=10,outline='red')
apple=1
apple2=0
def avancer():
global apple
if apple==5:
pomme()
#La tête avance
tete()
#Le corps avance
serpent()
print('x=',x,'y=',y)
#test de la pomme mangée ou pas
if x>=a-5 and x<=a+5 and y>=b-5 and y<=b+5:
apple=0
if apple==0:
pomme()
#========Programme Principal===========
#Initialisation
#dir: 1: ==> -1:<== 2:vers le haut -2:vers le bas
dir2=int(1)
apple=int(5)
#x,y coordonnées de la tête du serpent
x,y=int(540),int(400)
fen=Tk()
can=Canvas(fen,bg='green',height=600,width=700)
can.pack()
#création de barrières
can.create_line(10,10,1190,10,width=10,fill='white')
can.create_line(10,10,10,790,width=10,fill='white')
can.create_line(10,790,1190,790,width=10,fill='white')
can.create_line(1190,10,1190,790,width=10,fill='white')
#S: liste des coordonnées de points qui constituent le serpent
S=[[500,400],[510,400],[520,400],[530,400],[540,400]]
oi=can.create_oval(S[0][0],S[0][1],S[0][0],S[0][1],width=10,outline='light yellow')
for h in range(1,len(S)):
oi=can.create_oval(S[h][0],S[h][1],S[h][0],S[h][1],width=10,outline='yellow')
#Réceptionneur d'evts
touche=Entry(fen)
touche.pack(side=LEFT)
touche.bind('<Left>', dirnb1)
touche.bind('<Right>', dirnb2)
touche.bind('<Up>', dirnb3)
touche.bind('<Down>', dirnb4) |
Partager