简体   繁体   中英

pygame.time.clock() or pygame.clock.tick() error

I have a main loop in my code where it loops everything and when I run my code I get this error:

    Traceback (most recent call last):
      File "C:\Users\Javier\Documents\Python Pygame\First Game\First 
    Game.py", line 17, in <module>
        clock=pygame.time.clock()
    AttributeError: module 'pygame.time' has no attribute 'clock'

I understand what the error is: it means it can't find the 'class' clock and in another instance where I used pygame.tick.clock() I got the same error.

    #import modules here

    import pygame

    #define what colours are
    black=(0,0,0)
    white=(255,255,255)
    turquoise=(64,224,208)
    #initalise pygame
    pygame.init()
    #set up screen
    screen=pygame.display.set_mode((400,400))
    #what the screen is called and backgroud colour
    screen.fill(black)
    pygame.display.set_caption("Snake!")
    #set a variable for how quick the game runs
    clock=pygame.time.clock()

    '''making future references easier
    eval just takes code as a string and runs it
    eg:
    instead of doing print(5+8)
    you can do eval(print(5+8))'''
    def key(key):
        return pygame.ket.get_pressed()[eval("pygame.K_"+key)]
    #draw the snake
    snake=(50,150,150,50)
    #display.flip shows what has been drawn on the screen eg the snake
    while True:
        pygame.draw.rect(screen,turquoise,snake)
        pygame.display.flip()
        clock.tick(60)

When I comment the problem code ( clock.tick() and pygame.time.clock ) the window opens and displays the turquoise "snake" but it has no limiting factor that slows down the rate of the loop executing.

To use the pygame clock:

clock = pygame.time.Clock()

Then,

clock.tick(60)

I believe it's pygame.time.Clock and pygame.time.Clock.tick

https://www.pygame.org/docs/ref/time.html#pygame.time.Clock

As you noted, the module couldn't find the class Clock . In Python, the convention for Classes is to capitalize them.

Go figure but documenation says it's

pygame.time.Clock

note the uppercase C in clock vs lowercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM