【问题标题】:Python and Pygame Value is updating but update is not taken into account by code?Python 和 Pygame 值正在更新,但代码没有考虑更新?
【发布时间】:2020-04-11 11:59:06
【问题描述】:

我想请你帮忙。我正在关注有关创建外星人游戏的教程,但我对其进行了一些修改,但无法使其正常工作。我在 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)

外星人.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:

感谢您的 cmets,我设法修复了它。谢谢你:)

【问题讨论】:

  • 我看到的实例函数看起来像是属于一个类,但没有看到任何类定义。您可以包含更多代码吗?至少类定义
  • 您说您在主文件中调用了increase_speed,我们可以看看这段代码吗?
  • 每个外星人都有自己的Settings实例吗?
  • 我在帖子中添加了更多代码我很抱歉格式不佳。是的,每个外星人都有一行代码: self.settings = Settings() 在主文件中我有这些代码行: if not self.aliens: self._create_fleet_1() self._create_fleet_2() self._create_fleet_3() self.settings .increase_speed() print("Next wave!") 当没有外星人时,代码正在生成舰队并执行“increase_speed”

标签: python function class methods pygame


【解决方案1】:

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

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

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

       self.settings = Settings()

这意味着每个Alien 都有自己的Settingsalien_speed

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

或者只是将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。实例属性对每个实例都是唯一的,但类属性由所有实例共享。

【讨论】:

  • 您好,非常感谢您的回答。我尝试了所有三种解决方案 3 小时,但我无法让它发挥作用。对不起,我是python的新手。您能否详细说明第一个或第二个解决方案?此外,我认为我在第三个中走得最远(我粘贴在“编辑 2”新代码设置的原始帖子中。它仍然无法正常工作。(我尝试使用那些“全局”关键字但没有它们,我仍然收到错误关于“分配前引用的局部变量”。感谢您的宝贵时间。
猜你喜欢
  • 2018-02-03
  • 1970-01-01
  • 2014-07-22
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
相关资源
最近更新 更多