如何reset/reinitalize变量?
How to reset/reinitalize variables?
我 运行 遇到了变量问题。首先看到这段代码然后我会解释我的问题:
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 ')
我希望当它再次运行时,运行dom 编号不能再次相同。这与我游戏中敌人的生成有关,它在死后在同一位置生成。我不想让它那样做,所以我尝试这样做:
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 ')
没有用的坚果,因为我在定义它之前使用了变量。
好吧,我想我明白了。
一种方法是每次生成随机项目时从列表中删除该项目。
另一种方法是使用 2 个随机项:x 和 y,这样您获得相同分数的可能性不大。
但是如果您不能使用这些解决方案,那么您可以更改随机种子:
https://www.w3schools.com/python/ref_random_seed.asp
要阻止同一个号码被再次使用:
from random import choice
blacklisted_numbers = [] #this might need to be global if you want to use this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350,260), (600, 260), (100, 80),(350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
#now its out of the loop so its not blacklisted
#Code all of your stuff
blacklisted_numbers.append(number)
总而言之,我的想法是,如果你创建一个空数组,你可以将所有使用过的 random_locations 附加到那里,并为数字分配一个随机选择,当它到达 while 循环时,如果第一个分配的值不在那里循环不会 运行 并且它会 运行 你的代码然后在所有这些之后你将元组列入黑名单。如果这不是您要问的,请进一步说明
我的做法是将位置移动到程序的顶部:
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80), (600, 80)]
test_sucks = None
if pygame.Rect.colliderect(hammer_rect, mole_rect):
randomsucks = random.choice(random_locations)
while randomsucks == test_sucks:
randomsucks = random.choice(random_locations)
test_sucks = randomsucks
...
# use randomsucks
我 运行 遇到了变量问题。首先看到这段代码然后我会解释我的问题:
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 ')
我希望当它再次运行时,运行dom 编号不能再次相同。这与我游戏中敌人的生成有关,它在死后在同一位置生成。我不想让它那样做,所以我尝试这样做:
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 ')
没有用的坚果,因为我在定义它之前使用了变量。
好吧,我想我明白了。
一种方法是每次生成随机项目时从列表中删除该项目。
另一种方法是使用 2 个随机项:x 和 y,这样您获得相同分数的可能性不大。
但是如果您不能使用这些解决方案,那么您可以更改随机种子: https://www.w3schools.com/python/ref_random_seed.asp
要阻止同一个号码被再次使用:
from random import choice
blacklisted_numbers = [] #this might need to be global if you want to use this function multiple times
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350,260), (600, 260), (100, 80),(350, 80), (600, 80)]
number = choice(random_locations)
while number in blacklisted_numbers:
number = choice(random_locations)
#now its out of the loop so its not blacklisted
#Code all of your stuff
blacklisted_numbers.append(number)
总而言之,我的想法是,如果你创建一个空数组,你可以将所有使用过的 random_locations 附加到那里,并为数字分配一个随机选择,当它到达 while 循环时,如果第一个分配的值不在那里循环不会 运行 并且它会 运行 你的代码然后在所有这些之后你将元组列入黑名单。如果这不是您要问的,请进一步说明
我的做法是将位置移动到程序的顶部:
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80), (350, 80), (600, 80)]
test_sucks = None
if pygame.Rect.colliderect(hammer_rect, mole_rect):
randomsucks = random.choice(random_locations)
while randomsucks == test_sucks:
randomsucks = random.choice(random_locations)
test_sucks = randomsucks
...
# use randomsucks