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

pygametimerandom
SCREEN_WIDTH = SCREEN_HEIGHT = BG_COLOR=pygame.Color(())
TEXT_COLOR=pygame.Color()

MainGame():
    window=my_tank = enemyTankList=[]
    enemyTankCount=():
        ():
        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()
            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()

    ():
        ()
        ()
    (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:
                    ()


            event.type == pygame.KEYUP:
                event.key==pygame.K_UP event.key==pygame.K_LEFT event.key==pygame.K_RIGHT event.key==pygame.K_DOWN:
                    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


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

MyTank(Tank):
    ():
        EnemyTank(Tank):
    (lefttopspeed):
        .imges={
            : 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
        .flag=():
        num=random.randint()
        num == :
            num == :
            num == :
            num == :
            Bullet():
    ():
        ():
        ():
        Wall():
    ():
        ():
        Explode():
    ():
        ():
        Music():
    ():
        ():
        __name__==:
    MainGame().startGame()

报错了

Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2792楼
Python全系列/第二阶段:Python 深入与提高/模块 2794楼
Python全系列/第二阶段:Python 深入与提高/文件处理 2795楼
Python全系列/第二阶段:Python 深入与提高/模块 2798楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2799楼

"""
新增功能
    1我方坦克切换方向
    2我方坦克移动

"""
import pygamea

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])
        #初始化我方坦克
        MyGame.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("georgia", 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:
                        #切换方向
                        MainGame.my_tank.directionr='L'
                        MainGame.my_tank.move()
                        print("按下左键,坦克向左移动")
                    elif event.key == pygame.K_RIGHT:
                        # 切换方向
                        MainGame.my_tank.directionr = 'R'
                        MainGame.my_tank.move()
                        print("按下右键,坦克向右移动")
                    elif event.key == pygame.K_UP:
                        # 切换方向
                        MainGame.my_tank.directionr = 'U'
                        MainGame.my_tank.move()
                        print("按上键,坦克向上动")
                    elif event.key == pygame.K_DOWN:
                        # 切换方向
                        MainGame.my_tank.directionr = 'D'
                        MainGame.my_tank.move()
                        print("按下键,坦克向右下动")


class Tank():
    #添加距离左边left  距离上边
    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/p1tanL.gif'),
                     'R': pygame.image.load('img/p1tankR.gif')
                      }
        #方向
        self.direction='L'
        #根据当前图片的方向加载图片 surface
        self.image=self.images[self.direction]
        #根据图片获取区域
        self.rect=self.image.get_rect()
        #设置区域left  和top
        self.rect.left=left
        self.rect.top=top
        #速度 决定移动的快慢
        self.speed10

        pass

    # 移动
    def move(self):
        #判断坦克的方向进行移动
        if self.direction == 'L':
            self.rect.left -= self.speed
        elif self.direction == 'U':
            self.rect.top -= self.speed
        elif self.direction == 'D':
            self.rect.top += self.speed
        elif self.direction == 'R':
            self.rect.left += self.speed

    # 射击
    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 深入与提高/游戏开发-坦克大战 2800楼
Python全系列/第二阶段:Python 深入与提高/模块 2802楼
Python全系列/第二阶段:Python 深入与提高/文件处理 2805楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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