【问题标题】:Stuck on Pong Game in Python卡在 Python 中的 Pong 游戏
【发布时间】:2018-11-04 00:39:34
【问题描述】:

我被这个乒乓球游戏困住了,不知道哪里出了问题。我已经为此工作了大约 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()

【问题讨论】:

    标签: python python-2.7 user-interface tkinter


    【解决方案1】:

    在你的Ball__init__ 方法中,我想你忘记将paddle 分配给self。尝试将其修改为:

    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.paddle = paddle
         self.move_active()
    

    (感谢@PRMoureu 发现展示位置问题)

    然后对hit_paddle 进行小调整以调用新分配的self.paddle 属性(注意我将self.Paddle 小写为self.paddle 以匹配__init__ 中的新行):

    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
    

    【讨论】:

    • (需要上移这个分配,在self.move_active()之前)
    • @PRMoureu 谢谢,我没有意识到这是个问题。
    【解决方案2】:

    您收到的错误“AttributeError: Ball instance has no attribute 'Paddle'”说明了一切。 Ball 类没有 Paddle 实例变量。

    要解决此问题,请将以下行添加到您的构造函数中:

    self.Paddle = paddle

    上面的行会将对 paddle 的引用作为实例变量存储在 Ball 类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多