0
votes

I am very new to pygame, and was trying to make a basic tower defence game. I have looked around, but cannot grasp how to create multiple sprites (towers) from the image I am using. Here is my code for the TD game. But I do not know how to create more then one sprite(tower).

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

Thanks for looking! I know the code is a bit messy right now.

1

1 Answers

0
votes

You need to use the Sprite class properly. To do this, you have to define your sprite as a subclass of pygame.sprite.Sprite, like this:

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)

Then you can spawn a tower any time you like by creating another instance and adding it to the group:

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

I highly recommend looking at the Sprite documentation: http://www.pygame.org/docs/ref/sprite.html - there's lots of good info in there. There are also many good tutorials out there that go into how you use sprites in Pygame. Here is a link to one that I wrote: http://kidscancode.org/blog/2016/08/pygame_1-2_working-with-sprites/