【问题标题】:How can I blit an image on the screen where i click after pressing a button如何在按下按钮后单击屏幕上的图像
【发布时间】:2020-05-02 07:54:06
【问题描述】:

我正在制作一个 pygame 游戏。单击屏幕底部的坦克按钮后,我需要能够在屏幕上放置坦克。

目前我已经硬编码了产卵的位置,但我无法将坦克放置在点击的位置(点击坦克按钮后)

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

这是我的主要代码功能

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)

点击坦克按钮后,我需要帮助我将坦克放置在屏幕上(无论我点击哪里)。

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    screen.blit(tank,pygame.mouse.get_pos()) 在鼠标位置对坦克进行blit。但这不会满足你。您必须将鼠标位置存储在列表中,并在主应用程序循环中对坦克进行 blit。

    为坦克位置添加一个列表,并在感谢产生时将鼠标位置添加到列表中:

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

    在主应用循环中绘制坦克:

    def main():
        # [...]
    
        tank_surf = pygame.image.load("tank.png")
    
        while run:
    
            # [...]
    
            for tank_pos in tank_pos_list:
                screen.blit(tank, tank_pos)
    

    点击按钮后设置spawner。如果单击按钮并设置spawner,则附加一个新坦克。 请注意,您必须添加一些代码,在第二次点击时评估它是在游戏区域中,但这是您必须自己解决的任务。

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

    我建议将事件处理和绘制对象分开。在主应用循环而不是事件循环中绘制所有对象:

    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)
    

    【讨论】:

    • 嘿,谢谢您的回答,但坦克正在按钮本身上产卵。当我单击按钮时,它会在按钮的各种坐标上生成一个坦克,具体取决于我单击它的位置。单击按钮后,我需要它在屏幕上(我单击的位置)生成。
    • @risi 我已经扩展了答案
    • 非常感谢,如果运行良好,我只需要评估游戏区域,它会完美运行。虽然它的工作原理你能更详细地解释一下发生了什么吗?我不太明白您是如何检查第二次点击的。
    • @rishi 最初是 spawner == False。第一次点击设置spawner = True。如果spawner == True(第二次点击)则spawn_tank()被调用并且spawner被设置为False。进程不能重新开始。
    • 我想检查 2 个生成的坦克之间的距离是否小于 40 像素。如果它更少,那么我不想破坏坦克。我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2020-12-27
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多