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

import pygame

BG_COLOR=pygame.Color(255,255,255)

SCREEN_WIDTH=700

SCREEN_HEIGHT=500

TEXT_COLOR=pygame.color(255,0,0)

class Tank:

    """

    坦克类

    """

    def __init__(self) -> None:

        pass

    def display_tank(self) -> None:

        """

        显示坦克

        """

        pass

    def move(self) ->None:

        """

        坦克移动

        """

        pass

    def shot(self) ->None:

        """

        坦克的射击

        """

        pass

class Mytank(Tank):

    """

    我方坦克类

    """

    def __init__(self) -> None:

        pass

class EnemyTank(Tank):

    """

    我方坦克类

    """

    def __init__(self) -> None:

        pass

class Mytank(Tank):

    """

    我方坦克类

    """

    def __init__(self) -> None:

        pass

class Bullet:

    """

    子弹类

    """

    def __init__(self) -> None:

        pass

    def display_bullet(self) ->None:

        """

        显示子弹

        """

        pass

    def move(self) ->None:

        """

        子弹的移动

        """

        pass

class Wall:

    """

    墙壁类

    """

    def __init__(self) -> None:

        pass

    def display_wall(self) -> None:

        """

        显示墙壁

        """

        pass

class Explode:

    """

    爆炸效果类

    """

    def __init__(self) -> None:

        pass

    def display_explode(self) -> None:

        """

        显示爆炸效果

        """

        pass

class Music:

    """

    音效类

    """

    def __init__(self) -> None:

        pass

    def play_music(self) -> None:

        """

        播放音效

        """

        pass

class MainGame:

    """

    墙壁类

    """    

    #游戏主窗口对象

    windou=None

    def __init__(self) -> None:

        pass

    def start_game(self) -> None:

        """

        开始游戏

        """

        pass

        #初始化窗口

        pygame.display.init()

        #创建一个游戏窗口

        MainGame.windou=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

        #刷新窗口

        pygame.display.set_caption("坦克大战1.0")

        while True:

            #给窗口填充颜色

            MainGame.windou.fill(BG_COLOR)

            #增加提示文字

            #1.增加文字内容

            num=6

            text=self.get_text_surface(f"敌方坦克的剩余数量{num}")

            #2.如何把文字加上

            MainGame.windou.blit(text,(10,10))

            pygame.display.update()

    def get_text_surface(self,text:str) -> None:

        """

        获取文字的图片

        """

        #初始化字体模块

        pygame.font.init()

        #创建字体

        font=pygame.font.SysFont("kaiti",18)

        #绘制文字信息

        text_surface=font.render(text,True,TEXT_COLOR)

        #将绘制的信息返回

        return text_surface

    def end_game(self) -> None:

        """

        游戏结束

        """

if __name__=="__main__":

    MainGame().start_game()

这个为什么没出现窗口,也没有报错。

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

pygame
time sleep
random    BG_COLOR = pygame.Color(, , )
SCREEN_WIDTH = SCREEN_HEIGHT = TEXT_COLOR = pygame.Color(, , )  Tank:
    (, left: , top: ) -> :
        .images = {
            : pygame.image.load(),
            : pygame.image.load(),
            : pygame.image.load(),
            : pygame.image.load(),
        }
        .direction = .image = .images.get(.direction)
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = .remove = () -> :
        .image = .images.get(.direction)
        MainGame.window.blit(.image, .rect)

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


    ():
        MyTank(Tank):
    ():
        EnemyTank(Tank):
    (,left,top,speed):    .images = {
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load(),
            :pygame.image.load(),
        }
        .direction = .rand_direction()
        .image = .images.get(.direction)
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = speed
    () -> :
        chice = random.randint(, )      chice == :
            chice == :
            chice == :
            chice == :
            Bullet:
    ():
        ():
        ():
        Wall:
    ():  ():  Explode:
    ():  ():  Music:
    ():  ():
        MainGame:
    window = my_tank = enemy_tank_last = []
    enemy_tank_count = () -> :
        () -> :
        pygame.display.init()
        MainGame.window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))  pygame.display.set_caption()  MainGame.my_tank = Tank(, )
        .create_enemy_tank()
        :
            sleep()
            MainGame.window.fill(BG_COLOR)  a = text = .get_text_surface(a)  MainGame.window.blit(text, (, ))  .get_event()
            MainGame.my_tank.display_tank()
            .display_enemy_tank()
            MainGame.my_tank.remove:
                MainGame.my_tank.move()
            pygame.display.update()  () -> :
        .enemy_top = .enemy_speed = i (.enemy_tank_count):
            left = random.randint(, )
            e_tank = EnemyTank(left, .enemy_top, .enemy_speed)
            .enemy_tank_list.append(e_tank)
    () -> :
        e_tank .enemy_tank_list:
            e_tank.display_tank()


    (, text: ) -> :
        pygame.font.init()  font = pygame.font.SysFont(, )  text_surface = font.render(text, , TEXT_COLOR)  text_surface

    () -> :
        event_list = pygame.event.get()
        event event_list:
            event.type == pygame.QUIT:
                .end_game()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                    ()
                    MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_RIGHT:
                    ()
                    MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_UP:
                    ()
                    MainGame.my_tank.direction = MainGame.my_tank.remove = event.key == pygame.K_DOWN:
                    MainGame.my_tank.direction = ()
                    MainGame.my_tank.remove = event.type == pygame.KEYUP event.key (pygame.K_LEFT, pygame.K_RIGHT,
                                                            pygame.K_UP, pygame.K_DOWN):
                MainGame.my_tank.remove = () -> :
        ()
        ()  __name__ == :
    MainGame().start_game()

老师帮我看一下哪里错了


Python 全系列/第二阶段:Python 深入与提高/坦克大战 1514楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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