這是一款基于python海龜模塊的翻色游戲。游戲由一塊長方形棋盤組成,棋盤上分為許多不同顏色的平面彩色瓷磚。游戲的目標是通過選擇棋盤上的一個瓷磚,然后將其顏色更改為另一種顏色,將所有瓷磚變成相同的顏色。新選擇的顏色將擴散到與所選圖塊的原始顏色匹配的所有相鄰圖塊。玩家繼續選擇另一塊瓷磚并將其顏色更改為另一塊,直到板上的所有瓷磚都翻轉為相同的顏色。除了矩形板之外,板下方還顯示了一系列顏色。這些是玩家可以選擇翻轉所選瓷磚顏色的顏色。
當我開始運行我的游戲時,它所占用的記憶體隨著運行時間的增加而變得越來越大。而且它變得越來越慢。我認為這可能是由于我的代碼末尾的 while 回圈。但我不確定。如何修改它以使其更快?
from turtle import *
from random import choice
from functools import partial
# set game dimension to be 5 x 5
g_dim = 5
# use random.choice() to create the color for the game
g_game = [choice(['#0000FF', '#FF0000', '#FFFF00', '#008000', '#00FFFF']) for i in range(25)]
# provide the option to flip during the game
optionColor = ['#0000FF', '#FF0000', '#FFFF00', '#008000', '#00FFFF']
# show a set of colors as option for user to flip
def promptColorToFlip(optionColor, # a list that contains a set of the color for user to choose from
height=100, # the height of the option tiles
width=100 # the width of the option tiles
):
# the coordinates of the first tiles
x = -200
y = -200
for i in range(len(optionColor)):
tile = prototype.clone()
tile.goto(i * width x, -(height 50) y)
tile.color('white', optionColor[i])
tile.onclick(partial(returnChosenColor, i))
# return the index of the select-to-flip-to color in the optionColor list
def returnChosenColor(userChosenColor, # the index of the select-to-flip-to color in the optionColor list
x, y # take the positional arguments from the onclick() function to avoid errors, no significant meaning
):
global userOption
userOption = userChosenColor
def refreshScreen(game, rows=5, columns=5, height=100, width=100):
x = -200
y = -200
for column in range(columns):
for row in range(rows):
square = prototype.clone()
square.goto(column * (5 width) x , row * (5 height) y)
square.onclick(partial(userChosenTile, row, column))
if state['framed'] == row*5 column:
square.color('black', game[row*5 column])
else:
square.color('white', game[row*5 column])
update()
def userChosenTile(ROW, COL, x, y):
global state, R, C
state['framed'] = ROW*5 COL
R = ROW
C = COL
def flipColor(row, col, game, orig, to):
print('excuted')
global userOption, state, R, C
if orig == to:
return game
if row < 0 or row >= g_dim:
return
if col < 0 or col >= g_dim:
return
idx = row*g_dim col
if game[idx] != orig:
return
print(idx, 'excuted')
game[idx] = to
flipColor(row-1, col, game, orig, to)
flipColor(row 1, col, game, orig, to)
flipColor(row, col-1, game, orig, to)
flipColor(row, col 1, game, orig, to)
state = {'framed':None}
R = None
C = None
userOption = None
return game
# initialize the game status
state = {'framed':None} # stores the number of the last selected tile, which will be framed with a black border
R = None # the row of the last selected tile
C = None # the column of the last selected tile
userOption = None # the index of the select-to-flip-to color in the optionColor list
# create a prototype of the tiles
prototype = Turtle()
prototype.shape('square')
prototype.shapesize(5, 5, 5)
prototype.penup()
# disable auto screen refresh
tracer(False)
# run the game
while True:
# the try and except block here is to prevent error from raising when user terminate the progarm
try:
promptColorToFlip(optionColor)
refreshScreen(g_game)
if state['framed'] is not None and R is not None and C is not None and userOption is not None:
g_game = flipColor(R, C, g_game, g_game[state['framed']], optionColor[userOption])
except:
pass
uj5u.com熱心網友回復:
我還沒有完全完成你有些復雜的代碼,但我很確定我在游戲運行一段時間時看到了你漸進式減速的根源。它就在這里,在你的refreshScreen函式中:
for column in range(columns):
for row in range(rows):
square = prototype.clone()
...
此代碼在每一幀上為您的棋盤的每個方格創建一個新海龜。那些海龜永遠不會消失,所以你只會不斷地堆積越來越多的海龜,這會隨著時間的推移減慢整個游戲的性能。解決方案可能是每個位置只使用一只海龜,并保留對它們的參考,以便您可以在必要時更改它們的顏色,而不是每次都重新制作它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464445.html
標籤:python-3.x 表现
