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

pygame,time
random
_display=pygame.display
COLOR_BLACK=pygame.Color(,,)
COLOR_RED=pygame.Color(,,)
():
    window=TANK_P1=SCREEN_HEIGHT=SCREEN_WIDTH=EnemyTank_list=[]
    EnemyTank_count=():
        ():
        _display.init()
        MainGame.window=_display.set_mode([MainGame.SCREEN_WIDTH,MainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank(, )
        .createEnemyTank()
        _display.set_caption()

        :
            time.sleep()
            MainGame.window.fill(COLOR_BLACK)
            .getEvent()
            MainGame.window.blit(.getTextface(%),(,))

            MainGame.TANK_P1.displayTank()

            .displayEnemyTank()
            MainGame.TANK_P1.stop == :
                MainGame.TANK_P1.move()
            _display.update()


    ():
        top = i (MainGame.EnemyTank_count):
            left = random.randint(, )
            speed = random.randint(, )
            eTank = EnemyTank(left, top, speed)
            MainGame.EnemyTank_list.append(eTank)


   ():
        eTank MainGame.EnemyTank_list:
            eTank.displayTank()
            eTank.randMove()

    ():
        ()
        ()

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

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


             event.type==pygame.KEYUP:
                    event.key==pygame.K_LEFT event.key==pygame.K_RIGHT \
                        event.key == pygame.K_UP event.key==pygame.K_DOWN:
                     MainGame.TANK_P1.stop=():
    (,left,top):
        .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
        .stop = .speed=():
        .image=.images[.direction]
        MainGame.window.blit(.image,.rect)


():
        .direction==:
            .rect.left>:
                .rect.left-=.speed
        .direction==:
            .rect.left+.rect.width<MainGame.SCREEN_WIDTH:
                .rect.left+=.speed
        .direction==:
            .rect.top > :
                 .rect.top-=.speed
        .direction==:
            .rect.top + .rect.height< MainGame.SCREEN_HEIGHT:
                .rect.top += .speed

    ():
        ():
        ():
        (Tank):
    ():
        ():
        (Tank):

    (,left,top,speed):
        .images={
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load()
        }
        .direction=.randDirection()
        .image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .speed=speed
        .stop=.step=():
            num=random.randint(,)
            num==:
                 num == :
                num == :
                num == :
                ():
        .step<=:
            .direction=.randDirection()
            .step=:
            .move()
            .step-=():
        ():
    ():
        ():
        ():
        ():
        ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        __name__==:
    MainGame().startGame()


报错
"C:\Program Files (x86)\Python36-32\python.exe" C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py
pygame 2.0.1 (SDL 2.0.14, Python 3.6.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 284, in <module>
    MainGame().startGame()
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 28, in startGame
    self.createEnemyTank()
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 59, in createEnemyTank
    eTank = EnemyTank(left, top, speed)
  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 208, in __init__
    self.image=self.images[self.direction]
KeyError: None

Process finished with exit code 1


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 331楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 332楼
Python 全系列/第二阶段:Python 深入与提高/模块 333楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 334楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 335楼

#主要包括坦克的创建,显示,移动及射击的功能

import pygame
COLOR_BLUE=pygame.Color(0,0,0)
COLOR_red=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([700,600])
    #初始化我方坦克
       MainGame.my_tank=Tank(350,300)
    #设置窗口标题
       pygame.display.set_caption("坦克大战")
       while True:
         MainGame.window.fill(COLOR_BLUE)
         self.eventlist()

         MainGame.window.blit(self.getTextSurface("剩余敌方坦克数量%d"%6),(5,5))
         #调用坦克的显示方法
         MainGame.my_tank.displayTank()
         pygame.display.update()
#结束游戏
    def endGame(self):
        print("谢谢使用,欢迎下次使用")
        exit()
    def getTextSurface(self,text):
        pygame.font.init()
        font=pygame.font.SysFont("kaiti",18)
        testSurface=font.render(text,True,COLOR_red)
        return testSurface
    def eventlist(self):
        enentlists=pygame.event.get()
        for event in enentlists:
            if event.type==pygame.QUIT:
                self.endGame()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_UP:
                    print("按下上键,坦克向上移动")
                if event.key == pygame.K_DOWN:
                    print("按下下键,坦克向下移动")
                if event.key == pygame.K_LEFT:
                    print("按下左键,坦克向左移动")
                if event.key == pygame.K_RIGHT:
                    print("按下右键,坦克向右移动")


class Tank():
    #添加距离左边left,距离上边top
    def __init__(self,left,top):
    #保存加载的图片
       self.images={"U":pygame.image.load("imgs/p1tankU.gif"),
                    "D": pygame.image.load("imgs/p1tankD.gif"),
                    "L": pygame.image.load("imgs/p1tankL.gif"),
                    "R": pygame.image.load("imgs/p1tankR.gif"),
                    }
    #(属性)方向
       self.direction="U"
    #根据当前图片的方向获取图片
       self.image=self.images[self.direction]
    #根据图片获取区域
       self.rect=self.image.get_rect()
    #设置区域的left和top
       self.rect.left = left
       self.rect.top = top

    #坦克的移动方法
    def moveTank(self):
        pass
#碰撞墙壁的方法
    def hitwalls(self):
        pass
#展示坦克
    def displayTank(self):
      #获取展示的对象
        self.image=self.images[self.direction]
      #调用blit方法显示
        MainGame.window.blit(self.image,self.rect)
#我方坦克类继承坦克类,主要包括创建与敌方坦克的碰撞方法
class MyTank(Tank):
    def __init__(self):
        pass
#碰撞敌方坦克的方法
    def hitEnemyTank(self):
        pass

#敌方坦克继承坦克类,主要包括创建于我方坦克的碰撞方法
class EnemyTank(Tank):
    def __init__(self):
        pass
    def hitMyTank(self):
        pass

#子弹类:主要包括子弹的创建,显示及移动的功能
class Bullet():
    def __init__(self):
        pass
#子弹的移动方法
    def bulletMove(self):
        pass
    #展示子弹的方法
    def displaybullet(self):
        pass
    #我方子弹碰撞敌方坦克的方法
    def hitEnemy(self):
        pass
    #敌方子弹与我方坦克的碰撞方法
    def hitMyTank(self):
        pass
    #子弹与墙壁的碰撞
    def hitwalls(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()

43.png


运行代码的时候,就出现了这个问题,是什么问题呢?

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 337楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 338楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 342楼

pygame,time
random
_display=pygame.display
COLOR_BLACK=pygame.Color(,,)
COLOR_RED=pygame.Color(,,)
():
    window=TANK_P1=SCREEN_HEIGHT=SCREEN_WIDTH=EnemyTank_list=[]
    EnemyTank_count=():
        ():
        _display.init()
        MainGame.window=_display.set_mode([MainGame.SCREEN_WIDTH,MainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank(, )
        .createEnemyTank()
        _display.set_caption()

        :
            time.sleep()
            MainGame.window.fill(COLOR_BLACK)
            .getEvent()
            MainGame.window.blit(.getTextface(%),(,))

            MainGame.TANK_P1.displayTank()

            .displayEnemyTank()
            MainGame.TANK_P1.stop == :
                MainGame.TANK_P1.move()
            _display.update()


    ():
        top = i (MainGame.EnemyTank_count):
            left = random.randint(, )
            speed = random.randint(, )
            eTank = EnemyTank(left, top, speed)
            MainGame.EnemyTank_list.append(eTank)


   ():
        eTank MainGame.EnemyTank_list:
            eTank.displayTank()
            eTank.randMove()

    ():
        ()
        ()

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

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


             event.type==pygame.KEYUP:
                    event.key==pygame.K_LEFT event.key==pygame.K_RIGHT \
                        event.key == pygame.K_UP event.key==pygame.K_DOWN:
                     MainGame.TANK_P1.stop=():
    (,left,top):
        .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
        .stop = .speed=():
        .image=.images[.direction]
        MainGame.window.blit(.image,.rect)


():
        .direction==:
            .rect.left>:
                .rect.left-=.speed
        .direction==:
            .rect.left+.rect.width<MainGame.SCREEN_WIDTH:
                .rect.left+=.speed
        .direction==:
            .rect.top > :
                 .rect.top-=.speed
        .direction==:
            .rect.top + .rect.height< MainGame.SCREEN_HEIGHT:
                .rect.top += .speed

    ():
        ():
        ():
        (Tank):
    ():
        ():
        (Tank):

    (,left,top,speed):
        .images={
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load()
        }
        .direction=.randDirection()
        .image=.images[.direction]
        .rect=.image.get_rect()
        .rect.left=left
        .rect.top=top
        .speed=speed
        .stop=.step=():
            num=random.randint(,)
            num==:
                 num == :
                num == :
                num == :
                ():
        .step<=:
            .direction=.randDirection()
            .step=:
            .move()
            .step-=():
        ():
    ():
        ():
        ():
        ():
        ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        ():
    ():
        ():
        __name__==:
    MainGame().startGame()



报错

"C:\Program Files (x86)\Python36-32\python.exe" C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py

pygame 2.0.1 (SDL 2.0.14, Python 3.6.5)

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

Traceback (most recent call last):

  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 284, in <module>

    MainGame().startGame()

  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 28, in startGame

    self.createEnemyTank()

  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 59, in createEnemyTank

    eTank = EnemyTank(left, top, speed)

  File "C:/Users/Administrator/Desktop/python课件/坦克游戏/代码/test/tank08.py", line 208, in __init__

    self.image=self.images[self.direction]

KeyError: None


Process finished with exit code 1



老师,帮我看看是什么原因


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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