会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132370个问题

老师帮忙看下什么问题,视频看了三四遍了, 和视频里老师讲的代码一一对应了,还是找不到问题所在啊 

"""
新增功能
     加载我方坦克

"""
import pygame
SCREEN_WIDTH=700
SCREEN_HEIGHT=500
BG_COLOR=pygame.Color(0,0,0)
TEXT_COLOR=pygame.Color(255,0,0)
class MainGame():
    window = None
    my_tank = None
    def __init__(self):
        pass
    #开始游戏
    def startGame(self):
        # 加载主窗口
        pygame.display.init()
        # 设置窗口的大小及显示
        MainGame.window=pygame.display.set_mode([SCREEN_HEIGHT,SCREEN_HEIGHT])
        # 初始化我方坦克
        MainGame.my_tank = Tank(350, 250)
        #设置窗口的标题

        pygame.display.set_caption("坦克大战1.03")
        while True:
            # 给窗口设置填充色
            MainGame.window.fill(BG_COLOR)
            # 获取事件
            self.getEvent()
            # 绘制文字的方法
            MainGame.window.blit(self.getTextSuface('敌方坦克剩余数量%d'%6),(10,10))
            # 调用坦克显示的方法
            MainGame.my_tank.displayTank()
            pygame.display.update()

    #结束游戏
    def endGame(self):
        print("谢谢使用,欢迎再来")
        exit()
    #左上角文字的绘制
    def getTextSuface(self,text):
        #初始化字体模块
        pygame.font.init()
        #查看所有的字体名称
        #print(pygame.font.get_fonts())
        #获取字体Font对象
        font=pygame.font.SysFont('microsoftyaheimicrosoftyaheiuibold',18)
        #绘制文字信息
        textSurface=font.render(text,True,TEXT_COLOR)
        return textSurface


    #获取事件
    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 ():
    # 添加距离左边left,添加上部top
    def __init__(self, left, top):
       #保存加载的图片
        self.images={
           'U': pygame.image.load('img/p1tankU.gif'),
           'D': pygame.image.load('img/p1tankD.gif'),
           'L': pygame.image.load('img/p1tankL.gif'),
           'R': pygame.image.load('img/p1tankR.gif'),
        }
        #方向
        self.direction = 'L'
        # 根据当前图片的方向获取图片
        self.image = self.images[self.direction]
        # 根据图片获取区域
        self.rect = self.image.get_rect()
        # 设置区域的left 和 top
        self.rect.left = left
        self.rect.top = top



    # 移动
    def move(self):
        pass


    # 射击
    def shot(self):
        pass
    # 展示坦克的方法
    def display(self):
        # 获取展示的对象
        self.image = self.images[self.direction]
        # 调用blit方法展示
        MainGame.window.blit(self.image, self.rect)

#我方坦克
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):
        pass


class Wall():
    def __init__(self):
        pass
     #展示墙壁的方法
    def displayWall(self):
        pass
class Explode():
    def __init__(self):
        pass
    #展示爆炸效果的方法
    def displayExplode(self):
        pass
class Music():
    def __init__(self):
        pass
    #播放音乐
    def play(self):
        pass
if __name__=="__main__":
    MainGame().startGame()
    #MainGame().getTextSuface()


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2207楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2208楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2209楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2210楼

1.png

问题在图片里,麻烦老师帮忙解答

Python 全系列/第二阶段:Python 深入与提高/异常机制 2211楼
Python 全系列/第二阶段:Python 深入与提高/(旧)坦克大战 2212楼

代码:

tkinter *
tkinter.filedialog *  tkinter.colorchooser * tkinter.filedialog *

Application(Frame):

    (master=):
        ().(master)  .master = master
        .textpad = .pack()
        .createWidget()

    ():
        menubar = Menu(root)

        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        menubar.add_cascade(==menuFile)
        menubar.add_cascade(==menuEdit)
        menubar.add_cascade(==menuHelp)

        menuFile.add_command(===.test)
        menuFile.add_command(===.openfile)
        menuFile.add_command(===.savefile)

        menuFile.add_separator()  menuFile.add_command(===.exit)

        root[] = menubar

        .textpad = Text(root==)
        .textpad.pack()

        .contextMenu = Menu(root)
        .contextMenu.add_command(==.openAskColor)

        root.bind(.createContextMenu)

    ():
        .textpad.delete()  (askopenfilename(=)=) f:  .textpad.insert(INSERTf.read())
            .filename = f.name

    ():
        (.filename) f:
            c = .textpad.get(END)
            f.write(c)

    ():
        root.quit()

    ():
        ():
        s1 = askcolor(==)
        root.config(=s1[])

    (event):
        .contextMenu.post(event.x_rootevent.y_root)


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


控制台:

C:\Users\微软\AppData\Local\Programs\Python\Python37\python.exe C:/Users/微软/pycharm_execrise/pythonProject/GUI/note.py

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\微软\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__

    return self.func(*args)

  File "C:/Users/微软/pycharm_execrise/pythonProject/GUI/note.py", line 62, in openfile

    self.textpad.insert(INSERT, f.read())

  File "C:\Users\微软\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 322, in decode

    (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte


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

#开发一款画图软件
from tkinter import *
from tkinter.filedialog import *
from tkinter.colorchooser import *

win_width=600
win_height=500
class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.x = 0
        self.y = 0
        self.fg = "red"
        self.pack()
        self.createWidget()

    def createWidget(self):

        self.drawpad = Canvas(root,width=win_width,height=win_height*0.9,bg="white")
        self.drawpad.pack()

        btn_star = Button(root,text="strar",name = "star").pack(side="left",padx=10)
        btn_pen = Button(root,text="pen",name="pen").pack(side="left",padx=10)
        btn_rect = Button(root,text="rect",name="rect").pack(side="left",padx=10)
        btn_clear = Button(root,text="clear",name="clear").pack(side="left",padx=10)
        btn_erasor = Button(root,text="erasor",name="erasor").pack(side="left",padx=10)
        btn_line = Button(root, text="line", name="line").pack(side="left", padx=10)
        btn_lineArrow = Button(root, text="lineArrow", name="lineArrow").pack(side="left", padx=10)
        btn_color = Button(root, text="color", name="color").pack(side="left", padx=10)

        btn_pen.bind_class("Button",'<1>',self.eventManager)

    def eventManager(self,event):
        name = event.widget.winfo_name()
        print(name)
        if name=="line":
            self.drawpad.bind("<B1-Motion>",self.myline)


    def myline(self,event):

        self.drawpad.create_line(self.x,self.y,event.x,event.y,fill=self.fg)



if __name__=='__main__':
    root = Tk()
    root.geometry(str(win_width)+"x"+str(win_height))
    app = Application(master=root)
    root.title("简易画图软件")
    root.mainloop()

这里出不来直线咋回事呢

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2218楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 2219楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 2220楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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