如何修复随机选择生成器?

时间:2018-09-03 11:18:50

标签: python

我的python游戏无法正常工作,序列开头为:

if int(total_time) > 10:   

不会触发,但是当我按D,C或W时,我得到的是“您打开了东西”文本。据我所知,这里的代码是正确的,只是行不通。我使用或prevtime允许您第一次使用它。

import random, time, pygame, sys
from pygame.locals import *
total_time = time.clock()
pygame.init()
XQR_prevtime = 0
ppayh_prevtime = 0
pu_ekaw_prevtime = 0
diff = 1
windowSurface = pygame.display.set_mode((400,400),0,32)
time.sleep(3)
XQR_awakened = False
ppayh_awakened = False
pu_ekaw_awakened = False

    if int(total_time) > 10:

        if int(XQR_prevtime) > (12 - diff) or int(XQR_prevtime) == 0 or XQR_awakened == True:
            if XQR_awakened == True:
                print("You left something open...")
                time.sleep(2)
                print("And a mystery came in")
                time.sleep(2)
                sys.exit()
            if random.randint(0,diff) == 1:
                print(3)
                XQR_prevtime = time.clock()
                door_opening.play()
                XQR_awakened = True

        if int(ppayh_prevtime) > (12 - diff) or int(ppayh_prevtime) == 0 or ppayh_awakened == True:
            if ppayh_awakened == True:
                print("You left something open...")
                time.sleep(2)
                print("And a friend came in")
                time.sleep(2)
                sys.exit()
            if randint(0,diff) == 1:
                print(3)
                ppayh_prevtime = time.clock()
                closet_opening.play()
                ppayh_awakened = True

        if int(pu_ekaw_prevtime) > (12 - diff) or int(pu_ekaw_prevtime) == 0 or pu_ekaw_prevtime == True:
            if ekaw_up_awakened == True:
                print("You left something open...")
                time.sleep(2)
                print("And an answer came in")
                time.sleep(2)
                sys.exit()
            if randint(0,diff) == 1:
                print(3)
                pu_ekaw_prevtime = time.clock()
                window_opening.play()
                pu_ekaw_awakened = True

1 个答案:

答案 0 :(得分:2)

total_time 没有变化,因此您永远无法达到条件。

total_time = time.clock()

float分配一个数值(total_time)。没有对time.clock()函数的引用,该函数返回只是一个普通的float对象,而不是计时器对象。

正常的float值不变,它们是不可变的。 total_time的值不会随着游戏的运行而改变。

如果要测量经过的时间,只需继续致电time.clock()

if time.clock() > 10:

您无需在此处将浮点值转换为int,使用整数进行比较就可以了。