Справочная информация: я делаю игру на Морском корабле, когда пользователи нажимают на броненосец ИИ, я хочу изменить цвет кнопки по умолчанию на «взрыв» .PNG.
Теперь вот проблема. Итак, у меня есть эта сетка кнопок, и я хотел бы знать, как мне разместить новую кнопку перед старой? Он работает для одной из кнопок, но не для одной.
В частности, в AI Underwater и Player Underwater. Это изображение должно помочь.
Так что это код для обновления кнопки.
def draw_new_button_hit(row, column):
global redraw_gameboard
global Player
global AI_player
global AI_surface_cell
global player_underwater_cell
script_dir = os.path.dirname(__file__)
rel_path = "explode.png"
image = Image.open(os.path.join(script_dir, rel_path))
image = image.resize((50,50), Image.ANTIALIAS)
imtk = ImageTk.PhotoImage(image, master = redraw_gameboard)
new_button = Button(redraw_gameboard,
image=imtk,
height = 20+10,
width = 20+16,
command= already_shot, state = DISABLED)
new_button.image = imtk
new_button.lift()
new_button.grid(row = row, column = column)
И это работает для AI & Player SURFACE. Но не Подводные Доски.
Это код для создания моей доски.
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import os
from random import randint
import random
from PIL import Image, ImageTk
Player = {'Carrier':[(1, 11, 1), (1, 12, 1), (1, 13, 1), (1, 14, 1)], 'Submarine': [(1, 0, 0), (1, 1, 0), (1, 2, 0)]}
AI_player = {'Carrier':[(12,12,1), (12,13,1), (12,14,1), (12,15,1)], 'Submarine': [(12,6,0), (12,7,0), (12,8,0)]}
button_height = 2
button_width = 4
def redraw_boards():
global redraw_gameboard
global Player
global AI_player
Player["Player Surface"] = {}
Player["Player Underwater"] = {}
AI_player["AI Surface"] = {}
AI_player["AI Underwater"] = {}
redraw_gameboard = tk.Tk()
redraw_gameboard.title("Battleship Game")
redraw_gameboard.geometry("1000x1000")
redraw_gameboard.resizable(False, False)
Label(redraw_gameboard, text="Player Underwater", height = 3, width = 15).grid(row=0, column=0, columnspan=10)
Label(redraw_gameboard, text="Player Surface", height = 3, width = 15).grid(row=0, column=12, columnspan=10)
# Preparing spacings between the grids
Label(redraw_gameboard, text="", height = 20, width = 4).grid(row=1, column=10, rowspan=10)
Label(redraw_gameboard, text="AI Underwater", height = 3, width = 15).grid(row=11, column=0, columnspan=10)
Label(redraw_gameboard, text="AI Surface", height = 3, width = 15).grid(row=11, column=11, columnspan=10)
#Label(redraw_gameboard, text="", height = 4, width = 1).grid(row=11, column=0, columnspan=10)
for i in range(12, 22):
AI_player["AI Underwater"][i] = {}
for j in range(10):
AI_player["AI Underwater"][i][j] = {}
AI_player["AI Underwater"][i][j]["Presence"] = None
AI_underwater_cell = Button(redraw_gameboard,
height = button_height,
width = button_width,
highlightbackground="#000080",
command=lambda row=i, column=j, depth = 0: shoot(row, column, depth))
AI_underwater_cell.grid(row=i, column=j)
AI_underwater_cell.lower()
for i in range(1, 11):
Player["Player Underwater"][i] = {}
for j in range(10):
Player["Player Underwater"][i][j] = {}
Player["Player Underwater"][i][j]["Presence"] = None
player_underwater_cell = Button(redraw_gameboard,
height = button_height,
width = button_width,
command=cannot_shoot,
highlightbackground="#000080")
player_underwater_cell.grid(row=i, column=j)
player_underwater_cell.lower()
for i in range(1, 11):
Player["Player Surface"][i] = {}
for j in range(11, 21):
Player["Player Surface"][i][j] = {}
Player["Player Surface"][i][j]["Presence"] = None
player_surface_cell = Button(redraw_gameboard,
height = button_height,
width = button_width,
command=cannot_shoot,
highlightbackground="#1E90FF")
player_surface_cell.grid(row=i, column=j)
for i in range(12, 22):
AI_player["AI Surface"][i] = {}
for j in range(11, 21):
AI_player["AI Surface"][i][j] = {}
AI_player["AI Surface"][i][j]["Presence"] = None
AI_surface_cell = Button(redraw_gameboard,
height = button_height,
width = button_width,
highlightbackground="#1E90FF",
command=lambda row=i, column=j, depth = 1: shoot(row, column, depth))
AI_surface_cell.grid(row=i, column=j)
for i in range(len(Player["Carrier"])):
player_ship_location = Button(redraw_gameboard,
height = button_height,
width = button_width,
command=cannot_shoot,
highlightbackground="#2E8B57")
player_ship_location.grid(row = Player["Carrier"][i][0], column = Player["Carrier"][i][1])
for i in range (len(Player["Submarine"])):
player_ship_location = Button(redraw_gameboard,
height = button_height,
width = button_width,
command=cannot_shoot,
highlightbackground="#2E8B57")
player_ship_location.grid(row = Player["Submarine"][i][0], column = Player["Submarine"][i][1])
print (redraw_gameboard.grid_slaves)
redraw_gameboard.mainloop()