不断在我的pygame项目上出现属性错误,我不明白为什么

时间:2019-11-04 18:34:14

标签: python pygame

import pygame, random, time
# main function where we call all other functions, start the game loop, quit pygame and clean up the window. Inside we create a game object, display surface, and start the game loop by calling the play method on the game object. There is also a set caption with the title of the game.
def main():
    pygame.init()
    size =(500,400)
    surface=pygame.display.set_mode(size)
    pygame.display.set_caption('Pong v2')
    game = Game(surface)
    game.play()
    pygame.quit()
# This is where we define the class game    
class Game():
    # an object in this class represents a complete game

    # here we initialize a game. self is the game that is initialized surface is the display window surface object we also set default values for continuing the game and closing the window. we also define what fps we are running the game at, and define the velocity color position and radius of the ball
    def __init__(self,surface):

        # defining surface, fps, background color
        self.surface=surface
        self.FPS=120
        self.bg_color=pygame.Color('black')
        screen_width = surface.get_width()
        screen_height = surface.get_height()

        # defining ball attributes
        ball_radius=10
        ball_pos = [random.randint(ball_radius, screen_width-ball_radius), 
                    random.randint(ball_radius, screen_height-ball_radius)]
        ball_color=pygame.Color('white')
        ball_velocity=[2,1]
        self.ball=Ball(ball_pos,ball_radius,ball_color,ball_velocity,surface)

        # defining paddle attributes

        rect_left=[50,450]
        rect_top=225
        rect_height=60
        rect_width=10
        self.rect=Rect(rect_left[0],rect_top,rect_width,rect_height,surface)


        self.game_Clock=pygame.time.Clock()
        self.close_clicked=False
        self.continue_game=True
        self.score=0
        self.frame_counter=0

    def play(self):
        # game is played until player clicks close
        while not self.close_clicked:  

            self.handle_events()

            self.draw()
            # if nothing sets this to false the game will continue to update
            if self.continue_game:
                self.update()

            self.game_Clock.tick(self.FPS)
    # score is drawn onto the screen (unimportant this is just playing with a feature for the next version), we define color font background etc of the score message and update score upon points being scored
    def draw_score(self):
        font_color = pygame.Color("white")
        font_bg    = pygame.Color("black")
        font       = pygame.font.SysFont("arial", 72)
        text_img   = font.render("Score: " + str(self.score), True, font_color, font_bg)     
        text_pos   = (0,0)
        self.surface.blit(text_img, text_pos)        
    # ball, surface, score, and two paddles are drawn, pygame also updates this drawing once per frame    
    def draw(self):
        self.surface.fill(self.bg_color)

        self.draw_score()
        #pygame.draw.rect(self.surface,pygame.Color('blue'),(50,225,10,50))
        #pygame.draw.rect(self.surface,pygame.Color('red'),(450,225,10,50))
        self.rect.draw()

        self.ball.draw()


        pygame.display.update()
    # score value set to default of 0 we tell ball to move and add 1 to frame counter upon each update. update game object for the next frame 
    def update(self):
        self.ball.move()
        self.score=0
        self.frame_counter+=self.frame_counter+1



    # here we setup an event loop and figure out if the action to end the game has been done
    def handle_events(self):
        events=pygame.event.get()
        for event in events:
            if event.type== pygame.QUIT:
                self.close_clicked=True
# user defined class ball            
class Ball:
    # self is the ball to intialize. color/center/radius are defined for the ball that is initialized
    def __init__(self,center,radius,color,velocity,surface):
        self.center=center
        self.radius=radius
        self.color=color
        self.velocity=velocity
        self.surface=surface
    # screen size is determined and edge of ball is checked that it is not touching the edge. if it is touching the edge it bounces and reverses velocity   
    def move(self):
        screen_width=self.surface.get_width()
        screen_height=self.surface.get_height()
        screen_size=(screen_width,screen_height)
        for i in range(0,len(self.center)):
            self.center[i]+=self.velocity[i]
            if (self.center[i]<=0 + self.radius or self.center[i]>=screen_size[i] - self.radius):
                self.velocity[i]=-self.velocity[i]
    # ball is drawn            
    def draw(self):
        pygame.draw.circle(self.surface,self.color,self.center,self.radius)


class Rect:
    def __init__(self,left,top,width,height,surface):
        self.left=left
        self.top=top
        self.width=width
        self.height=height
        self.surface=surface

    def draw(self):
        pygame.draw.rect(self.surface,pygame.Color('blue'),self.rect)

main()

好,所以问题是我不断收到属性错误,该错误表明我的Rect对象没有属性rect,但是我不知道为什么会这样?我正在尝试在称为pong的游戏的窗口的任一侧绘制一个矩形(假定是桨),因此我需要将矩形作为一个类来进行碰撞检测,最终我想使桨移动。我阅读了文档,但我仍然不明白为什么这不起作用

File "/home/user/pong2.py", line 129, in <module>
    pygame.draw.rect(self.surface,pygame.Color('blue'),self.rect)
builtins.AttributeError: 'Rect' object has no attribute 'rect'

1 个答案:

答案 0 :(得分:2)

您必须在类self.rect的构造函数中设置属性Rect。使用参数lefttopwidthheight创建pygame.Rect的实例。例如:

class Rect:
    def __init__(self,left,top,width,height,surface):
        self.surface=surface
        self.rect = pygame.Rect(left, top, width, height)

    def draw(self):
        pygame.draw.rect(self.surface,pygame.Color('blue'),self.rect)

但是您根本不需要属性self.leftself.topself.widthself.height
 如果您想知道矩形的边界,则可以使用pygame.Rect对象self.rect.leftself.rect.topself.rect.widthself.rect.height < / p>