pygame创建多个精灵

时间:2017-02-05 21:05:45

标签: pygame

我对pygame很新,并试图制作一个基本的塔防游戏。我环顾四周,但无法掌握如何从我正在使用的图像创建多个精灵(塔)。这是我的TD游戏代码。但我不知道如何创建超过一个精灵(塔)。

import pygame
import time
import sys
import os
pygame.init()

WINDOWWIDTH = 1800
WINDOWHEIGHT = 1800
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (0,30)
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
background_colour = (63,200,70)

GAMETITLE = "Tower Defence"
font = pygame.font.SysFont(None, 100) 
def balanceText(balance):
    screen_text = font.render(str(balance), True, (255,255,255))
    screen.blit(screen_text, [1640,0])

def main():
    balance = 100


    pygame.display.set_caption(GAMETITLE)
    clock = pygame.time.Clock()

    spritegroup =pygame.sprite.Group()
    sprite =pygame.sprite.Sprite()
    tower = sprite.image = pygame.image.load("tower.png")

    sprite.image = tower
    sprite.rect = sprite.image.get_rect()

    drawTower = False
    towerPlaced = False

    bulletX = 250
    sprite.add(spritegroup)

    while True:


        screen.fill(background_colour)
        pygame.draw.rect(screen, (255,255,255), ((0, 100), (1100, 90)))
        pygame.draw.rect(screen, (255, 255,255), ((1010, 100), (100, 600)))
        pygame.draw.rect(screen, (255,255,255), ((1010, 700), (2400, 90)))
        pygame.draw.rect(screen, (139,69,19), ((1600, 0), (2400, 18000)))
        pygame.draw.rect(screen, (128, 128,128), (( 300,250), (140,140)))
        pygame.draw.rect(screen, (128, 128,128), (( 600,250), (140,140)))
        pygame.draw.rect(screen, (128, 128,128), (( 800,700), (140,140)))
        pygame.draw.rect(screen, (128, 128,128), (( 1150,500), (140,140)))
        balanceText(balance)
        if drawTower:
            spritegroup.draw(screen)
        pygame.display.update()
        clock.tick(30)


        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:

                if pygame.mouse.get_pressed()[0] and towerPlaced == False:

                    mousePos = pygame.mouse.get_pos()
                    if mousePos[0] > 300 and mousePos[0] < 450 and mousePos[1] > 250 and mousePos[1] < 400:
                        drawTower = True
                        sprite.rect.center = (370, 320)
                        towerPlaced = True
                        balance -= 50

                    elif mousePos[0] > 600 and mousePos[0] < 750 and mousePos[1] > 250 and mousePos[1] < 400:
                        drawTower = True
                        sprite.rect.center = (670, 320)
                        towerPlaced = True
                        balance-= 50

                    elif mousePos[0] > 800 and mousePos[0] < 950 and mousePos[1] > 700 and mousePos[1] < 850:
                        drawTower = True
                        sprite.rect.center = (870, 770)
                        towerPlaced = True
                        balance-= 50

                    elif mousePos[0] > 1150 and mousePos[0] < 1300 and mousePos[1] > 500 and mousePos[1] < 650:
                        drawTower = True
                        sprite.rect.center = (1220, 570)
                        towerPlaced = True
                        balance-= 50

            elif event.type == pygame.QUIT:
                pygame.display.quit()



main()    

感谢您的期待!我知道代码现在有点乱。

1 个答案:

答案 0 :(得分:0)

您需要正确使用Sprite类。要做到这一点,你必须将你的精灵定义为pygame.sprite.Sprite的子类,如下所示:

class Tower(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = tower_img
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

然后,您可以通过创建另一个实例并将其添加到组来随时生成塔:

new_tower = Tower(370, 320)
spritegroup.add(new_tower)

我强烈建议您查看Sprite文档: http://www.pygame.org/docs/ref/sprite.html - 那里有很多好消息。还有很多很好的教程可以帮助你了解如何在Pygame中使用精灵。这是我写的一个链接: http://kidscancode.org/blog/2016/08/pygame_1-2_working-with-sprites/