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

pygame


_display = pygame.display
color_black = pygame.Color()
color_red = pygame.Color()
version = maingame():
    window = Screen_width = Screen_height = TANK_P1 = ():
        _display.init()
        maingame.window = _display.set_mode([maingame.Screen_widthmaingame.Screen_height])
        maingame.TANK_P1 = Tank()
        _display.set_caption(+ version)
        .getTextSurface()
        :
            maingame.window.fill(color_black)
            .getEvent()
            maingame.window.blit(.getTextSurface(% )())
            maingame.TANK_P1.displayTank()
            maingame.TANK_P1.move()
            _display.update()

    ():
        eventList = pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endgame()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                    ()
                    maingame.TANK_P1.direction = event.key == pygame.K_RIGHT:
                    ()
                    maingame.TANK_P1.direction = event.key == pygame.K_UP:
                    ()
                    maingame.TANK_P1.direction = event.key == pygame.K_DOWN:
                    ()
                    maingame.TANK_P1.direction = event.key == pygame.K_SPACE:
                    ()

    (text=):
        pygame.font.init()
        = pygame.font.get_fonts()
        font = pygame.font.SysFont()
        textSurface = font.render(textcolor_red)
        textSurface
        (fontList)

    ():
        ()
        ()


Tank:
    TANK_P1 = (lefttop):
        .images = {
            : pygame.image.load(): pygame.image.load(): pygame.image.load(): pygame.image.load()
        }
        .direction = .image = .images[.direction]
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top

        .speed = ():
        .image = .images[.direction]
        maingame.window.blit(.image.rect)

    ():

        .direction == :
            .rect.left > :
                .rect.left -= .speed
        .direction == :
            .rect.top > :
                .rect.top -= .speed
        .direction == :
            .rect.top + .rect.height < maingame.Screen_width:
                .rect.top += .speed
            .direction == :
                .rect.left + .rect.height < maingame.Screen_height:
                    .rect.left += .speed

    ():

为什么边界这卡不住 老师!?

Python 全系列/第二阶段:Python 深入与提高/坦克大战 1002楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 1003楼

老师你好我把功能的代码写完了,运行时也没报错,但是里面的功能用不了

"""
开发画图软件的菜单
"""

from tkinter.filedialog import *
from tkinter.colorchooser import *

# 窗口的高度和宽度
win_width = 900
win_height = 450

class Application(Frame):
    def __init__(self, master=None, bgcolor="#000000"):
        super().__init__(master)
        self.master = master
        self.bgcolor = bgcolor
        self.x = 0
        self.y = 0
        self.fgcolor = "#ff0000"
        self.lastDraw = 0
        self.starDrawFlage = False
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建绘图区
        self.drawpad = Canvas(root, width=win_width, height=win_height*0.9, bg=self.bgcolor)
        self.drawpad.pack()
        # 创建按钮
        btn_start = Button(root, text="开始", name="start")
        btn_start.pack(side="left", padx="10")
        btn_pen = Button(root, text="画笔", name="pen")
        btn_pen.pack(side="left", padx="10")
        btn_rect = Button(root, text="矩形", name="rect")
        btn_rect.pack(side="left", padx="10")
        btn_clear = Button(root, text="清屏", name="clear")
        btn_clear.pack(side="left", padx="10")
        btn_erasor = Button(root, text="橡皮檫", name="erasor")
        btn_erasor.pack(side="left", padx="10")
        btn_line = Button(root, text="直线", name="line")
        btn_line.pack(side="left", padx="10")
        btn_lineArrow = Button(root, text="箭头直线", name="lineArrow")
        btn_lineArrow.pack(side="left", padx="10")
        btn_color = Button(root, text="颜色", name="color")
        btn_color.pack(side="left", padx="10")

        # 事件处理
        btn_pen.bind_class("<Button>", "<1>", self.eventManager)
        self.drawpad.bind("ButtonRelease-1", self.stopDraw)



    def eventManager(self, event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.drawpad.bind("<B1-Motion>", self.myline)
        elif name == "lineArrow":
            self.drawpad.bind("<B1-Motion>", self.mylineArrow)
        elif name =="rect":
            self.drawpad.bind("<B1-Motion>", self.myRect)
        elif name == "pen":
            self.drawpad.bind("<B1-Motion>", self.myPen)
    def stopDraw(self, event):
        self.starDrawFlage = False
        self.lastDraw = 0

    def starDraw(self, event):
        self.drawpad.delete(self.lastDraw)

        if not self.starDrawFlage:
            self.starDrawFlage = True
            self.x = event.x
            self.y = event.y


    def myline(self, event):
        self.starDraw(event)
        self.lastDraw = \
            self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)
    def mylineArrow(self, event):
        self.starDraw(event)
        self.lastDraw = \
            self.drawpad.create_line(self.x, self.y, event.x, event.y, arrow=LAST, fill=self.fgcolor)
    def myRect(self, event):
        self.starDraw(event)
        self.lastDraw = \
            self.drawpad.create_line(self.x, self.y, event.x, event.y, outline=self.fgcolor)
    def myPen(self, event):
        self.starDraw(event)
        self.lastDraw = \
            self.drawpad.create_line(self.x, self.y, event.x, event.y, fill=self.fgcolor)
        self.x = event.x
        self.y = event.y


if __name__ == "__main__":
    root = Tk()
    root.geometry(str(win_width)+"x"+str(win_height)+"+200+300")
    root.title("百战程序员的画图软件")
    app = Application(master=root)
    root.mainloop()


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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