简体   繁体   English

如何在PyGame中按时间增量在屏幕上显示精灵

[英]How to blit sprites to screen on timed increments in PyGame

I'm making a game in Pygame in which the some characters appear on one side of the screen and move to the other, where they teleport to the place they started from and do it again. 我正在Pygame中制作一个游戏,其中一些角色出现在屏幕的一侧,然后移到另一侧,在那里他们传送到开始的地方并再次执行。 I want the sprites to appear one at a time, with a random time increment between their spawning. 我希望子画面一次出现一个,并且在它们之间产生随机的时间增量。 I have created a class for the sprites, and I have put them in a group called "monsters". 我为精灵创建了一个类,并将它们放在称为“怪物”的组中。 My blitting them looks like this: 我发短信给他们看起来像这样:

for monster in monsters:
            monster.render(screen)
            time.sleep(0.5)

What this does is make the screen go black until all the monsters spawn. 这是使屏幕变黑直到所有怪物都产卵。 I want them to spawn one at a time and for the player to be able to see that. 我希望它们一次生成一个,以便玩家能够看到。 Here's the whole class that code above is in: 这是上面的代码所在的整个类:

import pygame, sys, random, time
from monster import *

pygame.init()

class Main:
    clock = pygame.time.Clock()

    screenSize = (500,500)
    background = pygame.image.load("C:/Users/Nathan/PycharmProjects/Monsters II A Dark Descent/images/background.jpg")

    screen = pygame.display.set_mode(screenSize)
    pygame.display.set_caption("MONSTERS!")

    monsters = pygame.sprite.Group()

    counter = 0

    x = 450
    while counter < 5:
            y = random.randint(50,450)
            monster = Monster(x,y)
            monsters.add(monster)
            counter = counter + 1

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.blit(background,(0,0))

        for monster in monsters:
            monster.render(screen)
            time.sleep(0.5)


        x=x-1

        clock.tick(60)

        pygame.display.flip()


Main()

I am also having a second issue. 我还有第二个问题。 I want the sprites to move a random speed (their current position(x) plus a random number) once they spawn. 我希望精灵在生成时以随机速度移动(它们的当前位置(x)加一个随机数)。 I also want them to teleport where they started and do it again once they reach the end of the screen. 我还希望他们将他们开始的地方传送出去,并在到达屏幕末尾时再次执行。 Here is the sprite movement class: 这是精灵运动类:

def move(self):
        if(self.x > 500):
            self.x = 450
        self.x -= random.randint(1,5)

Currently them move at varying speeds and just go off the edge of the screen. 目前,它们以不同的速度移动,只是离开屏幕的边缘。 Here is the whole sprite class: 这是整个精灵类:

import random, pygame

class Monster(pygame.sprite.Sprite):

    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.image = pygame.image.load("C:\Users\Nathan\PycharmProjects\Monsters II A Dark Descent\images\monster.png")

    def move(self):
        if(self.x > 500):
            self.x = 450
        self.x -= random.randint(1,5)

    def render(self, screen):
        screen.blit(self.image, (self.x, self.y))

Thank you for your time. 感谢您的时间。 If you need any more details let me know. 如果您需要更多详细信息,请告诉我。

Nothing will happen during 在此期间什么都不会发生

time.sleep(...)

so the whole game stops while you wait for each monster to be blit ted. 因此当您等待每个monsterblit时,整个游戏将停止。

You need to allow the while True loop to keep running, but control the monsters that are being displayed. 您需要允许while True循环继续运行,但要控制正在显示的monsters For example, you could keep a count of visible_monsters which is added to after a suitable interval: 例如,您可以保留一个visible_monsters计数,该计数将在适当的时间间隔后添加到其中:

elapsed, visible_monsters = 0, 0

while True:

    elapsed += clock.tick(60)

    for event in pygame.event.get():
        ...

    screen.blit(background,(0,0))

    if elapsed >= 500:
        elapsed = 0
        visible_monsters += 1

    for monster in monsters[:visible_monsters]:
        monster.render(screen)

For your second problem, keep the monster 's init_x and init_y so you can reuse them later and check that they are inside the screen when you render them (eg 0 <= self.x <= screenSize[0] ). 对于你的第二个问题,保持monsterinit_xinit_y以便以后可以重新使用它们,并检查他们的内screen ,当你render它们(如0 <= self.x <= screenSize[0]

To have various sprites appear at different timings you should you use a timer loop as John says. 要使各种精灵在不同的时间出现,您应该使用约翰所说的计时器循环。 The code he has given is correct. 他提供的代码是正确的。 That is the only way you can do this without slowing down the entire game loop. 这是您不降低整个游戏循环速度的唯一方法。

elapsed, visible_monsters = 0,0 过去了,visible_monsters = 0,0

means he is assigning two varables in one line. 表示他正在一行中分配两个变量。 It could also be written as 也可以写成

elapsed = visible_monsters = 0 经过的= visible_monsters = 0

time.sleep() would only pause your program obviously and the only way you can spawn your monsters at different timing is using the Clock.tick function, of cause you must have already called the pygame.time.Clock() into a variable. time.sleep()只会明显地暂停程序,并且您可以在不同时间生成怪物的唯一方法是使用Clock.tick函数,因为您必须已经将pygame.time.Clock()调用为变量。 i used that a lot of times in my snake game too. 我在蛇游戏中也使用了很多次。 you could also use the Pygame USEREVENT probably you should read the pygame documentation and see how to do cool things with the pygame time module. 您也可以使用Pygame USEREVENT,也许您应该阅读pygame文档,并了解如何使用pygame时间模块完成一些有趣的事情。 and the fact that you have two while loops there with no means of ending them probably turns your game surface black while running the game and i feel that jonrsharpe ans is a good go check your code again and just fix the issues 而且您有两个while循环而无法结束它们的事实可能会在运行游戏时使您的游戏表面变黑,我觉得jonrsharpe ans是个不错的选择,再次检查您的代码并解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM