Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 外星人入侵计划速成班第14章-游戏有一个黑屏,而不是显示一个按钮_Python_Button_Pygame - Fatal编程技术网

Python 外星人入侵计划速成班第14章-游戏有一个黑屏,而不是显示一个按钮

Python 外星人入侵计划速成班第14章-游戏有一个黑屏,而不是显示一个按钮,python,button,pygame,Python,Button,Pygame,所以我正在经历外星人入侵项目,除了这个,我没有遇到任何我不能解决的问题。我试图从书中输入,并在游戏开始时制作一个透明覆盖的按钮。当我这样做的时候,游戏开始时是一个黑屏。我已经在Vs代码和Sublime中测试了这个程序,它在这两个方面都做了相同的事情 按钮就在那里,我使用下一章的代码进行了测试。我可以告诉它在那里,因为当我把鼠标移到屏幕中间时,它会像往常一样触发游戏。真奇怪 我一直在反复检查代码,但我没有发现输入有任何问题。我也将检查前面章节的输入 任何帮助都会很好,因为这似乎真的很愚蠢,无法坚持

所以我正在经历外星人入侵项目,除了这个,我没有遇到任何我不能解决的问题。我试图从书中输入,并在游戏开始时制作一个透明覆盖的按钮。当我这样做的时候,游戏开始时是一个黑屏。我已经在Vs代码和Sublime中测试了这个程序,它在这两个方面都做了相同的事情

按钮就在那里,我使用下一章的代码进行了测试。我可以告诉它在那里,因为当我把鼠标移到屏幕中间时,它会像往常一样触发游戏。真奇怪

我一直在反复检查代码,但我没有发现输入有任何问题。我也将检查前面章节的输入

任何帮助都会很好,因为这似乎真的很愚蠢,无法坚持下去

注意:我只包括了相关的方法。如果您想查看其他人的代码,请询问

button.py

import pygame.font

class Button:

def __init__(self, ai_game, msg):
    """Initialize button attributes."""
    self.screen = ai_game.screen
    self.screen_rect = self.screen.get_rect()
    #Set the dimensions and properties of the button.
    self.width, self.height = 200, 50
    self.button_color = (0, 255, 0)
    self.text_color = (255, 255, 255)
    self.font = pygame.font.SysFont(None, 48)

    #Build the button's rect object and center it.
    self.rect = pygame.Rect(0, 0, self.width, self.height)
    self.rect.center = self.screen_rect.center

    #The buttone needs to be prepped only once.
    self._prep_msg(msg)

def _prep_msg(self, msg):
    """Turn msg into a rendered image and center text on the button."""
    self.msg_image = self.font.render(msg, True, self.text_color, self.button_color)
    self.msg_image_rect = self.msg_image.get_rect()
    self.msg_image_rect.center = self.rect.center

def draw_button(self):
    #Draw a blank button then draw message
    self.screen.fill(self.button_color, self.rect)
    self.screen.blit(self.msg_image, self.msg_image_rect)
gamestats.py

class GameStats:

def __init__(self, ai_game):
    """Initialise statistics."""
    self.settings = ai_game.settings
    self.reset_stats()
    
    #Start Alien Invasion in an inactive state.
    self.game_active = False

def reset_stats(self):
    """Initialise statistics that can change during the game."""
    self.ships_left = self.settings.ship_limit
    

你的按钮代码很好

问题在于,只要
未激活self.stats.game,就只能在
\u update\u screen()
中将按钮绘制到屏幕上。那很好

但是!您不会调用
\u update\u screen()
,除非
self.stats.game\u处于活动状态
,因此在“按钮显示”模式下,不会绘制任何内容:

def run_game(self):
    """Start the main loop for the game."""
    while True:
        self._check_events()
        
        if self.stats.game_active:      #  <<-- HERE
            self.ship.update()
            self._update_bullets()
            self._update_screen()       #  <<-- AND HERE
            self._update_aliens()
def run_游戏(self):
“”“启动游戏的主循环。”“”
尽管如此:
自我检查事件()

如果self.stats.game_处于活动状态:#我刚刚测试过。它起作用了!我重新阅读了这一页,我读的代码似乎跨越了两页,所以我看不到缩进和它是如何正确包装的。self.\u update\u screen()在if循环之外是有道理的。谢谢你的帮助!
def run_game(self):
    """Start the main loop for the game."""
    while True:
        self._check_events()
        
        if self.stats.game_active:      #  <<-- HERE
            self.ship.update()
            self._update_bullets()
            self._update_screen()       #  <<-- AND HERE
            self._update_aliens()
def run_game(self):
    """Start the main loop for the game."""
    while True:
        self._check_events()
        
        if self.stats.game_active:     
            self.ship.update()
            self._update_bullets()
            self._update_aliens()

        self._update_screen()           #  <<-- HERE (always paint)