陷入Python的Pong游戏

时间:2018-05-24 17:40:09

标签: python python-2.7 user-interface tkinter

我被困在这场乒乓球比赛中,无法弄清楚出了什么问题。我已经工作了大约2个星期,一旦我开始制作可用的桨,它就给我带来了一百万个错误,我无法从那里解决它。我一直试图摆脱错误大约一周。

错误:

<div class="card-deck mb-3 text-center">
  <div *ngFor="let index of [0, 1, 2, 3]" class="card mb-4">
    <div class="card-header">
      Destination {{ index + 1 }}
    </div>
    <div class="card-body">
      <div class="input-group mb-3">
        <select class="form-control" [(ngModel)]="selectedPlanets[index]">
          <option *ngFor="let planet of planets" [value]="planet.name">
            {{planet.name}}
          </option>
        </select>
      </div>
    </div>
  </div>
</div>

代码:

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'

2 个答案:

答案 0 :(得分:2)

在您的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以匹配{{1}中的新行}}):

__init__

答案 1 :(得分:1)

您获得的错误&#34;属性错误:Ball实例没有属性&#39; Paddle&#39;&#34;这一切都说明了。 Ball类没有Paddle实例变量。

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

self.Paddle = paddle

上面的行将把paddle的引用存储为Ball类中的实例变量。

相关问题