2017-02-05 260 views
0

我對pygame很新,並且正在嘗試做一個基本的塔防遊戲。我環顧四周,但無法掌握如何從我使用的圖像創建多個精靈(塔)。這是我的TD遊戲代碼。但我不知道如何創建更多的精靈(塔)。pygame創建多個精靈

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()  

感謝您的期待!我知道現在的代碼有點混亂。

回答

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) 

我強烈建議看雪碧文檔: http://www.pygame.org/docs/ref/sprite.html - 有很多很好的信息在裏面。那裏有很多很好的教程,介紹如何在Pygame中使用精靈。這裏是我寫的一個鏈接: http://kidscancode.org/blog/2016/08/pygame_1-2_working-with-sprites/