会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133940个问题
Python 全系列/第二阶段:Python 深入与提高/文件处理 632楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 634楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 635楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 637楼

tkinter *
tkinter.colorchooser *


win_width=win_height=Application(Frame):

    (master=bgcolor=):
        ().(master)
        .master = master
        .bgcolor = bgcolor
        .x=.y=.fgcolor= .pack()
        .createWidget()

    ():
        drawpad = Canvas (root=win_width=win_height*=.bgcolor)
        drawpad.pack()

        btn_start = Button(root==)
        btn_start.pack(==)
        btn_pen = Button(root==)
        btn_pen.pack(==)
        btn_rect = Button(root==)
        btn_rect.pack(==)
        btn_clear = Button(root==)
        btn_clear.pack(==)
        btn_erasor = Button(root==)
        btn_erasor.pack(==)
        btn_line = Button(root==)
        btn_line.pack(==)
        btn_lineArror = Button(root==)
        btn_lineArror.pack(==)
        btn_color = Button(root==)
        btn_color.pack(==)

        btn_pen.bind_class(.eventManager)

    (event):
        name = event.widget.winfo_name()
        (name)
        name==:
            .drawpad.bind(.myline)

    (event):
            .drawpad.create_line(.x.yevent.xevent.y=.fgcolor)


__name__ == :
    root = Tk()
    root.geometry((win_width)++(win_height)+)
    root.title()
    app = Application(=root)
    root.mainloop()

老师,代码没有显示错误,为啥在添加方法后(eventManager,myline),按钮不能使用了呢?

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__

    return self.func(*args)

  File "C:/Users/16054/PycharmProjects/GUI/maspaint/work1.py", line 52, in eventManager

    self.drawpad.bind("<B1-Motion>",self.myline)

AttributeError: 'NoneType' object has no attribute 'bind'


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 639楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 640楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 642楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 644楼

#坦克碰撞方法
def tank_hit_tank(self,tank):
    print('tank   ',tank.live)
    if self.live and tank.live:
        if collide_rect(self,tank):
            self.rect.left = self.old_left
            self.rect.top = self.old_top
#我方坦克
if MainGame.my_tank and MainGame.my_tank.live:
    MainGame.my_tank.display_tank()
else:
    MainGame.my_tank = None
if MainGame.my_tank and MainGame.my_tank.live:
    if MainGame.my_tank.remove:
        MainGame.my_tank.move()
        MainGame.my_tank.tank_hit_wall()
        for enemy in MainGame.enemy_tanks:
            print("检测我方是否碰撞敌方坦克")
            MainGame.my_tank.tank_hit_tank(enemy)
#敌方坦克
def display_enermy_tank(self):
    for enemy_tank in self.enemy_tanks:
        if enemy_tank.live:
            enemy_tank.display_tank()
            enemy_tank.rand_move()
            enemy_tank.tank_hit_wall()
            print("检测敌方是否碰撞我方坦克")
            enemy_tank.tank_hit_tank(MainGame.my_tank)
            enemy_bullet =enemy_tank.shot()
            if enemy_bullet:
                MainGame.enemy_bullet_list.append(enemy_bullet)
        else:
            MainGame.enemy_tanks.remove(enemy_tank)

老师,这节课讲的坦克的碰撞,上面是几段相关代码当我方坦克被击败后,代码报错live不存在,我试了改成if self.live and tank就不报错了,看问题好像出现在我方坦克被击败后,my_tank置为None,所以获取他的live失败,但是不明白,为什么敌方坦克被击败后也删除了,但是敌方坦克都被击败后也不报错。

我模拟写了一段代码:这个结果我也不是很明白,希望帮忙解答

Test:
    (a):
        .a = a
        .live = ():
        (.a)
t_list = []
i ():
    t = Test(i)
    t_list.append(t)
(t_list)
n t_list:
    n.prin()
    n.live:
        ()
    :
        (n)
        t_list.remove(n)
    (n.live)
(t_list)

F:\python_env\tank_env\Scripts\python.exe F:/python_env/tank_env/test.py

[<__main__.Test object at 0x000001FC6A6B3FA0>, <__main__.Test object at 0x000001FC6A6B1C90>, <__main__.Test object at 0x000001FC6A6B1B40>, <__main__.Test object at 0x000001FC6A6B2AD0>]

0

<__main__.Test object at 0x000001FC6A6B3FA0>

False

2

<__main__.Test object at 0x000001FC6A6B1B40>

False

[<__main__.Test object at 0x000001FC6A6B1C90>, <__main__.Test object at 0x000001FC6A6B2AD0>]


Process finished with exit code 0


Python 全系列/第二阶段:Python 深入与提高/坦克大战 645楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637