Pygame属性错误" Pygame.rect"

时间:2015-12-24 06:19:35

标签: python button attributes pygame

  

AttributeError:' pygame.Rect'对象没有属性' center'

这是我尝试运行我的代码时遇到的错误,我是python的新手所以它可能是一个菜鸟错误,如果它是我会欣赏如何解决它的解释。我目前正在使用一个关于如何将文本添加到按钮(#button)的教程(Tutorial),但是当我尝试将文本集中在按钮上时,我的当前和非常少的知识协调它给出了我属性错误。感谢任何帮助:)谢谢。

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]


#Colours
black = (0,0,0)
blue = (0,0,255)
green = (0,200,0)
red = (200,0,0)
green_bright = (0,255,0)
red_bright = (255,0,0)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()


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

def game_intro():

    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects("Broom!", largeText)
    TextRect.center = ((width/2),(height/2))

intro = True

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

    mouse = pygame.mouse.get_pos()

    screen.fill(blue)

    screen.blit(TextSurf, TextRect)

    #Button

    if 75+100 > mouse[0] > 75 and 400+50 > mouse[1] > 400:
        pygame.draw.rect(screen, green_bright,(75,400,100,50))
    else:
        pygame.draw.rect(screen, green,(75,400,100,50))

    smallText = pygame.font.Font("freesansbold.ttf",20)
    TextSurf, TextRect = text_objects("GO", smallText)
    TextRect.centre = ((75+(100/2)),(400+(50/2)))
    screen.blit(TextSurf, TextRect)

    if 550+100 > mouse[0] > 550 and 400+50 > mouse[1] > 400:
        pygame.draw.rect(screen, red_bright,(550,400,100,50))
    else:
        pygame.draw.rect(screen, red,(550,400,100,50))

    TextSurf, TextRect = text_objects("Exit", smallText)
    TextRect.centre = ((550+(100/2)),(400+(50/2)))
    screen.blit(TextSurf, TextRect)

    pygame.display.flip()
    pygame.display.update()
    clock.tick(15)

game_intro()

1 个答案:

答案 0 :(得分:0)

该问题是由拼写错误引起的。虚拟属性的名称是 center 但不是 centre。见pygame.Rect