三个文件:
blank.py:
import sys
import pygame
from setting import settings
import heroes
def run_game():
pygame.init()
setting=settings()
screen=pygame.display.set_mode((setting.width,setting.high))
chenping=heroes.teacher_chen(screen)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
screen.fill(setting.color)
chenping.blit()
pygame.display.flip()
run_game()
heroes.py:
import pygame
class teacher_chen():
def __init__(self,screen):
self.screen=screen
self.image=pygame.image.load('photos/chenping.bmp')
self.screen_rect=screen.get_rect()
self.rect.centerx=self.screen_rect.centerx #在这里报错:'teacher_chen' object has no attribute 'rect'
self.rect.bottom=self.screen_rect.bottom
def blit(self):
self.screen.blit(self.image,self.rect)
完整报错信息:
'teacher_chen' object has no attribute 'rect'
File "C:\Users\70707\Documents\python\game_alien\heroes.py", line 8, in init
self.rect.centerx=self.screen_rect.centerx
File "C:\Users\70707\Documents\python\game_alien\blank.py", line 23, in run_game
chenping=heroes.teacher_chen(screen)
File "C:\Users\70707\Documents\python\game_alien\blank.py", line 31, in
run_game()