gpt4 book ai didi

python - 如何从本地创建全局变量并存储它的先前值?

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:59 24 4
gpt4 key购买 nike

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

最佳答案

只需使用嵌套循环。您必须针对每颗小行星检查每颗子弹:

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

关于python - 如何从本地创建全局变量并存储它的先前值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com