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

pygame
time
random
SCREEN_WIDTH=SCREEN_HEIGHT=BG_COLOR=pygame.Color()
TEXT_COLOR=pygame.Color()


MainGame():
    window=my_tank=enemyTanklist=[]
    enemyTankCount=Bullet_list=[]
    ():
        ():
        pygame.display.init()
        MainGame.window = pygame.display.set_mode([SCREEN_WIDTHSCREEN_HEIGHT])
        MainGame.my_tank=Tank()
        .createEnemyTank()
        pygame.display.set_caption()
        :
            time.sleep()
            MainGame.window.fill(BG_COLOR)
            .getEvent()
            MainGame.window.blit(.getTextSurface(%(MainGame.enemyTanklist))())
            MainGame.my_tank.displayTank()
            .blitEnemyTank()
            MainGame.my_tank.stop:
                MainGame.my_tank.move()
            .blitBullet()
            pygame.display.update()

    ():
        top=i (MainGame.enemyTankCount):
            left = random.randint()
            speed = random.randint()
            enemy=EnemyTank(lefttopspeed)
            MainGame.enemyTanklist.append(enemy)

    ():
        enemyTank MainGame.enemyTanklist:
            enemyTank.displayTank()
            enemyTank.move()
            enemyTank.randMove()

    ():
        bullet MainGame.Bullet_list:
            bullet.displyBullet()

    ():
        ()
        ()

    (text):
        pygame.font.init()
        font = pygame.font.SysFont()
        textSurface = font.render(textTEXT_COLOR)
        textSurface

    ():
        eventList = pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endGame()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                    MainGame.my_tank.direction=MainGame.my_tank.stop=()
                event.key == pygame.K_RIGHT:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key == pygame.K_UP:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key == pygame.K_DOWN:
                    MainGame.my_tank.direction = MainGame.my_tank.stop = ()
                event.key ==pygame.K_SPACE:
                    ()
                    m = Bullet(MainGame.my_tank)
                    MainGame.Bullet_list.append(m)
                event.type==pygame.KEYUP:
                event.key ==pygame.K_UP event.key==pygame.K_LEFT event.key==pygame.K_DOWN event.key==pygame.K_RIGHT:
                    MainGame.my_tank.stop=Tank():
    (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=.stop=():
        .direction ==:
            .rect.left > :
                .rect.left-=.speed
        .direction ==:
            .rect.top>:
                .rect.top-=.speed
        .direction ==:
            .rect.top+.rect.height<SCREEN_HEIGHT:
                .rect.top+=.speed
        .direction ==:
            .rect.left+.rect.height < SCREEN_WIDTH:
                .rect.left+=.speed

    ():
        Bullet()

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

MyTank(Tank):
    ():
        EnemyTank(Tank):
    (lefttopspeed):
        .images={
            : pygame.image.load(): pygame.image.load(): pygame.image.load(): pygame.image.load()
        }

        .direction=.randomDirection()
        .image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .speed=speed
        .flag=.step = ():
        num=random.randint()
        num==:
            num==:
            num==:
            num==:
            ():
        .step <=:
            .direction=.randomDirection()
            .step=random.randint()
        :
            .move()
            .step-=Bullet():
    ():
        .image = pygame.image.load()
        .direction = tank.direction
        .rect = .image.get_rect()
        .direction == :
            .rect.left = tank.left + tank.rect.width / - .rect.width / .rect.top = tank.top - .rect.height
        .direction == :
            .rect.left = tank.rect.left - .rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .direction == :
            .rect.left = tank.rect.left + tank.rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.height
        .direction == :
            .rect.left = tank.rect.left + tank.rect.width
            .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .speed = random.randint(())

    ():
        ():
        MainGame.window.blit(.image.self.rect)


Wall():
    ():
        ():
        Explode():
    ():
        ():
        Music():
    ():
        ():
        __name__== :
    MainGame().startGame()

F:\Python\pythonProject\Tank\venv\Scripts\python.exe F:/Python/pythonProject/Tank/13.py

pygame 2.0.1 (SDL 2.0.14, Python 3.9.1)

Hello from the pygame community. https://www.pygame.org/contribute.html

发射子弹

Traceback (most recent call last):

  File "F:\Python\pythonProject\Tank\13.py", line 329, in <module>

    MainGame().startGame()

  File "F:\Python\pythonProject\Tank\13.py", line 52, in startGame

    self.getEvent()

  File "F:\Python\pythonProject\Tank\13.py", line 143, in getEvent

    m = Bullet(MainGame.my_tank)

TypeError: __init__() takes 1 positional argument but 2 were given


Process finished with exit code 1

请问老师我哪里多给参数了?

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 865楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 868楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 869楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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