Python書き込みTetris

この記事の例では、参考のためにPythonでTetrisを実装するための特定のコードを共有しています。具体的な内容は次のとおりです。

# coding=utf-8from tkinter import*from random import*import threading
from tkinter.messagebox import showinfo
from tkinter.messagebox import askquestion
import threading
from time import sleep
classBrickGame(object): 
# 始めるかどうか
start = True; 
# 底に到達するかどうか
isDown = True; 
isPause = False; 
# 形
window = None; 
# frame 
frame1 = None; 
frame2 = None; 
# ボタン
btnStart = None; 
# 描画クラス
canvas = None; 
canvas1 = None; 
# 題名
title ="BrickGame"; 
# 幅と高さ
width =450; 
height =670; 
# 行と列
rows =20; 
cols =10; 
# 落下する立方体の糸
downThread = None; 
# いくつかの正方形
brick =[[[[0,1,1],[1,1,0],[0,0,0]],[[1,0,0],[1,1,0],[0,1,0]],[[0,1,1],[1,1,0],[0,0,0]],[[1,0,0],[1,1,0],[0,1,0]]],[[[1,1,1],[1,0,0],[0,0,0]],[[0,1,1],[0,0,1],[0,0,1]],[[0,0,0],[0,0,1],[1,1,1]],[[1,0,0],[1,0,0],[1,1,0]]],[[[1,1,1],[0,0,1],[0,0,0]],[[0,0,1],[0,0,1],[0,1,1]],[[0,0,0],[1,0,0],[1,1,1]],[[1,1,0],[1,0,0],[1,0,0]]],[[[0,0,0],[0,1,1],[0,1,1]],[[0,0,0],[0,1,1],[0,1,1]],[[0,0,0],[0,1,1],[0,1,1]],[[0,0,0],[0,1,1],[0,1,1]]],[[[1,1,1],[0,1,0],[0,0,0]],[[0,0,1],[0,1,1],[0,0,1]],[[0,0,0],[0,1,0],[1,1,1]],[[1,0,0],[1,1,0],[1,0,0]]],[[[0,1,0],[0,1,0],[0,1,0]],[[0,0,0],[1,1,1],[0,0,0]],[[0,1,0],[0,1,0],[0,1,0]],[[0,0,0],[1,1,1],[0,0,0]]],[[[1,1,0],[0,1,1],[0,0,0]],[[0,0,1],[0,1,1],[0,1,0]],[[0,0,0],[1,1,0],[0,1,1]],[[0,1,0],[1,1,0],[1,0,0]]]]; 
# 現在の広場
curBrick = None; 
# 現在のブロック配列
arr = None; 
arr1 = None; 
# 現在の正方形
shape =-1; 
# 現在の正方形の行と列(左上隅)
curRow =-10; 
curCol =-10; 
# バックグラウンド
back =list(); 
# 格子
gridBack =list(); 
preBack =list(); 
# 初期化
def init(self):for i inrange(0,self.rows): 
self.back.insert(i,list()); 
self.gridBack.insert(i,list());for i inrange(0,self.rows):for j inrange(0,self.cols): 
self.back[i].insert(j,0); 
self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));for i inrange(0,3): 
self.preBack.insert(i,list());for i inrange(0,3):for j inrange(0,3): 
self.preBack[i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black")); 
# ゲームのグリッドを描く
def drawRect(self):for i inrange(0,self.rows):for j inrange(0,self.cols):if self.back[i][j]==1: 
self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white"); 
elif self.back[i][j]==0: 
self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white"); 
# プレビューボックスを描く
for i inrange(0,len(self.arr1)):for j inrange(0,len(self.arr1[i])):if self.arr1[i][j]==0: 
self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
elif self.arr1[i][j]==1: 
self.canvas1.itemconfig(self.preBack[i][j],fill="orange",outline="white"); 
# 現在移動しているブロックを描画します
if self.curRow!=-10 and self.curCol!=-10:for i inrange(0,len(self.arr)):for j inrange(0,len(self.arr[i])):if self.arr[i][j]==1:   
self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white"); 
# ブロックが一番下に移動したかどうかを確認します
if self.isDown:for i inrange(0,3):for j inrange(0,3):if self.arr[i][j]!=0: 
self.back[self.curRow+i][self.curCol+j]= self.arr[i][j]; 
# 行全体を判断して排除する
self.removeRow(); 
# 死んでいるかどうかを判断する
self.isDead(); 
# 次のブロックを取得
self.getCurBrick(); 
# 削除する必要がある行全体があるかどうかを判断します
def removeRow(self): 
count=0for i inrange(0,self.rows): 
tag1 = True;for j inrange(0,self.cols):if self.back[i][j]==0: 
tag1 = False;break;if tag1==True: 
# 上から下に移動
count=count+1for m inrange(i-1,0,-1):for n inrange(0,self.cols): 
self.back[m+1][n]= self.back[m][n]; 
scoreValue =eval(self.scoreLabel2['text'])
scoreValue +=5*count*(count+3)
self.scoreLabel2.config(text=str(scoreValue))
# 現在の正方形を取得します
def getCurBrick(self): 
self.curBrick =randint(0,len(self.brick)-1); 
self.shape =0; 
# 現在のブロック配列
self.arr = self.brick[self.curBrick][self.shape]; 
self.arr1 = self.arr; 
self.curRow =0; 
self.curCol =1; 
# 一番下までが間違っているかどうか
self.isDown = False; 
# キーボード入力を監視する
def onKeyboardEvent(self,event): 
# 開始されていないため、キーボード入力を監視する必要はありません
if self.start == False:return;if self.isPause == True:return;
# 元の値を記録する
tempCurCol = self.curCol; 
tempCurRow = self.curRow; 
tempShape = self.shape; 
tempArr = self.arr; 
direction =-1;if event.keycode==37: 
# 左にシフト
self.curCol-=1; 
direction =1; 
elif event.keycode==38: 
# 正方形の形を変える
self.shape+=1; 
direction =2;if self.shape =4: 
self.shape=0; 
self.arr = self.brick[self.curBrick][self.shape]; 
elif event.keycode==39: 
direction =3; 
# 右シフト
self.curCol+=1; 
elif event.keycode==40: 
direction =4; 
# 下に移動
self.curRow+=1;if self.isEdge(direction)==False: 
self.curCol = tempCurCol; 
self.curRow = tempCurRow; 
self.shape = tempShape; 
self.arr = tempArr; 
self.drawRect();return True; 
# 現在の正方形が境界に到達するかどうかを判断します
def isEdge(self,direction): 
tag = True; 
# 左側で、境界を判断します
if direction==1:for i inrange(0,3):for j inrange(0,3):if self.arr[j][i]!=0and(self.curCol+i<0 or self.back[self.curRow+j][self.curCol+i]!=0): 
tag = False;break; 
# 右側で、境界を判断します
elif direction==3:for i inrange(0,3):for j inrange(0,3):if self.arr[j][i]!=0and(self.curCol+i =self.cols or self.back[self.curRow+j][self.curCol+i]!=0): 
tag = False;break; 
# ダウン、底を判断する
elif direction==4:for i inrange(0,3):for j inrange(0,3):if self.arr[i][j]!=0and(self.curRow+i =self.rows or self.back[self.curRow+i][self.curCol+j]!=0): 
tag = False; 
self.isDown = True;break; 
# 変形を実行し、境界を決定します
elif direction==2:if self.curCol<0: 
self.curCol=0;if self.curCol+2=self.cols: 
self.curCol = self.cols-3;if self.curRow+2=self.rows: 
self.curRow = self.curRow-3;return tag; 
# ボックスが下に移動します
def brickDown(self):while True:if self.start==False:print("exit thread");break;if self.isPause==False: 
tempRow = self.curRow; 
self.curRow+=1;if self.isEdge(4)==False: 
self.curRow = tempRow; 
self.drawRect(); 
# 毎秒1フレームずつ減少します
sleep(1); 
# クリックして開始
def clickStart(self): 
self.start = True;for i inrange(0,self.rows):for j inrange(0,self.cols): 
self.back[i][j]=0; 
self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");for i inrange(0,len(self.arr)):for j inrange(0,len(self.arr[i])): 
self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white"); 
self.getCurBrick(); 
self.drawRect(); 
self.downThread = threading.Thread(target=self.brickDown,args=()); 
self.downThread.start();
def clickPause(self):
self.isPause=not self.isPause
print(self.isPause)if not self.isPause:
self.btnPause["text"]="タイムアウト"else:
self.btnPause["text"]="戻す"
def clickReStart(self):
ackRestart =askquestion("再起動","你确定要再起動吗?")if ackRestart =='yes':
self.clickStart()else:return
def clickQuit(self):
ackQuit =askquestion("脱落","你确定要脱落吗?")if ackQuit =='yes':
self.window.destroy()exit()
# 死んでいるかどうかを判断する
def isDead(self):for j inrange(0,len(self.back[0])):if self.back[0][j]!=0:showinfo("促す","あなたは電話を切ります、別のものを持っていきましょう!"); 
self.start = False;break; 
# 実行
def __init__(self): 
self.window =Tk(); 
self.window.title(self.title); 
self.window.minsize(self.width,self.height); 
self.window.maxsize(self.width,self.height);   
self.frame1 =Frame(self.window,width=300,height=600,bg="black"); 
self.frame1.place(x=20,y=30); 
self.scoreLabel1 =Label(self.window,text="Score:",font=(30))
self.scoreLabel1.place(x=340,y=60)
self.scoreLabel2 =Label(self.window,text="0",fg='red',font=(30))
self.scoreLabel2.place(x=410,y=60)
self.frame2 =Frame(self.window,width=90,height=90,bg="black"); 
self.frame2.place(x=340,y=120); 
self.canvas =Canvas(self.frame1,width=300,height=600,bg="black"); 
self.canvas1 =Canvas(self.frame2,width=90,height=90,bg="black"); 
self.btnStart =Button(self.window,text="開始",command=self.clickStart); 
self.btnStart.place(x=340,y=400,width=80,height=25); 
self.btnPause =Button(self.window,text="タイムアウト",command=self.clickPause); 
self.btnPause.place(x=340,y=450,width=80,height=25);
self.btnReStart =Button(self.window,text="再起動",command=self.clickReStart); 
self.btnReStart.place(x=340,y=500,width=80,height=25);
self.btnQuit =Button(self.window,text="脱落",command=self.clickQuit); 
self.btnQuit.place(x=340,y=550,width=80,height=25);
self.init(); 
# 現在の正方形を取得します
self.getCurBrick(); 
# 配列に従って、グリッドを描画します
self.drawRect();
self.canvas.pack();
self.canvas1.pack(); 
# キーボードイベントを聞く
self.window.bind("<KeyPress ",self.onKeyboardEvent); 
# キューブドロップスレッドを開始します
self.downThread = threading.Thread(target=self.brickDown,args=()); 
self.downThread.start();  
self.window.mainloop(); 
self.start=False; 
pass;if __name__=='__main__': 
brickGame =BrickGame();

Tetrisに関するさらにエキサイティングな記事については、トピック「TetrisGameCollection」をクリックして学習してください。

以上が本稿の内容ですので、皆様のご勉強に役立てていただければ幸いです。

Recommended Posts

Python書き込みTetris
PythonはTetrisゲームを実装しています
pythonでguiを書く
Pythonオープン読み取りおよび書き込み
pythonはダウンロードソフトウェアをコンパイルします
Pythonファイルの読み取りおよび書き込み操作
C ++を使用してPython3拡張機能を作成する
python設定ファイルの書き方
pythonでクラスを書く方法
pythonでreturnを書く方法
実際の戦闘| Python書き込みポートスキャナー
Python CookBook
Python FAQ
Python3モジュール
python(you-get)
Python文字列
Pythonの基本
Pythonの基本2
pythonでwinプログラムを書く方法
Python exec
Pythonノート
Python3タプル
CentOS + Python3.6 +
Python Advanced(1)
Pythonデコレータ
Pythonマルチスレッド
pythonでtryステートメントを書く方法
Pythonツールチェーン
Python3リスト
Pythonマルチタスク-日常
Pythonの概要
pythonの紹介
Pythonアナリティック
Pythonの基本
07.Python3関数
Pythonの基本3
Pythonマルチタスクスレッド
Python関数
python sys.stdout
python演算子
Pythonエントリ-3
Centos 7.5 python3.6
Python文字列
pythonキューキュー
Pythonの基本4
Pythonの基本5
pythonでゲームを書く方法を教えてください
Pythonメモリマップファイルの読み取りおよび書き込みメソッド
告白プログラムをpythonで書く方法
Pythonでファイルを読み書きする方法