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

问题描述 投票:1回答:1

我第二次发这个问题,但是信息量比较大。我的问题出在我的代码上,由于某些原因,有几行代码不能用。我正在用pygame库用python做一个瓷砖游戏。如果你运行这段代码,你会看到屏幕上有1个红色的方块从上到下无休止地飞。红色方块是敌人。我需要在10秒后不是1个方块飞起来,而是2个。MobY 类负责,会产生敌人,而在 Game 课堂上 new() 方法有一个循环,创建红色方块。

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

变量 self.mobY 储存在 __init__ 等于1,也就是说,在同一时间,屏幕上只能有1个红色方块。屏幕上还有一个定时器,其秒数存储在一个变量 self.seconds 该法 time(). 因此,10秒后,2个红色的方块飞过屏幕,理论上你需要把这几行代码放在 new() 方法,但我得到一个错误"'游戏'对象没有属性'秒'",虽然不是。

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

但我得到一个错误"'游戏'对象没有属性'秒'",虽然它不是。一般来说,我需要在10秒后,屏幕上会多出一个方块。下面是代码。

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()
python python-3.x timer pygame game-engine
1个回答
1
投票

但我得到一个错误"'游戏'对象没有属性'秒'"

是的,当然,因为 self.seconds 是在方法中定义的 time 而不是类的构造函数 Game. 当 new 被第1次调用,那么属性的 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

        # [...]
© www.soinside.com 2019 - 2024. All rights reserved.