salut
j'ai essayé de coder un tetris qui marche pas trop mal pour le moment, c'est pas fini hein ...(excusez le manque de lisibilité du code, j'ai attaqué python y a 5 jours, chui encore un peu brouillon).
Seulement, au bout d'un moment, il y a un fort ralentissement du jeu, j'ai trituré le code dans tous les sens en essayant d'affiner les boucles et les tests mais je n'arrive pas a un resultat satisfaisant.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
############################################################################################################################################
#								VARIABLES
############################################################################################################################################
#gen. variables
x_cur=3
y_cur=-4
laps=0.5
#tetris variables
tetris_num=0
tetris_rot=0
width_one=36.0
tetris=[[((0,2),(0,3),(1,2),(1,3)),((0,2),(0,3),(1,2),(1,3)),((0,2),(0,3),(1,2),(1,3)),((0,2),(0,3),(1,2),(1,3))],[((0,2),(0,3),(1,3),(2,3)),((0,1),(0,2),(0,3),(1,1)),((0,2),(1,2),(2,2),(2,3)),((0,3),(1,1),(1,2),(1,3))],[((0,3),(1,2),(1,3),(2,2)),((0,1),(0,2),(1,2),(1,3)),((0,3),(1,2),(1,3),(2,2)),((0,1),(0,2),(1,2),(1,3))],[((0,3),(1,3),(2,3),(3,3)),((0,1),(0,2),(0,3),(0,4)),((0,3),(1,3),(2,3),(3,3)),((0,1),(0,2),(0,3),(0,4))],[((0,2),(1,2),(1,3),(2,3)),((0,2),(0,3),(1,1),(1,2)),((0,2),(1,2),(1,3),(2,3)),((0,2),(0,3),(1,1),(1,2))],[((0,2),(1,2),(1,3),(2,2)),((0,2),(1,1),(1,2),(1,3)),((0,3),(1,2),(1,3),(2,3)),((0,1),(0,2),(0,3),(1,2))],[((0,3),(1,3),(2,2),(2,3)),((1,1),(1,2),(1,3),(2,3)),((0,2),(0,3),(1,2),(2,2)),((1,1),(2,1),(2,2),(2,3))]]
#arena variables
num_line=20
num_col=10
pix_width=width_one*num_col+2
pix_height=width_one*num_line+2
arena=[-1]*(num_line+4)
arena_end=[-1]*(num_col+2)
#colors variables
tetris_col=5
aff_color=tetris_col
bg_color="black"
color=[("orange red","red","dark red"),("light blue","blue","dark blue"),("pale green","light green","dark green"),("light grey","grey","dark grey"),("light yellow","yellow","yellow green"),("PeachPuff1","PeachPuff2","PeachPuff3"),("chocolate1","chocolate2","chocolate3"),(bg_color,bg_color,bg_color)]
#file d'attente
Q=[]
 
############################################################################################################################################
#								DEF
############################################################################################################################################
#affiche/efface un tetris
def draw_tetris(action):
	if action=='trace':
		couleur=aff_color
	if action=='efface':
		couleur=7
	for offset_x, offset_y in tetris[tetris_num][tetris_rot]:
			f=0
			x1=(x_cur+offset_x)*width_one+1
			y1=(y_cur+offset_y)*width_one+(width_one/2)+1
			y2=(y_cur+offset_y)*width_one+width_one
			col1,col2,col3 = color[couleur]
			tab.create_line(x1,y1,x1+width_one,y1,width=width_one,fill=col1)
			while f < width_one :
				tab.create_line(x1+f,y2-f,x1+width_one,y2-f,width=1,fill=col3)
				f=f+1
			tab.create_line(x1+width_one/4,y1,x1+width_one-width_one/4,y1,width=width_one/2,fill=col2)
	return
 
#teste la possibilite d'afficher le tetris
def test(x_test,y_test,tetris_num_test,tetris_rot_test):
	aaa=""
	for offset_x, offset_y in tetris[tetris_num_test][tetris_rot_test]:
		x1=(x_test+offset_x)
		y1=(y_test+offset_y)
		if arena[y1+4][x1+1] < 7:
			return "impossible"
	return "possible"
 
#pose le tetris dans arena
def put_tetris():
	s=0
	for offset_x, offset_y in tetris[tetris_num][tetris_rot]:
		s=s+1		
		x1=(x_cur+offset_x)
		y1=(y_cur+offset_y)
		arena[y1+4][x1+1]=tetris_col
	return
#deplace le tetris
def move(new_x,new_y,new_tetris,new_rot):
	global x_cur, y_cur, num_tetris, num_rot
	draw_tetris('efface')
	x_cur,y_cur,num_tetris,num_rot=new_x,new_y,new_tetris,new_rot
	draw_tetris('trace')
	return
#vers le bas
def down_move():
	global Q
	if test(x_cur,y_cur+1,tetris_num,tetris_rot)=="possible":
		move(x_cur,y_cur+1,tetris_num,tetris_rot)
		del(Q[0])
	elif test(x_cur,y_cur+1,tetris_num,tetris_rot)=="impossible":
		put_tetris()
		Q=["next"]
	return
#vers la gauche
def left_move():
	if test(x_cur-1,y_cur,tetris_num,tetris_rot)=="possible":
		move(x_cur-1,y_cur,tetris_num,tetris_rot)
	del(Q[0])
 
	return
#vers la droite
def right_move():
	if test(x_cur+1,y_cur,tetris_num,tetris_rot)=="possible":
		move(x_cur+1,y_cur,tetris_num,tetris_rot)
	del(Q[0])
	return
#rotation
def rotate_up():
	global tetris_rot
	if test(x_cur,y_cur,tetris_num,(tetris_rot+1)%4)=="possible":
		draw_tetris("efface")		
		tetris_rot=(tetris_rot+1)%4	
		draw_tetris("trace")		
	del(Q[0])
	return
#rotation
def rotate_down():
	global tetris_rot
	if test(x_cur,y_cur,tetris_num,(tetris_rot-1)%4)=="possible":
		draw_tetris("efface")		
		tetris_rot=(tetris_rot-1)%4	
		draw_tetris("trace")		
	del(Q[0])
	return
#gestion des touches clavier
def Q_left(key):
	if len(Q) > 1:
		Q.insert(1,"left")
	else:
		Q.append("left")
	return	
 
def Q_right(key):
	if len(Q) > 1:
		Q.insert(1,"right")
	else:
		Q.append("right")
	return
 
def Q_up(key):
	if len(Q) > 1:
		Q.insert(1,"r_up")
	else:
		Q.append("r_up")
	return	
def Q_down(key):
	if len(Q) > 1:
		Q.insert(1,"r_down")
	else:
		Q.append("r_down")
	return
def Q_space(key):
	if len(Q) > 1:
		Q.insert(1,"down")
	else:
		Q.append("down")
	return
#temporisation a l'arrache
def timing():
	global w
	while w==1:
		a=len(Q)
		if a==0:
			Q.append("down")
		elif Q[a-1]=="down":
			pass
		else:
			Q.append("down")
		sleep(laps)	
	return
#redessine le tableau (pas fini)
def update_arena():
	x=0
	while x < num_line+4:
		line="complete"
		for m in arena[x]:
			if m > 6 :
				line="incomplete"				
		if line=="complete":
			del(arena[x])
			arena.insert(0,[-1]+[7]*num_col+[-1])
		x=x+1
	retrace_arena()
	return
 
def retrace_arena():
	xx=0
	while xx < num_line+4:
		yy=1
		while yy < num_col+1:
			ra_color=arena[xx][yy]
			if ra_color > 7:
				ra_color = ra_color-8
			trace_one(yy-1,xx-4,ra_color)
			yy=yy+1
		xx=xx+1
	return
#dessine un carre
def trace_one(x,y,couleur):
	f=0
	x1=x*width_one+1
	y1=y*width_one+(width_one/2)+1
	y2=y*width_one+width_one
	col1,col2,col3 = color[couleur]
	tab.create_line(x1,y1,x1+width_one,y1,width=width_one,fill=col1)
	while f < width_one :
		tab.create_line(x1+f,y2-f,x1+width_one,y2-f,width=1,fill=col3)
		f=f+1
	tab.create_line(x1+width_one/4,y1,x1+width_one-width_one/4,y1,width=width_one/2,fill=col2)
	return
############################################################################################################################################
#								CORPS
############################################################################################################################################
#initialize arena
count=num_line+3
while count >= 0:
	arena[count]=[-1]+[7]*num_col+[-1]
	count=count-1
arena.append(arena_end)
 
#initialize tab
from Tkinter import *
from threading import *
from time import *
from random import *
tab=Canvas(bg=bg_color,height=pix_height,width=pix_width)
tab.pack()
w=1
tab.bind_all('<Left>',Q_left)
tab.bind_all('<Right>',Q_right)
tab.bind_all('<Up>',Q_up)
tab.bind_all('<Down>',Q_down)
tab.bind_all('<space>',Q_space)
tab.update()
k=Thread(None,timing,None)
k.start()
h=0
#test
while test(3,-4,tetris_num,tetris_rot)=="possible":
	h=h+1	
	Q=[]
	tetris_num=randrange(7)
	aff_color=randrange(7)
	fantome=randrange(10)
	if fantome==0:#enlever le commentaire denant +8 pour activer les tetris fantomes
		tetris_col=aff_color#+8
	else:
		tetris_col=aff_color
	tetris_rot=randrange(4)	
	x_cur,y_cur=3,-4
	while len(Q)==0:
		pass
	while Q[0] != "next":
		if Q[0]=="null":
			del(Q[0])
		elif Q[0]=="down":
			down_move()
		elif Q[0]=="left":
			left_move()
		elif Q[0]=="right":
			right_move()
		elif Q[0]=="r_up":
			rotate_up()
		elif Q[0]=="r_down":
			rotate_down()
		print Q,h
		tab.update()
		if len(Q)==0 :
			Q.append("null")
	update_arena()
w=0
Je me damande si le thread "timing" n'est pas en cause car j'ai pondu ça comme un barbare.