我正在玩游戏,玩家必须通过登陆胸部来收集硬币。但是,如果他们三次落在同一个胸部,它会删除那里的胸部并在其位置插入一个强盗 - 这会将他们的硬币重置为零(这部分不包含在运行时提供的代码中正确)。从基于坐标的tkinter画布中删除图像
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
from Tkinter import ttk
import random
from collections import Counter
CELL_WIDTH = 50
CELL_HEIGHT = 50
rows = 8
columns = 8
bandit_number = 5
chest_number = 10
moves = 0
coins = 0
chests_visited =()
def create_grid():
game_frame.pack()
game_grid.pack()
data = {}
for col in range(columns):
for row in range(rows):
x1 = col * CELL_WIDTH
y1 = row * CELL_HEIGHT
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_rectangle(x1, y1, x2, y2,
fill="green",
tags="rect")
return data
def create_chests(chest_image):
global chest_dict
chest_dict = {}
for i in range(chest_number):
while True:
row = random.randint(0, rows-1)
col = random.randint(0, columns-1)
if (row,col) not in chest_dict:
break
x1 = col * CELL_WIDTH + 24
y1 = row * CELL_HEIGHT - 26
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
chest_dict[row,col] = game_grid.create_image(x1, y1, image=chest_image,
tags="chest")
return chest_dict
def create_bandits(bandit_image):
global bandit_dict
bandit_dict = {}
for i in range(bandit_number):
while True:
row = random.randint(0, rows-1)
col = random.randint(0, columns-1)
if (row,col) not in bandit_dict:
break
x = col * CELL_WIDTH + 22
y = row * CELL_HEIGHT - 22
x2 = x + CELL_WIDTH
y2 = y + CELL_HEIGHT
bandit_dict[row,col] = game_grid.create_image(x, y, image=bandit_image)
return bandit_dict
def position_player(player_image):
global arrow
arrow = game_grid.create_image(26, 375, image=player_image)
display_widgets()
return arrow
def display_widgets():
global move_entry_x, move_entry_y, help_lbl
help_lbl = tk.Label(game_grid, text="Enter the x value in the first entry"+
" and the y value in the second." + '\n' +
"Use negatives to move left and down.")
game_grid.create_window(200, 420, window=help_lbl)
move_entry_x = tk.Entry(game_grid)
game_grid.create_window(70, 450, window=move_entry_x)
move_entry_y = tk.Entry(game_grid)
game_grid.create_window(200, 450, window=move_entry_y)
enter_btn = ttk.Button(game_grid, text="Enter", command=check_move)
game_grid.create_window(305, 450, window=enter_btn)
def check_move():
global help_lbl
if (
move_entry_x.get()[0] == "-" or
move_entry_y.get()[0] == "-"
):
try:
if (
int(move_entry_x.get()[1])*CELL_WIDTH < 26 or
int(move_entry_x.get()[1])*CELL_WIDTH > int(rows)*CELL_WIDTH
):
print("Illegal move! Enter a different value")
elif (
int(move_entry_y.get()[1])*CELL_WIDTH < 26 or
int(move_entry_y.get()[1])*CELL_WIDTH > int(rows)*CELL_HEIGHT
):
print("Illegal move! Enter a different value")
else:
move_player(arrow)
except ValueError:
print("Please enter a number!")
else:
try:
if (
int(move_entry_x.get())*CELL_WIDTH < 26 or
int(move_entry_x.get())*CELL_WIDTH > int(rows)*CELL_WIDTH
):
print("Illegal move! Enter a different value")
elif (
int(move_entry_y.get())*CELL_WIDTH < 26 or
int(move_entry_y.get())*CELL_WIDTH > int(rows)*CELL_HEIGHT
):
print("Illegal move! Enter a different value")
else:
move_player(arrow)
except ValueError:
print("Please enter a number!")
def move_player(arrow):
global move_entry_x, move_entry_y, help_lbl, moves
x_move = move_entry_x.get()
y_move = move_entry_y.get()
x = int(x_move)*CELL_WIDTH
y = int(y_move)*CELL_HEIGHT
game_grid.move(arrow, x, -y)
moves += 1
print("Moves = "+str(moves))
check_position(arrow, chest_dict)
def check_position(arrow, chest_dict):
global coins, arrow_coords, chests_visited
arrow_coords = game_grid.coords(arrow)
for i in chest_dict:
chest_coords = game_grid.coords(chest_dict[i])
if (
int(arrow_coords[0])-2 in chest_coords and
int(arrow_coords[1])-1 in chest_coords
):
coins += 10
chests_visited += tuple(arrow_coords)
print("Chests visited: "+str(chests_visited))
check_chests()
return arrow, chest_dict
def check_chests():
global chests_visited, chest_dict, bandit_dict
cnt = Counter(chests_visited)
if (
[k for k, v in cnt.items() if v == 3]
):
game_grid.create_image(arrow_coords[0],arrow_coords[1],
image=bandit_image)
print("bandit_time")
window = tk.Tk()
game_frame = tk.Frame(window)
game_grid = tk.Canvas(game_frame, width=500, height=500, borderwidth=0,
highlightthickness=0)
game_grid.itemconfig("rect", fill="green")
bandit_image = tk.PhotoImage(file="Bandit.png")
chest_image = tk.PhotoImage(file="Treasure Chest.png")
player_image = tk.PhotoImage(file="Arrow.png")
rects = create_grid()
bandits = create_bandits(bandit_image)
chests = create_chests(chest_image)
player = position_player(player_image)
window.mainloop()
我知道你可以使用canvas.delete(item_id)
删除对象,因为它已被定义,但我的问题是,因为我创造了我的对象有一本字典,他们没有具体的名字,我可以用我想知道如何根据它的坐标而不是名称从画布中删除对象。
此外,作为一个方面说明,因为我使用的图像,根据我发现的答案,格式必须是GIF,但我可以使用PNG格式,它仍然可以正常工作,但是当我尝试我的游戏在不同的设备上,我得到了预期的错误。是否有一个原因?
'Tkinter'为默认值仅适用于'GIF' (和另一种格式)。要使用PNG,您需要'PIL'或'pillow'模块:http://effbot.org/tkinterbook/photoimage.htm。当然'PIL/pillow'可以使用一些外部库,如果设备没有这个库,那么事件'PIL/pillow'不能帮助。 – furas
'canvas.delete(data [row,col])'或'canvas.delet(chest_dict [row,col])' – furas
@furas我已经走得更远了,但是您提出的解决方案让我的程序总是假定胸部始终第7行和列,这给了我一个“关键错误”。有任何想法吗? – Ernxst