global не работает. Не пойму в чем проблема?
Делаю небольшую игру, counter - глобальная переменная, но при дальнейшем ее использовании, не изменяется.
orb и block - это круги, которые отталкиваются друг от друга при столкновении и orb уничтожает block принося 1$ в counter.
from tkinter import *
import random
window = Tk()
window.title('Game')
window.geometry('900x800')
window.resizable(width=False, height=False)
canvas = Canvas(window, width=900, height=700)
canvas.pack()
side = Label(bg='black')
side.place(x=0,y=700,width=900,height=10)
class Orb:
def __init__(self):
self.x = random.randint(25,875)
self.y = random.randint(25,675)
self.v = -1
self.v1 = -1
self.photo = PhotoImage(file=r"C:\Users\NiKiT\PycharmProjects\Maximum\Module 2\Project Game\orb.png")
class Block:
def __init__(self,hp):
self.x = random.randint(50,850)
self.y = random.randint(50,650)
self.hp = hp
self.photo = PhotoImage(file=r"C:\Users\NiKiT\PycharmProjects\Maximum\Module 2\Project Game\block1.png")
blocks = [Block(1),Block(1),Block(1)]
orbs = [Orb(),Orb()]
counter = 0
speed = 10
def game():
global speed, counter
window.after(speed, game)
canvas.delete('all')
for orb in orbs:
canvas.create_image(orb.x, orb.y, image=orb.photo)
orb.x += orb.v
orb.y += orb.v1
if orb.x < 25:
orb.v = orb.v * (-1)
orb.x = 25
if orb.x > 875:
orb.v = orb.v * (-1)
orb.x = 875
if orb.y < 25:
orb.v1 = orb.v1 * (-1)
orb.y = 25
if orb.y > 675:
orb.v1 = orb.v1 * (-1)
orb.y = 675
for block in blocks:
canvas.create_image(block.x, block.y, image=block.photo)
if ((orb.x - block.x) ** 2 + (orb.y - block.y) ** 2) ** 0.5 < 75:
counter += 1
blocks.remove(block)
blocks.append(Block(1))
orb.v = orb.v * (-1)
orb.v1 = orb.v1 * (-1)
def ok():
hello.destroy()
hello_button.destroy()
game()
speed_up_money = 10
count_speed_up = 1
def speed_up_func():
global counter, speed_up_money, speed, count_speed_up
if counter >= speed_up_money:
count_speed_up += 1
counter -= speed_up_money
if count_speed_up == 1:
speed = 10
speed_up_money = 10
if count_speed_up == 2:
speed = 7
speed_up_money = 10
if count_speed_up == 3:
speed = 5
speed_up_money = 30
if count_speed_up == 4:
speed = 3
speed_up_money = 90
if count_speed_up == 5:
speed = 1
speed_up_money = ''
speed_up_button.destroy()
hello = Label(text="Цель игры - купить все улучшения\n(серые круги = $)",bg="grey77",font="Arial 24",relief="raised")
hello.place(y=300,x=200)
hello_button = Button(text="Ok",command=ok,font="Arial 14")
hello_button.place(y=380,x=675)
speed_up = Label(text=f"Улучшений скорости: {count_speed_up}/5\nУлучшить скорость: {speed_up_money}",font="Arial 14")
speed_up_button = Button(text="Купить",command=speed_up_func,font="Arial 12")
speed_up.place(x=200,y=710)
speed_up_button.place(x=200,y=760)
money = Label(text=f"$: {counter}")
money.place(x=50, y=750)
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
from tkinter import *
import random
window = Tk()
window.title('Game')
window.geometry('900x800')
window.resizable(width=False, height=False)
canvas = Canvas(window, width=900, height=700, bg='blue')
canvas.pack()
side = Label(bg='red') # black
side.place(x=0, y=600, width=900, height=10) # x=0, y=700, width=900, height=10
class Orb:
def __init__(self):
self.x = random.randint(25, 875)
self.y = random.randint(25, 675)
self.v = -1
self.v1 = -1
self.photo = PhotoImage(file="_+_.png") # установите свое
class Block:
def __init__(self, hp):
self.x = random.randint(50, 850)
self.y = random.randint(50, 650)
self.hp = hp
self.photo = PhotoImage(file="Ok.png") # установите свое
blocks = [Block(1),Block(1),Block(1)]
orbs = [Orb(),] # [Orb(),Orb()] ???
counter = 0
speed = 10
def game():
global speed, counter
window.after(speed, game)
canvas.delete('all')
for orb in orbs:
canvas.create_image(orb.x, orb.y, image=orb.photo)
orb.x += orb.v
orb.y += orb.v1
if orb.x < 25:
orb.v = orb.v * (-1)
orb.x = 25
if orb.x > 875:
orb.v = orb.v * (-1)
orb.x = 875
if orb.y < 25:
orb.v1 = orb.v1 * (-1)
orb.y = 25
if orb.y > 675:
orb.v1 = orb.v1 * (-1)
orb.y = 675
for block in blocks:
canvas.create_image(block.x, block.y, image=block.photo)
if ((orb.x - block.x) ** 2 + (orb.y - block.y) ** 2) ** 0.5 < 75:
print(f'counter = {counter}') #
counter += 1
blocks.remove(block)
blocks.append(Block(1))
orb.v = orb.v * (-1)
orb.v1 = orb.v1 * (-1)
# +++
money.config(text=f"$: {counter}", bg='#00FFFF') # +++
def ok():
hello.destroy()
hello_button.destroy()
game()
speed_up_money = 10
count_speed_up = 1
def speed_up_func():
global counter, speed_up_money, speed, count_speed_up
if counter >= speed_up_money:
count_speed_up += 1
counter -= speed_up_money
if count_speed_up == 1:
speed = 10
speed_up_money = 10
if count_speed_up == 2:
speed = 7
speed_up_money = 10
if count_speed_up == 3:
speed = 5
speed_up_money = 30
if count_speed_up == 4:
speed = 3
speed_up_money = 90
if count_speed_up == 5:
speed = 1
speed_up_money = ''
speed_up_button.destroy()
hello = Label(text="Цель игры - купить все улучшения\n(серые круги = $)",
bg="grey77", font="Arial 24", relief="raised")
hello.place(y=300,x=200)
hello_button = Button(text="Ok", command=ok,font="Arial 14")
hello_button.place(y=380, x=675)
speed_up = Label(text=f"Улучшений скорости: {count_speed_up}/5\nУлучшить скорость: {speed_up_money}",font="Arial 14")
speed_up_button = Button(text="Купить",command=speed_up_func,font="Arial 12")
speed_up.place(x=200, y=610)
speed_up_button.place(x=200, y=660)
money = Label(text=f"$: {counter}")
money.place(x=50, y=650)
if __name__ == '__main__': # +++
window.mainloop() # +++
_+_.png
Ok.png



