繁体   English   中英

为什么我的子弹不动,即使我的坦克

[英]why are my bullets not moving even though my tanks are

我正在制作 pygame 游戏。当我点击坦克按钮,然后点击屏幕(游戏区域)时,坦克会在该坐标上显示。 与坦克一起,子弹也被击中。 我可以让我的坦克移动,但子弹没有射击。 我希望坦克在子弹移动后重置后自动射击,比如 40 像素。

这是为坦克和子弹提供坐标的 function

tank_pos_list = []
bullet_list = []

def spawn_tank():
    global tank_pos_list
    global bullet_list
    qx, qy = pygame.mouse.get_pos()
    tankxy = [(qx - 35), (qy - 35)]
    tank_pos_list.append(tankxy)
    bullet_list.append(tankxy)

这是我的坦克和子弹运动 class。

class MovementClass:

    global bullet_list
    global tank_pos_list
    tank_surf = pygame.image.load("tank.png")
    bullet = pygame.image.load("bullet.png")

    def movetank(self, tankimg):
        for tank_pos in tank_pos_list:
            screen.blit(tankimg, (tank_pos[0], tank_pos[1]))
            tank_pos[0] += 0.2

    def movebullet(self, bulletimg):
        for j in range(len(bullet_list)):
            newx = (bullet_list[j][0] + 35)
            screen.blit(bulletimg, (newx, (bullet_list[j][1] + 34)))
            newx += 1

这是我的主要 function

def main():
    global new_tanks
    global spawner
    global tank_pos_list
    global fire_bullet_tank
    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")
    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 and my < 550 and mx < 500:
                    spawn_tank()

                    spawner = False
                if tanks.isOver(mouse_pos):
                    spawner = True

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

        if len(tank_pos_list) >= 11:
            tank_pos_list.pop(-1)

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

        #movement()

        movingtank = MovementClass()
        movingtank.movetank(pygame.image.load("tank.png"))
        movingbullet = MovementClass()
        movingbullet.movebullet(pygame.image.load("bullet.png"))

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

当你跑

for tank_pos in tank_pos_list:
   ...
   tank_pos[0] += 0.2

您正在更改tank_pos_list列表中列表中的第一个值。 请注意,您将tankxy添加到spawn_tank的两个列表中,这样您就可以看到tank_pos_listbullet_list的变化。 这是您在此处更改的两个列表中的相同列表。

但是当你跑

for j in range(len(bullet_list)):
    newx = (bullet_list[j][0] + 35)
    ...
    newx += 1

您只需创建一个新变量newx并更改其值; 但是您永远不会更改bullet_list中列表的任何值。


还有一些注意事项:

MovementClass类没有内部 state; 每帧创建 2 个新实例基本上是没用的。 改用全局函数(没有类)或仅内联这些函数。

您从磁盘的每一帧加载"tank.png""bullet.png" 您应该只在主循环之外加载图像一次。 否则,它很快就会成为主要的性能杀手。


Try to create a class or multiple classes that represents the different actors of your game (pygame offers the Sprite class for this) and implement the behaviour and state in that class. 虽然这不是最先进的技术,但它是小型游戏 go 恕我直言的正确方法。

例如,也许看看我为其他问题所做的这个答案,这几乎是关于如何创建这样一个游戏的分步指南。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM