生成多个相同的精灵 [英] Spawning Multiple of the same sprite

查看:71
本文介绍了生成多个相同的精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一款名为 Table Wars 的游戏,这是一款适用于两名玩家的回合制战略游戏.进展一直很顺利,直到我遇到了生成单位的问题.

I am currently working on a game called Table Wars, a turn based strategy game for two players. Progress has been going smoothly, until I ran into a problem with spawning units.

程序不会生成多个相同的单元,也不会在旧单元死后重新生成新单元.

The program won't spawn multiple of the same unit, or respawn new ones after the old ones die.

以下信息可能会有所帮助:

Here is some information that may help:

  • 每个类都存储在一个变量中:(redI = Red_Infantry())
  • 所有函数都存储在主循环中.
  • 精灵类具有硬编码的 X 和 Y 值,用于生成单位和移动单位.

我该怎么办?

根据要求,这是红色步兵的课程:

As requested, here is the class for the Red Infantry:

class Red_Infantry(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image('Soldier_red.png', -1)
        self.selected = 0
        self.area = screen.get_rect()
        self.rect.topleft = (100, 300)
        self.health = 100 #Soldiers are have mediocre toughness.
        self.attack_damage = 25
        self.range_maximum = 20 #in pixels, this is melee range
        self.range_minimum = 0
        self.update()
    def update(self):
        if self.health <= 0:
            self.kill()

以及生成这个单位的代码:

and the code to spawn this unit:

if spawned_units < 3 and PHASE == 1 and TURN == 'Red':
    if REDGOLD < 10:
        print "Out of money! Moving to Phase 2!"
        PHASE = 2
        spawned_units = 0
    elif event.type == KEYDOWN and event.key == K_1:
        if REDGOLD >= 10 and REDCOMMAND >= 5:
            Sprites.append(redI)
            REDGOLD -= 10
            REDCOMMAND -= 5
            spawned_units = spawned_units + 1
        else:
            print "Not enough gold!"

这与所有单位的风格相似.它在第一次正确执行,但在第二次、第三次等等时都没有正确执行,这意味着我只能有一个士兵.此外,当那个士兵通过 self.kill 死亡时,如果我尝试生成它,它就不会回来.

This is similar style with all units. It performs correctly the first time, but not in the second, third, and so on, meaning I can only have one Soldier. Also, when that soldier dies via self.kill, it won't come back if I try to spawn it.

推荐答案

您发布的部分生成过程不会创建任何新实例.除非在其他地方将 redI 声明为新的 Red_Infantry,否则每次需要修改代码以创建 Red_Infantry 的新实例生成一个士兵.

The part of the spawn procedure you posted doesn't create any new instances. Unless redI is declared as a new Red_Infantry somewhere else, you need to modify the code to create a new instance of Red_Infantry every time you want to spawn a soldier.

sprites.append(Red_Infantry())

更新精灵:

for sprite in sprites:
    sprite.update()

将移动和其他状态更改放在更新方法中.这是 pygame 所期望的,但如果您愿意,您可以使用不同的样式.要点是你必须有多个 Red_Infantry 实例才能看到多个精灵.

Put movement and other state changes in the update method. This is what pygame expects, but you can use a different style if you want. The main point is you must have multiple instances of Red_Infantry in order to see multiple sprites.

您也可以使用 pygame 的 Group 类代替用于保存精灵的简单列表.

You could also use pygame's Group class instead of a simple list to hold the sprites.

这是一个使用 Group 而不是列表的完整示例.在这个例子中,每次按下一个键都会产生一个 Enemy.每个 Enemy 将其唯一 ID 打印到标准输出.

Here's a full example that uses Group instead of list. In the example, an Enemy is spawned each time a key is pressed. Each Enemy prints its unique ID to stdout.

import sys
import random

import pygame
from pygame.locals import *

def main():
    pygame.init()
    screen = pygame.display.set_mode((480, 320))
    enemies = pygame.sprite.Group()
    while True:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                enemies.add(Enemy(screen))
            elif event.type == QUIT:
                sys.exit()
        enemies.update()
        screen.fill(pygame.Color("black"))
        enemies.draw(screen)
        pygame.display.update()

class Enemy(pygame.sprite.Sprite):

    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        print "created a new sprite:", id(self)
        self.image = pygame.image.load("sprite.png")
        self.rect = self.image.get_rect()
        self.rect.move_ip(random.randint(0, screen.get_width()),
                          random.randint(0, screen.get_height()))

    def update(self):
        self.rect.move_ip(random.randint(-3, 3), 0)

if __name__ == "__main__":
    main()

这篇关于生成多个相同的精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆