我遇到了变量问题。首先看到这段代码,然后我会解释我的问题:
if pygame.Rect.colliderect(hammer_rect, mole_rect):
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80),
(350, 80), (600, 80)]
randomsucks = random.choice(random_locations)
test_sucks = randomsucks
mole_spawn_new(randomsucks[0], randomsucks[1])
randomsucks = 0
score += 1
print('Score was increased by one ')
我希望当它再次运行时,随机数不能再次相同。这与我的游戏中敌人的生成有关,它在死亡后在同一位置生成。我不希望事情发展成这样,所以我尝试这样做:
if pygame.Rect.colliderect(hammer_rect, mole_rect):
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80),
(350, 80), (600, 80)]
randomsucks = random.choice(random_locations)
while randomsucks == test_sucks:
if test_sucks == randomsucks:
randomsucks = random.choice(random_locations)
test_sucks = randomsucks
mole_spawn_new(randomsucks[0], randomsucks[1])
randomsucks = 0
score += 1
print('Score was increased by one ')
但这不起作用,因为我在定义变量之前就使用了它。