Помогите с кодом. После выбора игрока, при нажатии клавиши, шар не поворачивает

Nefot

Новичок
Пользователь
Окт 25, 2020
1
0
1
from tkinter import *
import random
import math

Ball = Tk()
my_canvas = Canvas(Ball, width=500, height=500, bg='green')
Ball.update()


class Balls:
def __init__(self, canvas, coord, tags, number, key, color):
self.number = number
self.key = key
self.canvas = canvas
self.tags = tags
self.x1 = random.randrange(1, 10)
self.y1 = random.randrange(1, 10)
self.coord = coord
self.position = []
self.oval = canvas.create_oval(10, coord + 10, 65, coord + 65, fill=color, width=5, outline="#fff",
tag=self.tags)
self.name = canvas.create_text(36, coord + 37.5, text=number, justify=CENTER, font='Time 25 italic bold',
fill='white', tag=self.tags)
self.face = canvas.create_arc(10, coord + 10, 65, coord + 65, width=3, tag=self.tags, style=ARC)
self.firstArch = 0
self.endArch = 0
self.canvas.bind_all(self.key, self._condition)

def moving(self):
self.position = self.canvas.coords(self.tags)
if self.position[0] <= 0:
self.x1 = -self.x1
elif self.position[1] <= 0:
self.y1 = -self.y1
elif self.position[2] >= 500:
self.x1 = -self.x1
elif self.position[3] >= 500:
self.y1 = -self.y1

self.canvas.move(self.tags, self.x1, self.y1)
self.canvas.after(10, self.moving)

def turnRight(self, event):
self.firstArch -= 20
self.endArch = 90 + self.firstArch
self.x1 = (math.cos(math.radians((self.endArch + self.firstArch) / 2)))
self.x1 = (math.sin(math.radians((self.endArch + self.firstArch) / 2)))

def _condition(self, event):

self.canvas.bind_all("KeyPress-Right", self.turnRight)


gamer1 = Balls(my_canvas, 20, 'group1', '1', '<KeyPress-1>', 'blue')
gamer2 = Balls(my_canvas, 200, 'group2', '2', '<KeyPress-1>', 'red')
gamer3 = Balls(my_canvas, 100, 'group3', '3', '<KeyPress-1>', 'yellow')


def begin():
gamer1.moving()
gamer2.moving()
gamer3.moving()


btn = Button(text='СТАРТ', command=begin)

btn.pack()
my_canvas.pack()
Ball.mainloop()
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Код вставляйте с помощью ... -> код -> python.
После выбора игрока, при нажатии клавиши, шар не поворачивает
"KeyPress-Right" замените на "<KeyPress-Right>".
 

Форум IT Специалистов