如何从局部变量创建全局变量并存储其先前值?

时间:2019-09-09 20:11:48

标签: python arrays python-3.x function pygame

在我的pygame游戏中,我希望子弹能够检测出它何时位于给定的击键箱内。为此,我需要从本地创建全局变量。但是,每次出现新对象时,全局变量都会更新到新的Hitbox。这不能让我跟踪先前的点击框并检测子弹何时位于仍在屏幕上的旧对象中。我该如何预防?我应该如何存储a的先前值?

这是我在其中定义对象的点击框和其他功能的类。

class Enemy:
    def __init__(self, y, width, height):
        self.width = width
        self.height = height
        self.vel = 1.5
        self.y = y
        self.x = random.randrange(screen_width - 64 * 2)
        self.index = random.choice(number)
        self.hitboxes = [(self.x + 68, self.y + 68, self.width - 10, self.height - 14),
                         (self.x + 38, self.y + 47, self.width + 20, self.height - 5),
                         (self.x + 18, self.y + 12, self.width + 32, self.height + 30),
                         (self.x + 20, self.y + 32, self.width + 16, self.height + 5),
                         (self.x + 4, self.y + 7, self.width - 24, self.height - 31)]  # hitbox list
        self.hitbox = self.hitboxes[self.index]  # selecting hitbox from list  

    def draw(self, win):
        win.blit(asteroids[self.index], (self.x, self.y))
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

这是问题所在的主循环(请阅读代码中的注释)

asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),
             pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]

number = [0, 1, 2, 3, 4]

asteroids_on_screen = []

rock = Enemy(-140, 64, 64)

run = True
clock = pygame.time.Clock()
while run:
    last = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == my_event_id:
            x = random.randrange(screen_width - 64 * 2)
            index = random.choice(number)
            asteroids_on_screen.append(Enemy(rock.y, rock.width, rock.height))
    global a  # if I define a as a global here I will be able to detect
              # when the bullet is within the hitbox of the 
              # newest added object, since a gets updated for each 
              # object that enters the screen, but not the other ones.
    for a in asteroids_on_screen:
        if -141 < a.y < 500:
            a.y += a.vel
            a.hitbox = (a.hitbox[0], a.hitbox[1] + a.vel, a.hitbox[2], a.hitbox[3])
        else:
            asteroids_on_screen.pop(asteroids_on_screen.index(a))

    for bullet in bullets:
        if bullet.x + bullet.width < a.hitbox[0] + a.hitbox[2] and bullet.x - bullet.width > a.hitbox[0]:
            if bullet.y - bullet.height < a.hitbox[1] + a.hitbox[3] and bullet.y + bullet.height > a.hitbox[1]:
                rock.hit()
                bullets.pop(bullets.index(bullet))
        if 0 < bullet.y < 500:
            bullet.y -= bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

1 个答案:

答案 0 :(得分:1)

只需使用嵌套循环即可。您必须对照每个小行星检查每个子弹:

# move the asteroids
for a in asteroids_on_screen:
    if -141 < a.y < 500:
        a.y += a.vel
        a.hitbox = (a.hitbox[0], a.hitbox[1] + a.vel, a.hitbox[2], a.hitbox[3])
    else:
        asteroids_on_screen.pop(asteroids_on_screen.index(a))

# hit test for each combination of asteroid and bullet
for a in asteroids_on_screen:
    for bullet in bullets:
        if bullet.x + bullet.width < a.hitbox[0] + a.hitbox[2] and bullet.x - bullet.width > a.hitbox[0]:
            if bullet.y - bullet.height < a.hitbox[1] + a.hitbox[3] and bullet.y + bullet.height > a.hitbox[1]:
                rock.hit()
                bullets.pop(bullets.index(bullet))

# move the remaining bullets
for bullet in bullets:
    if 0 < bullet.y < 500:
        bullet.y -= bullet.vel
    else:
        bullets.pop(bullets.index(bullet))