pygame have multiple of the same images

809 views Asked by At

Hello I am trying to create towers for my Tower Defense game but every time i select a new tower the old one gets removed. I do not want this to be removed and I am sure there is a simple way to do this but I cannot find it. Here is my code. Thank you for any help.

def displayTower():
    global bx, by
    click = pygame.mouse.get_pressed()
    Background.blit(redTower, (mx-bx,my-by))
    Background.blit(redTower, (530,650))

while intro == 1:
    mousePos = pygame.mouse.get_pos()
    mousePressed = pygame.mouse.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        if 530 < mousePos[0] < 590 and 650 < mousePos[1] < 710:
            if mousePressed[0] == 1:
                clicked = True
        if clicked == True:
            mx, my = pygame.mouse.get_pos()
            bx = 30
            by = 30
            if mousePressed[0] == 0:
                Background.blit(redTower, (mx-bx,my-by))
                tx = mx - bx
                ty = my - by
                clicked = False
displayTower()
1

There are 1 answers

5
Dalen On

For one thing, you are calling displayTower() outside of a while loop, so it never gets executed. So you are blitting only one tower at a time, not two.

You each time have to blit all the screen or blit a portion of it and update only the rects affected by the change.

Yes, what you blitted should stay on, but you cannot count on it without proper updating when you wish.

To be secure, you should use internal surface, then blit it over the screen surface when you are done blitting and drawing. Anyway, what should the background variable contain? A screen or your surface?

So the second thing is that you never update the screen. You have to use either pygame.display.flip() or pygame.display.update().

And, do use events to get mouse position, it's more clever. Also add a sleep or pygame.time.clock() to regulate fps, this is almost a busy loop what you wrote.