Antony Tang 2021-03-21 14:46 采纳率: 80%
浏览 46
已采纳

python tkinter ,画布反弹球小游戏,应该怎么改?

有哪位大佬能告诉我怎么修改吗?

源代码如下,注释为解释器的报错。

from tkinter import *
from random import *
import time

class Ball:
    def _init_(self,canvas,color,winW,winH,racket):
        self.canvas =canvas
        self.racket =racket
        self.id =canvas.create_oval(0,0,20,20,fill=color)
        self.canvas.move(self.id,winW/2,winH/2)
        startPos=[-4,-3,-2,-1,1,2,3,4]
        sjuffle(starPos)
        self.x =startPos[0]
        self.y =-step
        self.notTouchBottom =True
    def hitRacket(self,ballPos):
        racketPos =self.canvas.coords(self.racket.id)
        if ballPos[2] >=racketPos[0] and ballPos[0] <= racketPos[2]:
            if ballPos[3] >= racketPos[1] and ballPos[3] <= racketPos[3]:
                return True
        return False
    def ballMove(self):
        self.canvas.move(self.id,self.x,self.y)
        ballPos =self.canvas.coord(self.id)
        if ballPos[0] <=0:
            self.x =step
        if ballPos[1] <=0:
            self.y =step
        if ballPos[2] >= winW:
            self.x =-step
        if ballPos[3] >= winH:
            self.y =-step
        if self.hitRacket(ballPos) == True:
            self.y = -step
        if ballPos[3] >= winH:
            self.notTouchBottom = False
class Racket:
    def _init_(self,canvas,color):
        self.canvas =canvas
        self.id =canvas.creat_rectange(0,0,100,15,fill=color)
        self.canvas.move(self.id,270,400)
        self.x =0
        self.canvas.bind_all('<KrypPress-Right>',self.moveRight)
        self.canvas.bind_all('<KeyPress-Left>',sef,moveLeft)
    def racktMove(self):
        self.canvas.move(self.id,self.x,0)
        racketPos =self.canvas,coords(self.id)
        if racketPos[0] <= 0:
            self.x=0
        elif racketPos[2] >= winW:
            self.x = 0
    def moveLeft(self,event):
        self.x =-3
    def moveRight(slef,event):
        self.x =3
winW = 640
winH =480
step =3
speed =0.01
tk=Tk()
tk.title('Bouncing Ball')
tk.wm_attributes('-topmost',1)
canvas =Canvas(tk,width=winW,height=winH)
canvas.pack()
tk.update()
racket =Racket(canvas,'purple','purple')
ball =Ball(canvas,'yellow',winW,winH,racket)
while ball.notTouchBottom:
    try:
        ball.ballMove()
    except:
        print('单机关闭按钮终止程序运行')
        break
    racket.racketMove()
    tk.update()
    time.sleep(speed)

#line 66, in <module>
#    racket =Racket(canvas,'purple','purple')
#TypeError: Racket() takes no arguments

希望能得到各位大佬的帮助。

  • 写回答

1条回答 默认 最新

  • m0_50244597 2021-03-21 18:24
    关注

    是__init__而不是_init_

    旁边跟的是两个_

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像
  • ¥15 如何用Matlab求Pearcey函数的数值积分?
  • ¥15 一个简单的函数定义问题,输出结果是相反的,怎么回事呢?
  • ¥15 页面加载报错,不知道怎么处理
  • ¥20 vue-draggable-resizable选中状态保持
  • ¥15 flink cdc读取SqlServer2016