【发布时间】:2015-12-24 06:19:35
【问题描述】:
AttributeError: 'pygame.Rect' 对象没有属性 'center'
这是我在尝试运行代码时遇到的错误,我对 python 很陌生,所以这可能是一个新手错误,如果是,我将不胜感激有关如何修复它的解释。我目前正在使用教程 (Tutorial) 关于如何在按钮 (#button) 上添加文本,但据我目前所知,当我尝试将文本围绕按钮坐标居中时,它给了我一个属性错误。任何帮助表示赞赏:) 谢谢。
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()
【问题讨论】:
-
你打错了:不是
centre,而是center -
哈哈没看到,谢谢帮助
标签: python button attributes pygame