日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 俄罗斯方块ai_TKinter实现俄罗斯方块

發布時間:2023/12/10 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 俄罗斯方块ai_TKinter实现俄罗斯方块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[python]代碼庫# -*- coding: utf-8 -*-

from Tkinter import *

import random

from tkMessageBox import askquestion

# 尚需改進,有待提高

# 各種方塊的表示,與參考方塊的相對位置

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 = 28

self.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

"""

用來debug

for 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] += 1

color = 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 = 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

def is_dead(self):

# 判斷死亡與否,最上方中間四個方塊

for i in {8, 9, 10, 11}:

if self.all_square[i, 0]:

return True

else:

return False

def 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] = 0

def get_score(self):

for j in range(self.row):

for i in range(self.column):

# 判斷某行是否全滿

if not self.all_square[i, j]:

break

else:

self.scores += 10

self.delete_one_line(j)

# 移動方塊停止

def get_seated(self):

self.all_square[tuple(self.head_square)] = 1

for 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 True

return 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]]:

return

else:

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 True

self.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 True

self.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('', self.move)

self.canvas.focus_set()

while True:

if self.is_dead():

self.gameover()

break

if 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 俄罗斯方块ai_TKinter实现俄罗斯方块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。