【问题标题】:How to create a global variable from a local and store it's previous value?如何从本地创建全局变量并存储它的先前值?
【发布时间】:2020-01-11 14:40:06
【问题描述】:

在我的 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)

这里是问题所在的主循环(阅读代码中的cmets)

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

【问题讨论】:

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


    【解决方案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))
    

    【讨论】:

    • 当然,我怎么没想到呢?感谢您的帮助,我真的很感激
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 2019-10-02
    • 2014-11-29
    • 2014-02-07
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多