2016-12-24 76 views
0

我正在玩遊戲,玩家必須通過登陸胸部來收集硬幣。但是,如果他們三次落在同一個胸部,它會刪除那裏的胸部並在其位置插入一個強盜 - 這會將他們的硬幣重置爲零(這部分不包含在運行時提供的代碼中正確)。從基於座標的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格式,它仍然可以正常工作,但是當我嘗試我的遊戲在不同的設備上,我得到了預期的錯誤。是否有一個原因?

+0

'Tkinter'爲默認值僅適用於'GIF' (和另一種格式)。要使用PNG,您需要'PIL'或'pillow'模塊:http://effbot.org/tkinterbook/photoimage.htm。當然'PIL/pillow'可以使用一些外部庫,如果設備沒有這個庫,那麼事件'PIL/pillow'不能幫助。 – furas

+0

'canvas.delete(data [row,col])'或'canvas.delet(chest_dict [row,col])' – furas

+0

@furas我已經走得更遠了,但是您提出的解決方案讓我的程序總是假定胸部始終第7行和列,這給了我一個「關鍵錯誤」。有任何想法嗎? – Ernxst

回答

0

隨着furas的幫助下,我找到了解決辦法:

由於每個寶箱都有自己獨特的行和列,您可以使用它的行和列是指一個個體的胸部,這意味着我們必須找到列出可能的行和列。

由於x1 = col * CELL_WIDTH + 24y1 = row * CELL_HEIGHT - 26其中x1是x座標和y 1是y座標,對行和列的公式必須(x1 - 24)/CELL_WIDTH = col(y1 + 26)/CELL_HEIGHT = row

然後,您將使用arrow_coords[0]arrow_coords[1]替代x和y座標。並獲得從chest_dict具體的行和列,你再會舍入爲最接近的整數,它會從工作區中刪除特定的胸口:

game_grid.delete(chest_dict[int(round(int(arrow_coords[0]-24)/50, 0)), 
          int(round(int(arrow_coords[1]+26)/50, 0))])