我想请你帮忙。我正在遵循有关创建外星人游戏的教程,但我对其进行了一些修改,但无法使其正常工作。我在settings pyfile中具有Alien_speed的价值。我正在方法increase_speed中对其进行修改,并进行打印(并且实际上它正在按我的意愿增长)。但是外星人的速度仍然相同。我不明白为什么它不起作用。也许有人可以指出我正确的方向吗?

settings.py:

import pygame

resolution_width = 1280
resolution_height = 720

class Settings:
    """A class to store all settings for Space Impact."""

    def __init__(self):
        """Initialize the game's static settings."""
        # Screen settings
        # This line is needed to avoid error: No video mode has been set
        self.screen = pygame.display.set_mode((0, 0))
        self.screen_width = resolution_width
        self.screen_height = resolution_height
        self.bg_image = pygame.image.load("images/background_1.png").convert()

        # Bullet settings
        self.bullet_speed = self.screen_width*0.01
        self.bullet_width = self.screen_width*0.02
        self.bullet_height = self.screen_height*0.02
        self.bullet_color = (0, 0, 0)

        # How quickly the game speeds up
        self.speedup_scale = 999
        # Ship Settings
        self.ships_limit = 3



        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.alien_speed = self.screen_width*0.003

    def increase_speed(self):
        """Increase speed settings."""
        self.alien_speed *= self.speedup_scale
        print(self.alien_speed)


Alien.py:

import pygame
from pygame.sprite import Sprite
from settings import Settings
import random


class Alien(Sprite):
    """A class to represent a single alien."""

    def __init__(self, space_impact):
        """Initialize alien and set it's starting position."""
        super().__init__()
        self.screen = space_impact.screen
        self.settings = Settings()

        # Load the alien image and set it's rect attribute.
        self.index = 0
        self.timer = 0
        self.image = []
        self.image.append(pygame.image.load('images/alien_1.png'))
        self.image.append(pygame.image.load('images/alien_2.png'))
        self.image = self.image[self.index]
        self.image = pygame.transform.scale(self.image, (80 * int(self.settings.screen_width * 0.0019),
                                            40 * int(self.settings.screen_width*0.0019)))

        self.rect = self.image.get_rect()

        random_height = random.uniform(0.01, 0.85)
        random_width = random.uniform(1.1, 2)
        self.rect.x = int(self.settings.screen_width * random_width)
        self.rect.y = int(self.settings.screen_height * random_height)

        # Store the alien's exact horizontal position.
        self.x = float(self.rect.x)

    def update(self):
        """Move the alien to left side."""
        self.x -= self.settings.alien_speed
        self.rect.x = self.x

        if self.timer >= 0 and self.timer <= 25:
            self.timer += 1
            self.index = 0

        elif self.timer >= 26 and self.timer < 50:
            self.timer += 1
            self.index = 1
        else:
            self.timer = 0

        if self.index == 0:
            self.image = pygame.image.load("images/alien_1.png")
        if self.index == 1:
            self.image = pygame.image.load("images/alien_2.png")

        self.image = pygame.transform.scale(self.image, (80 * int(self.settings.screen_width * 0.0019),
                                            40 * int(self.settings.screen_width * 0.0019)))


编辑:当然,在我的主文件中,我正在调用函数self.settings.increase_speed()



编辑2:

import pygame

resolution_width = 1280
resolution_height = 720

class Settings:
    """A class to store all settings for Space Impact."""

    screen_width = resolution_width
    alien_speed = screen_width * 0.003
    speedup_scale = 3
    alien_speed *= speedup_scale

    def __init__(self):
        """Initialize the game's static settings."""
        # Screen settings
        # This line is needed to avoid error: No video mode has been set
        self.screen = pygame.display.set_mode((0, 0))
        self.screen_width = resolution_width
        self.screen_height = resolution_height
        self.bg_image = pygame.image.load("images/background_1.png").convert()

        # Bullet settings
        self.bullet_speed = self.screen_width*0.01
        self.bullet_width = self.screen_width*0.02
        self.bullet_height = self.screen_height*0.02
        self.bullet_color = (0, 0, 0)

        # How quickly the game speeds up
        self.speedup_scale = 3
        # Ship Settings
        self.ships_limit = 3




    def increase_speed(self):
        """Increase speed settings."""
        global alien_speed
        global speedup_scale
        alien_speed *= speedup_scale
        print(self.alien_speed)


编辑3:

感谢您的评论,我设法解决了这个问题。谢谢 :)

最佳答案

Settings似乎包含全局设置,但是每个Alien都会创建自己的Settings实例:


  

class Alien(Sprite):
   """A class to represent a single alien."""

   def __init__(self, space_impact):
       # [...]

       self.settings = Settings()



这意味着每个Alien都有自己的Settings,因此也有alien_speed

您必须更新每个Alien实例中的设置,这意味着您必须分别为每个increase_speed()调用Alien

或者只是将Settings的单例实例传递给Aliens。这样就足以更新单例实例:

class Alien(Sprite):
    """A class to represent a single alien."""

    def __init__(self, space_impact, settings):
        """Initialize alien and set it's starting position."""
        super().__init__()
        self.screen = space_impact.screen
        self.settings = settings
        # [...]


在“主要”中:

alien = Alien(space_impact, self.settings)


另一个选择是将类Settings的属性转换为class attributes。实例属性对于每个实例都是唯一的,但是类属性由所有实例共享。

关于python - Python和Pygame的值(value)正在更新,但代码未考虑更新?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59400380/

10-11 03:24