Home > front end >  How to reset/reinitalize variables?
How to reset/reinitalize variables?

Time:05-03

I've ran into an problem with variables. First see this code then I will explain my problem:

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

I want that when it runs again, the random numbers can't be same again. This is the related to the spawn of the enemy in my game and it is spawning at the same location after dying. I don't wanted it to go that way, so I tried to do this:

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

Nut that didn't work because I was using the variable before defining it.

CodePudding user response:

well I think I get the problem here.

One approach would be to remove the item from the list each time you generate a random item.

Another way you can do this is by using 2 random items: x and y, this way the probability that you will get the same point is not very likely.

But If you can't use these solutions then you can change the random seed: https://www.w3schools.com/python/ref_random_seed.asp

CodePudding user response:

To stop the same number from being used again:

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)

For summary what my thinking is that if you create an empty array you can append all used random_locations there and assign a random choice to the number and when it hits the while loop, if the first assigned value is not in there the loop will not run and it will run your code then after all of that you blacklist the tuple. If this was not what you are asking please clarify more

CodePudding user response:

The way I would do it is to move the locations to the top of the program:

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
  • Related