from random import randint from Tkinter import * from tkMessageBox import * from pygame import * import pygame from pygame.locals import * def jeu (): #Ecran de fond du jeu screen = pygame.display.set_mode((480,600)) couleurfond = 0xd0d0d0 fond = Surface((300,720)); fond.fill(couleurfond) key.set_repeat(50,50) #Variable qui continue la boucle si = 1, stoppe si = 0 continuer = 1 #Rafraîchissement de l'écran pygame.display.flip() #import de la musique pygame.mixer.init() son = pygame.mixer.Sound('son.wav') son.play(loops=-1, maxtime=0, fade_ms=0) #Fonction pour Pygame font.init() #Tableau lignes_vides = 2049 all_set = 4095 tableau = [lignes_vides]*(24)+[all_set] #Masque pour les cubes de couleur masque = image.load('masquetetris.png') rouge = Surface((300,30),SRCALPHA) rouge.fill((255,0,0,100)) #Plein ecran font0 = font.Font('FreeMonoBold.ttf',20) #Tableau de jeu objet = [ [[0, 0, 128, 224], [0, 192, 128, 128], [0, 0, 224, 32], [0, 64, 64, 192],(255,0,0)], [[0, 0, 64, 224], [0, 64, 96, 64], [0, 0, 224, 64], [0, 32, 96, 32],(100,100,0)], [[0, 0, 96, 192], [0, 64, 96, 32], [0, 0, 96, 192], [0, 64, 96, 32],(0,100,100)], [[0, 0, 192, 96], [0, 32, 96, 64], [0, 0, 192, 96], [0, 32, 96, 64],(0,0,255)], [[0, 0, 32, 224], [0, 64, 64, 96], [0, 0, 112, 64], [0, 96, 32, 32],(0,255,0)], [[0, 0, 96, 96], [0, 0, 96, 96], [0, 0, 96, 96], [0, 0, 96, 96],(100,255,100)], [[0, 0, 0, 240], [64, 64, 64, 64], [0, 0, 0, 240], [32, 32, 32, 32],(100,0,100)] ] Score = 0 Vitesse = 30 S = 0 #Accélère le jeu time.set_timer(KEYDOWN,Vitesse) #Affichage et calcul du score et de la vitesse du jeu screen.blit(font0.render('Score: '+str(Score),1,(200,200,200)),(330,300)) screen.blit(font0.render('Vitesse: '+str(0),1,(200,200,200)),(330,330)) k_up_is_up = False k_space_is_up = False test = lambda shift,down,rotation: not any([int(i*shift)&tableau[e] for e,i in enumerate(obj[(one+rotation)%4],ligne+down)]) def make_surf(index,one): img = Surface([120]*2,SRCALPHA) img.fill((0,0,0,0)) shadow = img.copy() color = objet[index][-1] for y,i in enumerate(objet[index][one]): i /= 16 for x in 90,60,30,0: if i&1: img.blit(masque,img.fill(color,(x,y*30,30,30))) shadow.fill(color+(50,)if shadow_set else couleurfond,(x,y*30,30,30)) i /= 2 return img,shadow def YShadowUpdate(): global ligne,Yshadow C = ligne while test(1,1,0): ligne+=1 Yshadow = (ligne-4)*30 ligne = C Yshadow = ligne = 1 shadow_set = True next_index = randint(0,6) next_one = randint(0,3) next_imgs = make_surf(next_index,next_one) while ligne: event.clear() Y = -120 X = 90 ligne = 0 index = next_index one = next_one img,shadow = next_imgs obj = objet[index] next_index = randint(0,6) next_one = randint(0,3) next_imgs = make_surf(next_index,next_one) screen.blit(transform.scale(next_imgs[0],(60,60)),screen.fill(0,(30*12,60,60,60))) while True: ev = event.wait() if ev.type == KEYDOWN: if not ev.key or ev.key== K_DOWN: if not Y%30: if test(1,1,0): ligne+=1 else: fond.blit(img,(X,ligne*30)) s = 0 for e,i in enumerate(obj[one],ligne): tableau[e] = tableau[e]|i if tableau[e] == all_set: S += 1 if not S%10: Vitesse -= 1; time.set_timer(KEYDOWN,Vitesse) s += 1 Score += s**2 del(tableau[e]); tableau.insert(0,lignes_vides) display.update(screen.blit(rouge,(0,(e-4)*30))) time.wait(300) fond.blit(fond,(0,30),(0,0,300,(e)*30)) fond.fill(couleurfond,(0,0,30*10,30)) screen.fill(0,(330,300,120,60)) screen.blit(font0.render('Score: '+str(Score),1,(200,200,200)),(330,300)) screen.blit(font0.render('Vitesse: '+str(30-Vitesse),1,(200,200,200)),(330,330)) screen.blit(fond,(0,0),(0,120,300,(e)*30)) display.flip() break if not ev.key: Y += 3 else: Y = (ligne-4)*30 screen.blit(fond,(0,-120)) screen.blit(shadow,(X,Yshadow)) screen.blit(img,(X,Y)) display.flip(); continue elif ev.key == K_LEFT: if test(2,0,0): obj = [[j<<1 for j in i]for i in obj]; X -= 30; YShadowUpdate(); continue elif ev.key == K_RIGHT: if test(0.5,0,0): obj = [[j>>1 for j in i]for i in obj]; X += 30; YShadowUpdate(); continue elif ev.key == K_UP and not k_up_is_up: k_up_is_up = True if test(1,0,1): one = (one+1)%4; img,shadow = make_surf(index,one); YShadowUpdate(); continue elif ev.key == K_SPACE and not k_space_is_up: display.update((screen.blit(fond,(X,Y),(X,Y,120,120)),screen.blit(img,(X,Yshadow)))) Y = Yshadow; ligne = Yshadow/30+4; k_space_is_up = True; continue elif ev.type == KEYUP: if ev.key == K_UP: k_up_is_up = False; continue elif ev.key == K_SPACE: k_space_is_up = False; continue elif ev.key == K_p: while event.wait().type != KEYUP: pass continue elif ev.key == K_s: shadow_set = not shadow_set img,shadow = make_surf(index,one) continue elif ev.type == QUIT: exit() while event.wait().type != QUIT: pass try: pygame.init() #Menu du jeu #Fenetre 1 fen1 = Tk () fen1.title ("TETRIS") #Image Menu can1=Canvas(fen1, width=400, height=500) photo=PhotoImage(file='tet.gif') item=can1.create_image(200, 200,image = photo) #Nom Menu can1.pack() text1 = Label (fen1, text = 'Tetris', fg= 'red' ) text1.pack () #Boutton Jouer bou1 = Button (fen1, text= 'Jouer', height=10, width=36, bg= 'dark cyan', command =jeu) bou1.pack(side=LEFT) #Boutton Quitter bou2 = Button (fen1, text= 'Quitter', height=10, width=36, bg= 'dark red', command = fen1.destroy) bou2.pack(side=RIGHT) bou2.pack () fen1.mainloop() finally: pygame.quit() exit()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question