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

'''#新增功能 添加事件 1.点击关闭 关闭窗口 2.按下键盘,判断按下的是什么键,分别对不同的键做处理'''# 导入pygame模块import pygameSCREEN_WIDTH=700SCREEN_HEIGHT=500BG_COLOR=pygame.Color(0,0,0)# 主类class MainGame(): window=None def __init__(self): pass # 开始游戏 def startGame(self): # 加载主窗口 # 初始化窗口 pygame.display.init() # 设置窗口的大小及显示 MainGame.window=pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT]) # 设置标题的窗口 pygame.display.set_caption('坦克大战游戏测试1.03') while True: # 给窗口设置填充色 MainGame.window.fill(BG_COLOR) # 获取事件 self.getEvent() pygame.display.updata() # 结束游戏 def endGame(self): print('谢谢使用,欢迎再次使用') exit() # 获取事件 def getEvent(self): # 获取所有事件 eventList=pygame.event.get() # 遍历事件 for event in eventList: # 判断按下的键是关闭还是键盘按下 # 如果按下的是退出,关闭窗口 if event.type == pygame.QUIT: self.endGame() # 如果是键盘的按下 if event.type == pygame.KEYDOWN: # 判断按下的是上、下、左、右 if event.key == pygame.K_LEFT: print('按下左键,坦克向左移动') elif event.key == pygame.K_RIGHT: print('按下右键,坦克向右移动') elif event.key == pygame.K_UP: print('按下上键,坦克向上移动') elif event.key == pygame.K_DOWN: print('按下下键,坦克向下移动')class Tank(): def __init__(self): pass # 射击 def shot(self): pass # 展示坦克的方法 def dispayTank(self): pass# 我方坦克class MyTank(Tank): def __init__(self): pass# 敌方坦克class EnemyTank(Tank): def __init__(self): pass# 子弹类class Bullet(): def __init__(self): pass #移动 def move(self): pass #展示子弹的方法 def displayBullet(self): passclass Wall(): def __init__(self): pass #展示墙壁的方法 def displayWall(self): passclass Explode(): def __init__(self): pass #展示爆炸效果的方法 def displayExplode(self): passclass Music(): def __init__(self): pass #播放音乐 def play(self): passif __name__=='__main__': MainGame().startGame()Traceback (most recent call last): File "E:\Pycharm.Lee\mytanc\tanc__04.py", line 113, in MainGame().startGame() File "E:\Pycharm.Lee\mytanc\tanc__04.py", line 34, in startGame pygame.display.updata()AttributeError: module 'pygame.display' has no attribute 'updata'老师这直接闪退,是怎么回事

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 963楼

638c33578c0cf7fcbc2db7d9ff249c8.png

tank03 - 副本.z

老师,为什么我设置不同的速度,最后坦克遇到边缘的情况不同,
像我这个代码,速度设置30,就会出现我图片里的这种情况,如果
速度设置为10,坦克的炮筒就不会出去界面。


Python 全系列/第二阶段:Python 深入与提高/坦克大战 964楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 967楼
Python 全系列/第二阶段:Python 深入与提高/(旧)坦克大战 970楼

8b9968301066de2249af4d847aa98ca.png

老师,如果我这个图像距离左边缘的距离大于0,但小于速度的值,
那如果坦克再向左移动,左端距离减速度的值不就是小于零了吗?


Python 全系列/第二阶段:Python 深入与提高/坦克大战 971楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 972楼

from tkinter import *
from tkinter import messagebox

class Application(Frame):
    """一个经典GUI的程序的类的写法"""

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()

        self.createWidget()

    def createWidget(self):
        """创建组件"""
        self.btn01 = Button(self)
        self.btn01["text"] = '送花'
        self.btn01.pack()
        self.btn01["command"] = self.songhua

        #创建一个退出按键
        self.btnQuit = Button(self, text='退出', command=root.destory)
        self.btnQuit.pack()

        def songhua(self):
            messagebox.showinfo("送花","送你一多玫瑰花,亲亲我吧")


root = Tk()
root.title("我的第一个Gui程序")
root.geometry("600x500+0+0")
app = Application(master=root)

root.mainloop()

D:\Users\asus\PycharmProjects\gui\venv\Scripts\python.exe D:/Users/asus/PycharmProjects/gui/my01.01.py
Traceback (most recent call last):
  File "D:/Users/asus/PycharmProjects/gui/my01.01.py", line 34, in <module>
    app = Application(master=root)
  File "D:/Users/asus/PycharmProjects/gui/my01.01.py", line 13, in __init__
    self.createWidget()
  File "D:/Users/asus/PycharmProjects/gui/my01.01.py", line 21, in createWidget
    self.btn01["command"] = self.songhua
AttributeError: 'Application' object has no attribute 'songhua'

Process finished with exit code 1

QQ截图20200402222047.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 975楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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