Я застрял в этой игре в понг и не могу понять, что не так.Я работал над этим около 2 недель, и как только я начал делать ударную таблицу веслом, это породило мне миллион ошибок, и я не мог исправить это оттуда.Я пытался избавиться от ошибок около недели.
Ошибки:
Traceback (most recent call last):
File "animationTest.py", line 75, in <module>
ball_canv = Ball(canvas, paddle_canv)
File "animationTest.py", line 26, in __init__
self.move_active()
File "animationTest.py", line 50, in move_active
self.ball_update()
File "animationTest.py", line 37, in ball_update
if self.hit_paddle(pos) == True:
File "animationTest.py", line 42, in hit_paddle
paddle_pos = self.canvas.coords(self.Paddle.pad)
AttributeError: Ball instance has no attribute 'Paddle'
Код:
from Tkinter import *
import time
import random
HEIGHT = 500
WIDTH = 800
COLOR = 'blue'
SIZE = 50
root = Tk()
canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg=COLOR)
canvas.pack()
class Ball:
def __init__(self, canvas, paddle):
self.ball = canvas.create_oval(0, 0, SIZE, SIZE, fill='black')
self.label = Label(root, text = 'score: {}')
self.speedx = 6
self.speedy = 6
self.hit_top = False
self.active = True
self.canvas = canvas
self.move_active()
def ball_update(self):
canvas.move(self.ball, self.speedx, self.speedy)
pos = canvas.coords(self.ball)
if pos[1] <= 500:
self.speedy = -6
if pos[3] <= 100:
self.hit_top = True
if pos[2] >= 800:
self.speedx = -6
if self.hit_paddle(pos) == True:
self.speedy = -6
self.speedx = random.randrange(-6,6)
def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(self.Paddle.pad)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False
def move_active(self):
if self.active:
self.ball_update()
root.after(1, self.move_active)
class Paddle:
def __init__(self, canvas):
self.pad = canvas.create_rectangle(0,0,100,10, fill='red')
canvas.bind('<Motion>', self.motion)
self.active = True
self.move_active
def motion(self, event):
'''update paddle coordinates using current mouse position'''
canvas.coords(self.pad, event.x-50, 0, event.x+50, 10)
def move_active(self):
if self.active:
self.motion()
root.after(1, self.move_active)
paddle_canv = Paddle(canvas)
ball_canv = Ball(canvas, paddle_canv)
while ball_canv.self.hit_top == False:
padddle_canv.motion()
root.update_idletasks()
root.update()
time.sleep(0.01)
root.mainloop()