社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我被python中的变量困住了

chutiya • 3 年前 • 1212 次点击  

我是python的初学者,因此对它了解不多,遇到了一个问题。首先看一下这段代码,然后我将解释我的问题

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

但这不起作用,因为我在定义变量之前使用了它

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133414
 
1212 次点击  
文章 [ 3 ]  |  最新文章 3 年前