Advertisement
here2share

# Tk_Tetris.py

Oct 24th, 2020 (edited)
2,728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. # Tk_Tetris.py
  2.  
  3. import random,time
  4. from Tkinter import *
  5. t=time.time()
  6. score,bw,bh,H,W,speed=0,10,20,30,30,300
  7. blk={0xf:"red",0x2e:"#0f0",0x27:"blue",0x47:"#ff0",0x66:"#0ff",0xC6:"#38f",0x6C:"#f0f"}
  8. board=[ [0xf if j==bh else 0]*bw+[0xf]*2 for j in range(bh+2+1) ]
  9. new_piece=lambda pc: ( [((z>>2)+1,z&3) for z in range(16) if (pc>>z)&1],3,-2,pc )
  10. collide=lambda piece,px,py: [1 for (i,j) in piece if board[j+py][i+px]]
  11. BALANCE=blk.keys()*5
  12. bal=BALANCE[:]
  13. random.shuffle(bal)
  14. piece,px,py,pc=new_piece(bal.pop())
  15. def Score():
  16.         scr.create_text(12,18,text="SCORE: %i"%(score),font=('Verdana 18 bold'),fill='white',anchor='w')
  17.         scr.create_text(12,36,text="SPEED: %i"%(int(300-speed)/6+1),font=('Verdana 10'),fill='white',anchor='w')
  18. def tick(e=None):
  19.     global piece,px,py,pc,tickcnt,board,score,speed,bal
  20.     if not speed: return
  21.  
  22.     keys=e.keysym if e else "" # get key event
  23.  
  24.     npx=px+(-1 if keys=="Left" else (1 if keys=="Right" else 0))
  25.     npiece=[(j,3-i) for (i,j) in piece] if keys=="Up" else piece     #rotate
  26.     if not collide(npiece,npx,py): piece,px=npiece,npx
  27.     if keys=="Down": py=(j for j in range(py,bh) if collide(piece,px,j+1)).next()
  28.  
  29.     if e==None:
  30.         if collide(piece,px,py+1):
  31.             if py<0: # game over
  32.                 h=400
  33.                 scr.create_rectangle(0,0,W*10,50,fill='darkred')
  34.                 scr.create_rectangle(0,h-30,W*10,h+30,fill='purple')
  35.                 scr.create_text(12,h,text="GAME OVER",font=('Verdana 34 italic'),fill='white',anchor='w')
  36.                 Score()
  37.                 speed=0
  38.                 return
  39.             for i,j in piece: board[j+py][i+px]=pc
  40.             if not bal:
  41.                 bal=BALANCE[:]
  42.                 random.shuffle(bal)
  43.             piece,px,py,pc=new_piece(bal.pop())
  44.             score+=1
  45.         else: py+=1
  46.  
  47.         nb=[l for l in board[:bh] if 0 in l]+board[bh:]
  48.         s=len(board)-len(nb)
  49.         if s: score,board=score+((2**s)*50),[board[-1][:] for j in range(s)]+nb
  50.         ttt=(time.time()-t)/30
  51.         speed = max(50,speed-0.25)
  52.         scr.after(int(speed-ttt),tick)
  53.  
  54.     scr.delete("all")
  55.     for i,j,c in [(i,j,blk.get(board[j][i],"#000")) for i in range(bw) for j in range(bh)]:
  56.         scr.create_rectangle(i*W,j*H,(i+1)*W,(j+1)*H,fill=blk[pc] if (i-px,j-py) in piece else c)
  57.     Score()
  58.        
  59. root=Tk()
  60. root.geometry("{}x{}+10+10".format(bw*W,bh*H))
  61. scr=Canvas(width=bw*W,height=bh*H,bg="#000")
  62. scr.after(speed,tick)
  63. scr.bind_all("<Key>",tick)
  64. scr.pack()
  65. scr.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement