rishi
rishi

Reputation: 640

How can I blit an image on the screen where i click after pressing a button

I am making a pygame game. I need to be able to place tanks on the screen after I click the tank button which on the bottom of my screen.

Currently i have hard coded the position of spawn but I am not able to place the tank at the position of click(after i click on the tank button)

def spawn_tank():
    tank = pygame.image.load("tank.png")
    screen.blit(tank, (250, 350))

This is my main code function

spawner = False

def main():
    global new_tanks
    global spawner
    run = True
    fps = 90
    tanks = Button((59, 255, 140), 100, 610, 80, 80, text = "Tanks")
    towers = Button((59, 255, 140), 510, 610, 150, 80, text = "Towers")

    blue = pygame.image.load("blue_base.png")
    red = pygame.image.load("red_base.png")

    while run:

        mx, my = pygame.mouse.get_pos()
        pos = (mx, my)
        screen.fill((50, 168, 66))
        x = pos[0]
        y = pos[1]
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            pygame.draw.rect(screen, (201, 142, 47), (0, 600, 1000, 100))
            pygame.draw.line(screen, (0, 0, 0), (500,0), (500, 600))
            tanks.draw(screen)
            towers.draw(screen)

            tanks = Button((59, 255, 140), 100, 610, 80, 80, text="Tanks")
            mx, my = pygame.mouse.get_pos()
            mouse_pos = (mx, my)

            if tanks.isOver(mouse_pos):
                tanks = Button((0, 255, 0), 100, 610, 80, 80, text="Tanks")
                tanks.draw(screen)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    spawner = True


            else:
                tanks = Button((59, 255, 140), 100, 610, 80, 80, text="Tanks")
                tanks.draw(screen)

            towers = Button((59, 255, 140), 510, 610, 150, 80, text="Towers")
            mx, my = pygame.mouse.get_pos()
            mouse_pos = (mx, my)

            if towers.isOver(mouse_pos):
                towers = Button((0, 255, 0), 510, 610, 150, 80, text="Towers")
                towers.draw(screen)

            else:
                towers = Button((59, 255, 140), 510, 610, 150, 80, text="Towers")
                towers.draw(screen)
            if spawner:
                spawn_tank()


            screen.blit(blue, (0, 100))
            screen.blit(red, (800, 100))

            pygame.display.flip()
            clock.tick(fps)

I need help with the placement of the tank on the screen(wherever I click) after clicking on the tank button.

Upvotes: 1

Views: 325

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

screen.blit(tank,pygame.mouse.get_pos()) blits the tank at the mouse position. But that won't satisfy you. You have to store the mouse position in a list and to blit the tanks in the main application loop.

Add a list for the tank positions and add the mouse position to the list when the thank spawns:

tank_pos_list = []

def spawn_tank():
    global tank_pos_list 
    tank_pos_list.append(pygame.mouse.get_pos()) 

Draw the tanks in the main application loop:

def main():
    # [...]

    tank_surf = pygame.image.load("tank.png")

    while run:

        # [...]

        for tank_pos in tank_pos_list:
            screen.blit(tank, tank_pos)

When the button is clicked then set spawner. If the button is clicked and spawner is set the append a new tank. Note, you have to add some code, that evaluates it the second click is in the game area, but that is a a task which you have to solve yourself.

if event.type == pygame.MOUSEBUTTONDOWN:
    if spawner:
        spawn_tank()
        spawner = False
    if tanks.isOver(mouse_pos):
        spawner = True

I recommend to separate the event handling and drawing the objects. Draw all the objects in the main application loop rather than the event loop:

def main():
    global new_tanks
    global spawner
    run = True
    fps = 90
    tanks = Button((59, 255, 140), 100, 610, 80, 80, text = "Tanks")
    tanks_over = Button((0, 255, 0), 100, 610, 80, 80, text="Tanks")
    towers = Button((59, 255, 140), 510, 610, 150, 80, text = "Towers")
    towers_over = Button((0, 255, 0), 510, 610, 150, 80, text="Towers")

    blue = pygame.image.load("blue_base.png")
    red = pygame.image.load("red_base.png")
    tank_surf = pygame.image.load("tank.png")

    spawner = False

    while run:

        mx, my = pygame.mouse.get_pos()
        pos = (mx, my)
        x = pos[0]
        y = pos[1]

        mouse_pos = (mx, my)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                if spawner:
                    spawn_tank()
                    spawner = False
                if tanks.isOver(mouse_pos):
                    spawner = True

        screen.fill((50, 168, 66))

        pygame.draw.rect(screen, (201, 142, 47), (0, 600, 1000, 100))
        pygame.draw.line(screen, (0, 0, 0), (500,0), (500, 600))

        if tanks.isOver(mouse_pos):
            tanks_over.draw(screen)
        else:
            tanks.draw(screen)

        if towers.isOver(mouse_pos):
            towers_over.draw(screen)
        else:
            towers.draw(screen)

        screen.blit(blue, (0, 100))
        screen.blit(red, (800, 100))

        for tank_pos in tank_pos_list:
            screen.blit(tank_surf, tank_pos)

        pygame.display.flip()
        clock.tick(fps)

Upvotes: 1

Related Questions