窗口颜色/在pygame中添加飞船

问题描述

我可以编辑窗口大小。但是,当尝试更改窗口背景颜色时,我遇到了问题。不管我输入窗口背景的哪个数字都保持黑色。我决定尝试在代码中添加一条船,以查看背景中是否有任何变化。什么都没改变。

import sys
import pygame
from settings import Settings
from ship import Ship
 
class AlienInvasion:
 
    """Overall class to manage game assets and behavior."""
 
    def __init__(self):
        """Initialize the game,and greate game resources."""
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")
         
        self.ship = Ship(self)
 
    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
    # Redraw the screen during each pass through the loop.
        self.screen.fill(self.settings.bg_color)
         
        self.ship.blitme()
 
    # Make the most recently drawn screen visible.
 
        pygame.display.flip()
 
if __name__ == '__main__':
    # Make a game instance,and run the game.
    ai = AlienInvasion()
    ai.run_game()

此:

class Settings:
    """A class to store all settings for Alien Invasion."""
 
    def __init__(self):
        # Screen Settings
        self.screen_width = 600
        self.screen_height = 500
        self.bg_color = (230,230,230)

和这个:

import pygame
 
class Ship:
    """A class to manage the ship."""
 
    def __init__(self,ai_game):
        """Initialize the ship and set its starting position."""
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
 
        # Load the ship image and get its rect.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
 
        # Start each new ship at the bottom enter of the screen.
        self.rect.midbottom = self.screen_rect.midbottom
 
    def blitme(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image,self.rect)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)