俄罗斯方块Python
生活随笔
收集整理的這篇文章主要介紹了
俄罗斯方块Python
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# -*- coding: utf-8 -*-from tkinter import *
import random
#尚需改進,有待提高#各種方塊的表示,與參考方塊的相對位置
shapedic={1:((0,0),(1,0),(0,-1),(1,-1)),#正方形2:((0,0),(0,-1),(0,-2),(0,1)),#長條3:((0,0),(0,-1),(1,0),(1,1)),#之字型4:((0,0),(0,-1),(-1,0),(-1,1)),#反之字型5:((0,0),(1,0),(-1,0),(-1,-1)),#L型6:((0,0),(1,0),(-1,0),(1,-1)),#反L型7:((0,0),(1,0),(-1,0),(0,-1))#T型}#旋轉函數,順時針旋轉90度,相對于參考方塊
change_dic={(0,0):(0,0),(0,1):(-1,0),(-1,0):(0,-1),(0,-1):(1,0),(1,0):(0,1),(1,-1):(1,1),(1,1):(-1,1),(-1,1):(-1,-1),(-1,-1):(1,-1),(2,0):(0,2),(0,2):(-2,0),(-2,0):(0,-2),(0,-2):(2,0)}#隨機顏色
colorDict = {0 :'#CCC0B4',1 :'#EEE4DA',2 : '#EDE0C8',3 : '#F2B179',4 : '#EC8D54',5 : '#F67C5F', 6 : '#EA5937',7 : '#804000', 8 : '#F1D04B',9 : '#E4C02A',10 : '#EE7600',11 : '#D5A500',12 : '#E4C02A',13 : '#804000',14 : '#EA5937',15 : '#EE7600',16 : '#776E65',17 : '#776E65',18 : '#FFFFFF',19 : 'yellow',20 : 'blue',21 : 'lightblue',22 : 'red'}#俄羅斯方塊
class Game_Russia:def __init__(self):#每個方塊的大小self.width=20#方塊數目,長和寬self.row=28self.column=19# #初始化
# self.scores=0
# self.all_square={}#坐標系網格中個位置方塊的存在性
# self.head_square=[]#參考方塊絕對位置
# self.new_square=[]#移動方塊相對位置
# self.direction=-1#方塊初始方向
# #規定界限
# #i表示第i列,0在左邊
# #j表示第j行,零在上面
# for j in range(-4,self.row):
# for i in range(self.column):
# self.all_square[i,j]=0
# #劃界,開口向上
# for j in range(self.row+1):
# self.all_square[19,j]=1
# self.all_square[-1,j]=1
# for i in range(-1,self.column+1):
# self.all_square[i,28]=1"""用來debugfor j in range(self.row+1):for i in range(-1,self.column+1):print self.all_square[i,j],print"""self.window = Tk()self.window.geometry()self.window.maxsize(400,610)self.window.minsize(400,610)self.window.title(u"俄羅斯方塊")self.frame1=Frame(self.window,bg="white",relief=GROOVE,borderwidth=5)self.frame2=Frame(self.window,bg="white",relief=RAISED,borderwidth=2,height=40,width=570)self.canvas=Canvas(self.frame1,bg='purple',width=400,height=570)self.score_label=Label(self.frame2,text="Score: 0")self.frame1.pack()self.frame2.pack(fill=BOTH)self.score_label.pack(side=LEFT)self.canvas.pack(fill=BOTH)self.draw_wall()self.initial()self.get_new_square()self.draw_new_square()self.play()self.window.mainloop()"=== View Part ==="#邊界def draw_wall(self):self.canvas.create_line(5,5,385,5,fill='blue',width=1)self.canvas.create_line(385,5,385,565,fill='blue',width=1)self.canvas.create_line(5,5,5,565,fill='blue',width=1)self.canvas.create_line(5,565,385,565,fill='blue',width=1)#得分def draw_score(self):self.get_score()self.score_label.config(self.score_label,text="Score: "+str(self.scores))#畫下面所有不動的方塊def draw_square(self):color=colorDict[random.randint(0,len(colorDict)-1)]for j in range(self.row):self.canvas.delete("line"+str(j))for i in range(self.column):if self.all_square[i,j]:self.canvas.create_rectangle(5+i*self.width,5+j*self.width,5+(i+1)*self.width,5+(j+1)*self.width,fill=color,tags="line"+str(j))#畫移動的方塊def draw_new_square(self):self.canvas.delete("new")self.head_square[1]+=1color=colorDict[random.randint(0,len(colorDict)-1)]for i in range(4):self.canvas.create_rectangle(5+(self.head_square[0]+self.new_square[i][0])*self.width,5+(self.head_square[1]+self.new_square[i][1])*self.width,5+(self.head_square[0]+self.new_square[i][0]+1)*self.width,5+(self.head_square[1]+1+self.new_square[i][1])*self.width,fill=color,tags="new")"=== Model Part ==="def initial(self):#初始化self.scores=0self.all_square={}#坐標系網格中個位置方塊的存在性self.head_square=[]#參考方塊絕對位置self.new_square=[]#移動方塊相對位置self.direction=-1#方塊初始方向#規定界限#i表示第i列,0在左邊#j表示第j行,零在上面for j in range(-4,self.row):for i in range(self.column):self.all_square[i,j]=0#劃界,開口向上for j in range(self.row+1):self.all_square[19,j]=1self.all_square[-1,j]=1for i in range(-1,self.column+1):self.all_square[i,28]=1def is_dead(self):#判斷死亡與否,最上方中間四個方塊for i in {8,9,10,11}:if self.all_square[i,0]:return Trueelse:return Falsedef get_new_square(self):#獲得新的方塊,初始位置均為(9,-2)self.new=random.randrange(1,8)#隨機方塊#主方塊(參考方塊)的位置self.direction=random.randrange(4)self.head_square=[9,-2]self.new_square=list(shapedic[self.new])for i in range(self.direction):self.change()def delete_one_line(self,j):#得分后刪除整行for t in range(j,2,-1):for i in range(self.column):self.all_square[i,t]=self.all_square[i,t-1]for i in range(self.column):self.all_square[i,0]=0def get_score(self):for j in range(self.row):for i in range(self.column):#判斷某行是否全滿if not self.all_square[i,j]:breakelse:self.scores+=10self.delete_one_line(j)#移動方塊停止def get_seated(self):self.all_square[tuple(self.head_square)]=1for i in range(4):self.all_square[self.head_square[0]+self.new_square[i][0],self.head_square[1]+self.new_square[i][1]]=1#方塊是否到了最底端 def is_seated(self):for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0],self.head_square[1]+self.new_square[i][1]+1]:return Truereturn False"=== Control Part ==="#改變方塊朝向#通過旋轉改變,主方塊不動def change(self):if self.new>1:for i in range(4):if self.all_square[self.head_square[0]+change_dic[self.new_square[i]][0],self.head_square[1]+change_dic[self.new_square[i]][1]]:returnelse:for i in range(4):self.new_square[i]=change_dic[self.new_square[i]]else:return#右移def right_move(self):#先判斷是否可以移動for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0]-1,self.head_square[1]+self.new_square[i][1]]:return Trueself.head_square[0]-=1#左移def left_move(self):for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0]+1,self.head_square[1]+self.new_square[i][1]]:return Trueself.head_square[0]+=1#向下加速def down_quicker(self):while (not self.is_seated()):self.draw_new_square()self.canvas.after(50)self.canvas.update()#方向鍵控制def move(self,event):if event.keycode == 39:self.left_move() elif event.keycode == 38:self.change()elif event.keycode == 37:self.right_move()elif event.keycode == 40:self.down_quicker()else:pass#開始游戲def play(self):self.canvas.bind('<Key>',self.move)self.canvas.focus_set()while True:if self.is_dead():self.gameover()breakif self.is_seated():self.get_seated()self.get_new_square()self.draw_score()self.draw_square()self.draw_new_square()else:self.draw_new_square()self.canvas.after(500)self.canvas.update()#游戲結束def gameover(self):if askquestion("LOSE",u"你輸了!\n重新開始嗎?") == 'yes':return self.restart()else:return self.window.destroy()#重新開始 def restart(self):self.initial()self.draw_square()self.get_new_square()self.draw_new_square()self.play()#主程序
if __name__ == "__main__":Game_Russia()
總結
以上是生活随笔為你收集整理的俄罗斯方块Python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java技术:项目构建工具Maven最佳
- 下一篇: 基于51单片机+LCD1602显示