2016-11-20 55 views
-1

画像をグリッドのランダムなセルに配置するゲームを作成しようとしています。 私はグリッドを作成していましたが、画像をランダムに配置するためにグリッドを使用しようとしましたが、私が望むように動作していないようです。したがってbandits = 5 - -ランダムに画像をtkinterキャンバスグリッドに配置する方法

try: 
    import tkinter as tk 
    from tkinter import ttk 
    from tkinter.ttk import * 
except ImportError: 
    import Tkinter as tk 
    from Tkinter import ttk 
    from Tkinter.ttk import * 
import random 
from random import randint 

window = tk.Tk() 
style = ttk.Style(window) 
style.configure("BW.TLabel") 
game_frame = tk.Frame(window) 
game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0, 
         highlightthickness=0) 

def grid_create(): 
    global row, col, cell_width, cell_height, rect, rows , columns 
    rows = 8 
    columns = 8 
    cell_width = 50 
    cell_height = 50 
    rect = {} 
    for i in (game_grid, game_frame): 
     i.pack() 
    for column in range(8): 
     for row in range(8): 
      x1 = column*cell_width 
      y1 = row * cell_height 
      x2 = x1 + cell_width 
      y2 = y1 + cell_height 
      rect[row,column] = game_grid.create_rectangle(x1,y1,x2,y2, 
                 fill="green", 
                  tags="rect") 
    grid_draw() 
def grid_draw(): 
    global row, col 
    game_grid.itemconfig("rect", fill="green") 
    for i in range(8): 
     row = random.randint(0,8) 
     col = random.randint(0,8) 
    create_objects() 
def create_objects(): 
    rows = 8 
    columns = 8 
    cell_width = 50 
    cell_height = 50 
    bandits = 5 
    bandit_list = {} 
    bandit_img = tk.PhotoImage(file="Bandit.png") 

    for column in range(randint(0,8)): 
     for row in range(randint(0,8)): 
      x1 = column*cell_width + 22 
      y1 = row * cell_height - 22 
      x2 = x1 + cell_width 
      y2 = y1 + cell_height 
      bandit_list[row, column] = game_grid.create_image(x1, y2, 
                   image=bandit_img) 
    for i in range(randint(0,5)): 
     row = random.randint(0,8) 
     col = random.randint(0,8) 
grid_create() 
window.mainloop() 

私は、ランダムなセルに表示される5「盗賊」をしたいが、私はこれを行う方法を見つけ出すように見えることはできません。私はまた、「盗賊」が重なり合うことを望まない。どんな助けもありがとうございます。

イメージ:

Link to image as I do not have the required reputation to embed it here.

答えて

1

関数内のローカル変数に割り当てられた(メモリから)画像を削除PhotoImage"Garbage Collector"に問題があります。

グローバル変数ieに割り当てる必要があります。

# create global variable 
bandit_img = None 

def create_objects(): 
    # inform function to use global varia 
    global bandit_img 

    bandit_img = tk.PhotoImage(file="Bandit.png") 

またはインスタンスをウィジェットには:あなたがランダムな値(row,col)を取得し、それが山賊リストに存在しないかどうかを確認するために持っている唯一の5盗賊を取得するにはページhttp://effbot.org/tkinterbook/photoimage.htm


の「注意」を参照してください。リストに(row,col)が存在する場合は、再度ランダムな値を取得して再度チェックします。一意のものを見つけるときにwhile Trueループとbreakループを残すことができます(row,col)

try: 
    import tkinter as tk 
    import tkinter.ttk as ttk 
except ImportError: 
    import Tkinter as tk 
    import ttk 

import random 

# --- constants --- (UPPER_CASE names) 

ROWS = 8 
COLUMNS = 8 
CELL_WIDTH = 50 
CELL_HEIGHT = 50 
BANDITS_NUMBER = 5 

# --- functions --- 

def create_grid(): 

    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_bandits(image): 

    data = {} 

    for i in range(BANDITS_NUMBER): 

     while True: 
      row = random.randint(0, ROWS-1) 
      col = random.randint(0, COLUMNS-1) 
      if (row,col) not in data: 
       break 

     x1 = col * CELL_WIDTH + 22 
     y1 = row * CELL_HEIGHT - 22 
     x2 = x1 + CELL_WIDTH 
     y2 = y1 + CELL_HEIGHT 

     data[row, col] = game_grid.create_image(x1, y2, image=image) 

    return data 

# --- main --- 

# - init - 

window = tk.Tk() 

game_frame = tk.Frame(window) 
game_frame.pack() 

game_grid = tk.Canvas(game_frame, width=450, height=450, borderwidth=0, 
         highlightthickness=0) 
game_grid.pack() 
game_grid.itemconfig("rect", fill="green") 

# - data - 

rects = create_grid() 

# create global variable 
bandit_image = tk.PhotoImage(file="Bandit.png") 

# send image to function - so you don't need word "global" 
bandits = create_bandits(bandit_image) 

# - start - 

window.mainloop() 
関連する問題