时间问题和敌人的出现 (Python, Pygame)

Problems with time and the appearance of enemies (Python, Pygame)

我post第二次遇到这个问题,但有更多的信息。我的问题出在我的代码上,由于某种原因,有几行代码不起作用。我正在使用 pygame 库在 python 中制作一个拼图游戏。如果你运行代码,你会看到屏幕上有1个红色方块从上到下不停地飞。红色方块是敌人。我需要在 10 秒后飞的不是 1 个方格,而是 2 个方格。 MobY class 对此负责,它会产生敌人,而在 Game class 中new() 方法有一个创建红色方块的循环:

for i in range(self.mobY):
    m = MobY()
    self.all_sprites.add(m)
    self.mobs.add(m)

存储在__init__中的变量self.mobY等于1,即同一时间屏幕上只能有1个红色方块。屏幕上还有一个计时器,它的秒数存储在方法 time().
中的变量 self.seconds 中 为了让 10 秒后 2 个红色方块飞过屏幕,理论上你需要将这些代码行放在 new() 方法中:

if self.seconds >= 10:
    self.mobY += 1

但是我得到一个错误“'Game' 对象没有属性 'seconds'”,虽然它不是。
一般来说,我需要 10 秒后屏幕上会多出 1 个方块。
这是代码:

import sys
import pygame as pg
import random

#         R    G    B
DARKGREY = (40, 40, 40)
LIGHTGREY = (43, 43, 43)
RED = (255, 0, 0)

WIDTH = 1008
HEIGHT = 768
FPS = 60
TITLE = "TITLE GAME"
BGCOLOR = DARKGREY
TILESIZE = 48


class MobY(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((TILESIZE, TILESIZE))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(0, WIDTH, TILESIZE)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(6, 9)

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT + TILESIZE:
            self.rect.x = random.randrange(0, WIDTH, TILESIZE)
            self.rect.y = random.randrange(-100, -40)
            self.speedy = random.randrange(6, 9)


class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(1, 15)
        self.frame_count = 0
        self.mobY = 1

    def new(self):
        self.all_sprites = pg.sprite.Group()
        self.mobs = pg.sprite.Group()

        for i in range(self.mobY):
            m = MobY()
            self.all_sprites.add(m)
            self.mobs.add(m)

    def run(self):
        self.playing = True
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.update()
            self.draw()

    def quit(self):
        pg.quit()
        sys.exit()

    def update(self):
        self.all_sprites.update()

    def time(self):
        self.font = pg.font.Font(None, 50)
        self.font_color = pg.Color('springgreen')
        self.total_seconds = self.frame_count // FPS
        self.seconds = self.total_seconds % 6000
        self.output_string = "TIME: {0}".format(self.seconds)
        self.text = self.font.render(self.output_string, True, self.font_color)
        self.screen.blit(self.text, [10, 10])
        self.frame_count += 1

    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

    def draw(self):
        self.screen.fill(BGCOLOR)
        self.draw_grid()
        self.all_sprites.draw(self.screen)
        self.time()
        pg.display.flip()

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit()


g = Game()
while True:
    g.new()
    g.run()

But I get an error "'Game' object has no attribute 'seconds'"

当然可以,因为 self.seconds 是在方法 time 中定义的,而不是 class Game 的构造函数中定义的。当 new 第一次被调用时,属性 self.seconds 不存在,因为 time 之前没有被调用过。

无论如何你的方法是行不通的,因为条件 if self.seconds >= 10: si 连续满足并且会导致 self.mobY 递增多次。


使用 pg.time.get_ticks() 自调用 pygame.init() 以来的毫秒数。
定义必须创建第一个 MobY 的时间 self.next_mob_time。当超过时间点时,然后创建一个 MobY 的实例并将时间增加 10000。

current_time = pg.time.get_ticks()
if current_time > self.next_mob_time:
    self.next_mob_time += 10000
    self.mobY += 1

及时调用new。此外,必须在 Game 的构造函数中创建组,而不是在 new:

class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(1, 15)
        self.frame_count = 0
        self.mobY = 0
        self.next_mob_time = 0
        self.all_sprites = pg.sprite.Group()
        self.mobs = pg.sprite.Group()

    def new(self):
        current_time = pg.time.get_ticks()
        if current_time > self.next_mob_time:
            self.next_mob_time += 10000
            self.mobY += 1
            m = MobY()
            self.all_sprites.add(m)
            self.mobs.add(m)

    # [...]

    def time(self):
        self.new()
        self.seconds = pg.time.get_ticks() // 1000

        # [...]