How to create a global variable from a local and store it's previous value?

大憨熊 提交于 2020-01-15 07:55:06

问题


In my pygame game, I want the bullet to be able to detect when it's within the given hitbox. To do this I need to create a global variable from a local. However, the global variable is updated to the new hitbox every time a new object appears. That doesn't let me track the previous hitbox and detect when a bullet is within the old objects that are still on the screen. How do I prevent this? How should I store the previous value of a?

Here is the class where I'm defining the hitbox and the other features of the object.

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)

Here is the main loop where the problem is located (read the comments in the code)

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:


Just use nested loops. You have to check each bullet against each asteroid:

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


来源:https://stackoverflow.com/questions/57860627/how-to-create-a-global-variable-from-a-local-and-store-its-previous-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!