Итак, мой код работает, и я не знаю, в чем проблема. Когда я пытаюсь протестировать файл "Битва", он работает как положено, и все в порядке. Он открывает окно tkinter, как я хочу, показывает кнопки, Мои спрайты и т. Д. c. Но когда я использую свой основной файл для запуска файла битвы, он не показывает кнопки, списки или спрайты. Я просмотрел весь код и не могу понять, что не так. Если бы кто-нибудь мог мне помочь, это было бы здорово! Это много кода, и я вроде ладья ie, поэтому он может быть не самым чистым, но я оставляю много заметок.
Это боевой код:
import tkinter as tk
import random
import time
inBattle = False
gameOver = True
i = 0
enemy_type = 0
MAX_HP = 60
MAX_MP = 40
HPval = 20
MPval = 30
POTIONSval = 3
#test
def add_text(recap):
global i
recap.insert(0, 'Damage: ' + str(i))
i += 1
#switch to turn the whole thing on or off
def GameOver():
global gameOver
if gameOver == True:
gameOver = False
else:
gameOver = True
#spells
def Cast_Fireball(spells, MP, recap):
global MPval
if MPval - 10 > 0 or MPval - 10 == 0:
MPval -= 10
MP.config(text='MP: ' + str(MPval))
hit = random.randint(0, 100)
damage = random.randint(1,10) # 1d10
if hit > 9: # 90%
recap.insert(0, 'Hit for ' + str(damage))
spells.destroy()
else:
recap.insert(0, 'Missed!')
spells.destroy()
else:
recap.insert(0, 'Not enough mana.')
spells.destroy()
def Cast_Iceball(spells, MP, recap):
global MPval
if MPval - 12 > 0 or MPval - 12 == 0:
MPval -= 12
MP.config(text='MP: ' + str(MPval))
hit = random.randint(0, 100)
damage = random.randint(1,6) * 2 # 2d6
if hit > 4: # 95%
recap.insert(0, 'Hit for ' + str(damage))
spells.destroy()
else:
recap.insert(0, 'Missed!')
spells.destroy()
else:
recap.insert(0, 'Not enough mana.')
spells.destroy()
def Cast_Heal(spells, HP, MP, recap):
global MPval
global HPval
if MPval - 15 > 0 or MPval - 15 == 0:
if HPval < MAX_HP:
MPval -= 15
MP.config(text='MP: ' + str(MPval))
damage = random.randint(7,25) # 2d10 + 5
if HPval + damage > MAX_HP:
damage = MAX_HP - HPval
recap.insert(0, 'Healed for ' + str(damage))
HPval += damage
HP.config(text='HP: ' + str(HPval))
spells.destroy()
else:
recap.insert(0, 'Not enough mana.')
spells.destroy()
def Cast_Drain(spells, MP, recap):
global MPval
hit = random.randint(0, 100)
damage = random.randint(1,6) # 1d6
if hit > 19: # 80%
recap.insert(0, 'Hit for ' + str(damage))
recap.insert(0, 'Recovered some mana!')
MPval += damage
MP.config(text='MP: ' + str(MPval))
spells.destroy()
else:
recap.insert(0, 'Missed!')
spells.destroy()
#activates window for spell list
def Spell_list(HP, MP, recap):
spells = tk.Tk()
spells.title("Spell List")
spells.minsize(150,168)
Fireball = tk.Button(spells, text='Fireball', bg='black', font=("arial", 15, "bold"), fg="white", width=12, command= lambda: Cast_Fireball(spells, MP, recap))
Fireball.pack()
Iceball = tk.Button(spells, text='Iceball', bg='black', font=("arial", 15, "bold"), fg="white", width=12, command= lambda: Cast_Iceball(spells, MP, recap))
Iceball.pack()
Mana_Drain = tk.Button(spells, text='Mana Drain', bg='black', font=("arial", 15, "bold"), fg="white", width=12, command= lambda: Cast_Drain(spells, MP, recap))
Mana_Drain.pack()
Crude_Heal = tk.Button(spells, text='Crude Heal', bg='black', font=("arial", 15, "bold"), fg="white", width=12, command= lambda: Cast_Heal(spells, HP, MP, recap))
Crude_Heal.pack()
spells.mainloop()
#drinks a max hp potion and checks if your full already and doesn't use it
def Drink_Potion(HP,POTIONS,recap):
global HPval
global POTIONSval
if POTIONSval > 0 and HPval < MAX_HP:
temp = MAX_HP - HPval
HPval = MAX_HP
POTIONSval -= 1
recap.insert(0, 'Healed for ' + str(temp))
HP.config(text="HP: " + str(HPval))
POTIONS.config(text="Potions: " + str(POTIONSval))
#main fucntion
def Battle():
#trigger so the player on the other screen is stopped and cant move
global inBattle
global HPval
global MPval
global POTIONSval
global enemy_type
inBattle = True
#screen settings
screen = tk.Tk()
screen.title("Encounter")
screen.minsize(1000,500)
screen.configure(bg='black')
#background settings and rectangle for the bottom half of screen
canvas = tk.Canvas(screen, width=1000, height=500)
canvas.configure(bg='black')
canvas.pack()
canvas.create_rectangle(3,300,1000,500,fill='white')
sprites = tk.Canvas(screen, width=200, height=200, bg='black')
sprites.place(x=285, y=50)
#choses which enemy to fight
#enemy_type = random.randint(0,6)
#spawning of the enemy
if enemy_type == 0:
img = tk.PhotoImage(file='sprites/slime.png')
sprites.create_image(20,20, image=img, anchor="nw")
#box on right to list the past actions
recap = tk.Listbox(screen, font=("arial", 14), bg="white", width=22, height=12)
recap.place(x=754, y=10)
#trackers for health, mana, and potions
HP = tk.Label(screen, text="HP: " + str(HPval), font=("arial",30), bg="white")
HP.place(x=775, y=310)
MP = tk.Label(screen, text="MP: " + str(MPval), font=("arial",30), bg="white")
MP.place(x=775, y=360)
Potions = tk.Label(screen, text="Potions: " + str(POTIONSval), font=("arial",30), bg="white")
Potions.place(x=775, y=410)
#buttons for commands
Attack = tk.Button(screen,text="Attack",width=9, height=4, bg="black", fg="white", font=("arial", 24, "bold"), command= lambda: add_text(recap))
Attack.place(x=10, y= 313)
Defend = tk.Button(screen,text="Defend",width=9, height=4, bg="black", fg="white", font=("arial", 24, "bold"))
Defend.place(x=200, y= 313)
Item = tk.Button(screen,text="Item",width=9, height=4, bg="black", fg="white", font=("arial", 24, "bold"), command= lambda: Drink_Potion(HP, Potions, recap))
Item.place(x=390, y= 313)
Magic = tk.Button(screen,text="Magic",width=9, height=4, bg="black", fg="white", font=("arial", 24, "bold"), command= lambda: Spell_list(HP, MP, recap))
Magic.place(x=580, y= 313)
screen.mainloop()
#testing purposes / comment out on release
#Battle()
Это основной код:
import random
import turtle
import tkinter as tk
import time
import Battle
callOnce = False
GWIDTH = 800
GHEIGHT = 800
SWIDTH = 50
SHEIGHT = 50
START_Y = 0
END_Y = 0
PLAYER_X = 0
PLAYER_Y = 0
BUFFER = 80
DungeonCord = []
DungeonStorage = []
Treasure = []
pathingPlaces = []
pathingAmount = 0
start = tk.Tk()
start.title("Dungeon Generator")
start.minsize(500,500)
#generate function making it save the set x and y and passing it to the turtle functions
def Generate():
global GHEIGHT
global GWIDTH
Battle.GameOver()
GWIDTH = xInsert.get()
GHEIGHT = yInsert.get()
start.destroy()
DunGenText = tk.Label(text="Dungeon Generator", relief="solid", width = 20, font=("arial", 24, "bold"))
DunGenText.place(x=60, y=35)
xLabel = tk.Label(text="X Grid Count:", width = 10, font=("arial", 14))
xLabel.place(x=145,y=145)
xInsert = tk.Entry(start, width=5)
xInsert.place(x=280,y=150)
yLabel = tk.Label(text="Y Grid Count:", width = 10, font=("arial", 14))
yLabel.place(x=145,y=245)
yInsert = tk.Entry(start, width=5)
yInsert.place(x=280,y=250)
button = tk.Button(start,text="Generate",width=30, bg="black", fg="white", command=Generate)
button.place(x=135, y=450)
start.mainloop()
#main loop for the turtle side of things
while not Battle.gameOver:
#window settings
wn = turtle.Screen()
wn.title("Dungeon Crawler")
wn.setup(width=(int(GWIDTH) * int(SWIDTH))+BUFFER, height=(int(GHEIGHT) * int(SHEIGHT))+BUFFER)
wn.bgcolor("black")
wn.tracer(0)
#where the grid would start to draw itself
Gridx = (int(GWIDTH) * int(SWIDTH) * -1) / 2
Gridy = (int(GHEIGHT) * int(SHEIGHT)) /2
#this will determine the lowest number between the x axis and the y axis
if int(GWIDTH) > int(GHEIGHT):
LOWEST = int(GHEIGHT)
else:
LOWEST = int(GWIDTH)
#this is the formula for the dungeon count in the grid // ((x * y) - 2y) / l
DCOUNT = ((int(GHEIGHT) * int(GWIDTH)) - (2 * int(GHEIGHT))) / int(LOWEST)
#the function to draw the grid
def draw_grid(t, x, y, sx, sy,):
for i in range(int(y)):
for j in range(int(x)):
t.penup()
t.goto(x= Gridx + (sx * int(j)), y= Gridy - (sy * int(i)))
t.pendown()
t.forward(sx)
t.right(90)
t.forward(sx)
t.right(90)
t.forward(sx)
t.right(90)
t.forward(sx)
t.right(90)
#function to draw a square at a location
def draw_square(t, s):
for i in range(4):
t.forward(int(s) * 20)
t.right(90)
#drawing the circle on the left side that would be the start
def draw_start(t, t2):
global START_Y
global PLAYER_Y
START_Y = random.randint(0, int(GHEIGHT) - 1)
PLAYER_Y = START_Y
t.penup()
t.goto(x= Gridx + (SWIDTH / 2), y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * START_Y) - 10)
t.pendown()
t.circle(10)
t2.penup()
t2.goto(x= Gridx + (SWIDTH / 2), y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * START_Y))
t2.pendown()
draw_player(t2)
#drawing a circle on the right side that would be the end
def draw_end(t):
global END_Y
END_Y = random.randint(0, int(GHEIGHT) - 1)
t.penup()
t.goto(x= (Gridx + (SWIDTH / 2)) * -1, y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * END_Y) - 10)
t.pendown()
t.circle(10)
#draws the player at its location
def draw_player(t):
t.penup()
t.goto(x= (Gridx + (SWIDTH / 2)) + (SWIDTH * PLAYER_X) - 10, y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * PLAYER_Y)+3)
t.pendown()
for i in range(50):
t.forward(20)
t.right(144)
#drawing the dungeon at random locations that can be anyware between the entrance and the exit.
def draw_dungeons(t, d):
global DungeonCord
global Treasure
for i in range(int(d)):
closestX = random.randint(1, int(GWIDTH) - 2)
closestY = random.randint(0, int(GHEIGHT) - 1)
DungeonCord.append((closestX,closestY))
DungeonStorage.append((closestX,closestY))
for j in range(i):
if DungeonCord[j] == DungeonCord[i]:
closestX = random.randint(1, int(GWIDTH) - 2)
closestY = random.randint(0, int(GHEIGHT) - 1)
DungeonCord[i] = (closestX,closestY)
DungeonStorage[i] = (closestX,closestY)
if DungeonCord[j] == DungeonCord[i]:
closestX = random.randint(1, int(GWIDTH) - 2)
closestY = random.randint(0, int(GHEIGHT) - 1)
DungeonCord[i] = (closestX,closestY)
DungeonStorage[i] = (closestX,closestY)
#treasure room chance
TR = random.randint(1,5)
if TR == 5:
t.color("yellow")
Treasure.append(DungeonCord[i])
t.penup()
t.goto(x= (Gridx + (SWIDTH / 2)) + (SWIDTH * closestX) - 10, y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * closestY) + 10)
t.pendown()
draw_square(t, 1)
t.color("white")
#the pathmaking function that would go to each dungeon and from the start to the end
def draw_path(t):
global DungeonCord
global START_Y
global END_Y
global DCOUNT
global pathingAmount
global pathingPlaces
t.penup()
t.goto(x= Gridx + (SWIDTH / 2), y= (Gridy - (SHEIGHT / 2)) - (SHEIGHT * START_Y))
t.pendown()
myCordsx = 0
myCordsy = START_Y
dungeon = 0
for j in range(int(DCOUNT)):
traveling = True
closestX = 500
closestY = 500
#finds the closest dungeon
for i in range(int(DCOUNT)):
x, y = DungeonCord[i]
if abs(x - myCordsx) + abs(y - myCordsy) < abs(closestX - myCordsx) + abs(closestY - myCordsy):
closestX, closestY = x, y
dungeon = i
elif abs(x - myCordsx) + abs(y - myCordsy) == abs(closestX - myCordsx) + abs(closestY - myCordsy):
if x < closestX:
closestX, closestY = x, y
dungeon = i
#path to the current closest dungeon
while traveling:
if closestX > myCordsx: #path right
t.forward(int(SWIDTH))
myCordsx += 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif closestY > myCordsy: #path down
t.right(90)
t.forward(int(SWIDTH))
t.left(90)
myCordsy += 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif closestX < myCordsx: #path left
t.right(180)
t.forward(int(SWIDTH))
t.left(180)
myCordsx -= 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif closestY < myCordsy: #path up
t.left(90)
t.forward(int(SWIDTH))
t.right(90)
myCordsy -= 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
else:
traveling = False
i = 0 #reset on counter
DungeonCord[dungeon] = (99, 99) #null entry / dungeon is checked off
#pathing to the end
traveling = True
while traveling:
if int(GWIDTH) - 1 > myCordsx: #going right
t.forward(int(SWIDTH))
myCordsx += 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif END_Y > myCordsy: #going down
t.right(90)
t.forward(int(SWIDTH))
t.left(90)
myCordsy += 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif int(GWIDTH) - 1 != myCordsx and END_Y == myCordsy: #going left
t.right(180)
t.forward(int(SWIDTH))
t.left(180)
myCordsx -= 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
elif int(GWIDTH) - 1 == myCordsx and END_Y != myCordsy: #going up
t.left(90)
t.forward(int(SWIDTH))
t.right(90)
myCordsy -= 1
pathingPlaces.append((myCordsx, myCordsy))
pathingAmount +=1
else:
traveling = False
#all the turtle units involved in drawing
grid = turtle.Turtle()
grid.hideturtle()
grid.pencolor("white")
entrance = turtle.Turtle()
entrance.hideturtle()
entrance.pencolor("green")
end = turtle.Turtle()
end.hideturtle()
end.pencolor("red")
dun = turtle.Turtle()
dun.hideturtle()
dun.pencolor("white")
dun.shape("square")
path = turtle.Turtle()
path.hideturtle()
path.pencolor("green")
player = turtle.Turtle()
player.hideturtle()
player.pencolor("pink")
#player movement
def moveUP():
global PLAYER_X
global PLAYER_Y
for i in range(pathingAmount):
if (PLAYER_X, PLAYER_Y - 1) == pathingPlaces[i] and not Battle.inBattle:
PLAYER_Y -= 1
player.clear()
draw_player(player)
for i in range(int(DCOUNT)):
if (PLAYER_X, PLAYER_Y) == DungeonStorage[i]:
Battle.Battle()
break
def moveDOWN():
global PLAYER_X
global PLAYER_Y
for i in range(pathingAmount):
if (PLAYER_X, PLAYER_Y + 1) == pathingPlaces[i] and not Battle.inBattle:
PLAYER_Y += 1
player.clear()
draw_player(player)
for i in range(int(DCOUNT)):
if (PLAYER_X, PLAYER_Y) == DungeonStorage[i]:
Battle.Battle()
break
def moveLEFT():
global PLAYER_X
global PLAYER_Y
for i in range(pathingAmount):
if (PLAYER_X - 1, PLAYER_Y) == pathingPlaces[i] and not Battle.inBattle:
PLAYER_X -= 1
player.clear()
draw_player(player)
for i in range(int(DCOUNT)):
if (PLAYER_X, PLAYER_Y) == DungeonStorage[i]:
Battle.Battle()
break
def moveRIGHT():
global PLAYER_X
global PLAYER_Y
for i in range(pathingAmount):
if (PLAYER_X + 1, PLAYER_Y) == pathingPlaces[i] and not Battle.inBattle:
PLAYER_X += 1
player.clear()
draw_player(player)
for i in range(int(DCOUNT)):
if (PLAYER_X, PLAYER_Y) == DungeonStorage[i]:
Battle.Battle()
break
#a call once function so the dungeons, start, and end doesn't keep drawing.
if callOnce == False:
draw_grid(grid, GWIDTH, GHEIGHT, SWIDTH, SHEIGHT)
draw_start(entrance, player)
draw_end(end)
draw_dungeons(dun, DCOUNT)
draw_path(path)
callOnce = True
wn.listen()
wn.onkey(moveRIGHT, "d")
wn.onkey(moveUP, "w")
wn.onkey(moveLEFT, "a")
wn.onkey(moveDOWN, "s")
while True:
wn.update()