Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从局部变量创建全局变量并将其存储';以前的值是多少?_Python_Arrays_Python 3.x_Function_Pygame - Fatal编程技术网

Python 如何从局部变量创建全局变量并将其存储';以前的值是多少?

Python 如何从局部变量创建全局变量并将其存储';以前的值是多少?,python,arrays,python-3.x,function,pygame,Python,Arrays,Python 3.x,Function,Pygame,在我的pygame游戏中,我希望子弹能够在给定的命中框内检测到。为此,我需要从局部变量创建一个全局变量。但是,每次出现新对象时,全局变量都会更新为新的hitbox。这不允许我跟踪上一个hitbox并检测子弹何时位于仍在屏幕上的旧对象中。我如何防止这种情况?如何存储a的上一个值 这里是我定义hitbox和对象的其他特性的类 class Enemy: def __init__(self, y, width, height): self.width = width

在我的pygame游戏中,我希望子弹能够在给定的命中框内检测到。为此,我需要从局部变量创建一个全局变量。但是,每次出现新对象时,全局变量都会更新为新的hitbox。这不允许我跟踪上一个hitbox并检测子弹何时位于仍在屏幕上的旧对象中。我如何防止这种情况?如何存储a的上一个值

这里是我定义hitbox和对象的其他特性的类

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)
这是问题所在的主循环(阅读代码中的注释)

小行星=[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')] 数字=[0,1,2,3,4] 小行星在屏幕上显示=[] 岩石=敌人(-140,64,64) 运行=真 clock=pygame.time.clock() 运行时: last=pygame.time.get_ticks() 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT: 运行=错误 elif event.type==我的事件\u id: x=random.randrange(屏幕宽度-64*2) 索引=随机。选择(数字) 小行星在屏幕上。附加(敌人(岩石。y,岩石。宽度,岩石。高度)) 全局a#如果我在这里将a定义为全局a,我将能够检测到 #当子弹在目标的命中框内时 #最新添加的对象,因为每个 #对象进入屏幕,但不进入其他对象。 对于屏幕上的“小行星”中的: 如果-141a.hitbox[0]: 如果bullet.y-bullet.heighta.hitbox[1]: 摇滚乐 子弹.弹孔(子弹.索引(子弹)) 如果0只需使用嵌套循环即可。你必须对照每颗小行星检查每颗子弹:

#移动小行星
对于屏幕上的“小行星”中的:
如果-141a.hitbox[0]:
如果bullet.y-bullet.heighta.hitbox[1]:
摇滚乐
子弹.弹孔(子弹.索引(子弹))
#移动剩余的子弹
对于子弹中的子弹:
如果0
当然,为什么我没想到呢?谢谢你的帮助,我真的很感激
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))