AttributeError:'模块'对象没有属性' sleep' 。我的迷你游戏doenst工作

时间:2016-02-07 21:49:03

标签: python

以下是代码:

    import pygame
import sys
import os
from sys import *
from pygame import *
from os import *

pygame.init()
             #Colour Acronym Chart
#|  Black = Blck  |  Grey = Gry   | Dark = Drk    | White = Wht
#|  Deep = Dp     |  Metal = Mtl  | Light = Lht   |
#|  Blue = Bl     |  Baby = Bby   | Maroon = Mrn  |
#|  Red = Rd      |  Fire = Fr    | Orange = Orng |

# Window Colour Index
Wht = (255, 255, 255)
Blck = (0, 0, 0)            
Dp_Gry = (32, 32, 32)       
Mtl_Gry = (96, 96, 96)    
Lht_Gry = (160, 160, 160)

Dp_Bl = (0, 0, 102)
Lht_Bby_Bl = (0, 128, 255)

dark_maroon = (102, 0, 0)
fire_red = (255, 0, 0)
light_orange = (255, 128, 0)
#END OF COLOUR MODULE

display_width = 800
display_height = 600



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Hard Drive')
clock = pygame.time.Clock()

carImg = pygame.image.load('car1.png')
car_width = 45
car_height = 45

def car(x,y):
    gameDisplay.blit(carImg,(x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, Blck)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()



    def crash():
    message_display('You Crashed')

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIG HT:
                    x_change = 0

        x += x_change

        gameDisplay.fill(Wht)
       car(x,y)

       if x > display_width - car_width or x < 0:
           crash()


    pygame.display.update()
    clock.tick(60)

game_loop()
pygame.quit()
quit()

现在这是错误!

Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "C:/Users/*****/Desktop/PyGame/frame.py", line 104, in <module>
    game_loop()
  File "C:/Users/*****/Desktop/PyGame/frame.py", line 98, in game_loop
    crash()
  File "C:/Users/*****/Desktop/PyGame/frame.py", line 65, in crash
    message_display('You Crashed')
  File "C:/Users/*****/Desktop/PyGame/frame.py", line 58, in message_display
    time.sleep(2)
AttributeError: 'module' object has no attribute 'sleep'
>>> ================================ RESTART ================================
>>> 

这样可以帮助你理解我在做什么;我正在制作一个小型赛车游戏,但我收到了这个错误。然而,在我添加播放器撞坏汽车的弹出窗口之前,它运行正常,但是通过快速弹出窗口的实现,它崩溃了!

为什么会这样。我确实理解Python,但我在这方面有点迷失。

1 个答案:

答案 0 :(得分:3)

您需要导入模块time以修复错误'module' object has no attribute 'sleep' 尝试在第一行写import time
删除行time.sleep(2),因为您根本不需要它。