错误:AttributeError:module' pygame'没有属性'颜色'

时间:2017-07-12 18:45:20

标签: python python-3.x pygame

我是python和游戏开发的新手。我正在学习Udemy教程" Python游戏开发:创建蛇游戏"。以下是我的计划:

#Snake Game!
#Our game imports
import pygame
import sys
import random
import time

check_errors = pygame.init()

#(6,0)

if check_errors[1] > 0:
    print("(!) Had {0} initializing error, exiting....".format(check_errors[1]))
    sys.exit(-1)
else:
    print("(+) PyGame sucessfully initialized")

# Play surface. Create a black surface for game


playSurface = pygame.display.set_mode((720,460))
pygame.display.set_caption("SNAKE GAME")
time.sleep(5)

#Colors - r,g,b -- red green blue

red = pygame.Colors(255,0,0) #gameover
green = pygame.Colors(0,255,0) #snake
#black
black = pygame.Colors(0,0,0) #score
white = pygame.Colors(255,255,255) #background
brown = pygame.Colors(165,42,42) #food

# FPS frames per seconds controller 
fpsController = pygame.time.Clock()

#where do you want the snake to start
#Important variables

snakePos = [100,50] #should be less than the screen size - (720,460)
snakeBoday =  [[100,50],[90,50],[80,50]]

foodPos = [random.randrange(1,72)*10,random.randrange(1,46)*10]
foodSpawn = True

direction = 'RIGHT'
changeto = direction

#Game over function

def gameOver():
    myFont = pygame.font.SysFont('monaco',72)
    GOsurf = myFont.render('Game Over!!', True, red)
    GOrect = Gosurf.get_rect()
    Gorect.midtop = (360, 15)
    playSurface.blit(GOsurf,GOrect)
    python.display.flip()

gameOver()
time.sleep(10)

当我运行此代码时,我收到一条错误消息:

(+) PyGame sucessfully initialized
Traceback (most recent call last):
  File "C:\Apps\Python Project\Python Snake Game\snakeGame.py", line 28, in <module>
    red = pygame.Colors(255,0,0) #gameover
AttributeError: module 'pygame' has no attribute 'Colors'
[Finished in 6.9s]

我用Google搜索了这个错误。我遵循了这个解决方案(Why am I getting this AttributeError ? (python3 , pygame)):

enter image description here

我无法在我的工作区中找到此文件。我可以遵循其他任何解决方案吗?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

这里的问题是没有pygame.Color属性,您正在寻找import numpy as np from sklearn.metrics import jaccard_similarity_score #The y_pred represents the values that the program has found y_pred = [0,0,1,0,0,0,1,1,1,1,0,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,0,0,0,1,1,0,0,1,1,0,1,1,1] #The y_true represents the values that are actually correct y_true = [1,0,0,1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1,1,1,0,1,0,1,1,1] iou = jaccard_similarity_score(y_true, y_pred) 没有记录here