“实体”对象没有值“宽度”

时间:2019-01-28 05:09:42

标签: python python-3.x pygame

好的,所以我试图创建一个项目,在该项目中可以创建可以跳转的红色矩形,但是此后,我现在收到错误消息“实体没有属性“宽度”。”说我100%理解班级,但是我相信我在正确地使用它。我究竟做错了什么?我希望对我的代码提出任何建议!这是我的项目的代码。

# Imports--------------------------------------------------------------------------------------------------------------#

import pygame


# initialization-------------------------------------------------------------------------------------------------------#

pygame.init()

# Flags----------------------------------------------------------------------------------------------------------------#

gameExit = False

# Variables -----------------------------------------------------------------------------------------------------------#

display_height = 500
display_width = 500
bg = (0, 0, 0)
isJump = False
# Colors --------------------------------------------------------------------------------------------------------------#

FUCHSIA = (255, 0, 255)
PURPLE = (128, 0, 128)
TEAL = (0, 128, 128)
LIME = (0, 255, 0)
GREEN = (0, 128, 0)
OLIVE = (128, 128, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)
MAROON = (128, 0, 0)
SILVER = (192, 192, 192)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)
NAVY = (0, 0, 128)
AQUA = (0, 255, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Draw Screen----------------------------------------------------------------------------------------------------------#

win = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Airbender Training")
Clock = pygame.time.Clock()

# Mobs ----------------------------------------------------------------------------------------------------------------#
class entity:
    #Entities will be every mob in the game
    def __init__(self, width, height, velocity, x, y, sprite):

        entity.width = self.width
        entity.height = self.height
        entity.x_position = self.x_position
        entity.y_position = self.y_position
        entity.sprite = self.sprite


player_width = 64
player_height = 64
player_velocity = 5
player_x_position = 5
player_y_position = 5
player_sprite = pygame.draw.rect(win, RED, (player_x_position, player_y_position, player_width, player_height))
    # ^This makes a placeholder red square^
player = entity(player_width, player_height, player_velocity, player_x_position, player_y_position, player_sprite)

# Functions -----------------------------------------------------------------------------------------------------------#

def drawGameWindow():

    pygame.win.fill(bg)
    pygame.win.blit(player_sprite, player.x_position, player.y_position)
    Clock.tick(60)
    pygame.display.update()

def jump():

    jumpCount = 1
    while jumpCount <= entity.height * 1.5:
        entity.y_position += jumpCount
        jumpCount += 1
    while jumpCount >= entity.height * 1.5:
        entity.y_position -= jumpCount
        jumpCount -= 1

# Main Loop------------------------------------------------------------------------------------------------------------#

while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event == pygame.k_RIGHT or event == pygame.k_D:
                player.x_position += 5
            if event == pygame.k_LEFT or event == pygame.k_A:
                player.x_position -= 5
            while not isJump:
                if event == pygame.k_up or event == pygame.k_SPACE:
                    jump(player)

    drawGameWindow()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

整个代码中既没有标识Entity也没有Width,因此错误消息为:

  

“实体”没有属性“宽度”

错误发生在:

class entity:
    #Entities will be every mob in the game
    def __init__(self, width, height, velocity, x, y, sprite):

        entity.width = self.width

必须为:

class entity:
    #Entities will be every mob in the game
    def __init__(self, width, height, velocity, x, y, sprite):

        self.width = width
        self.height = height
        self.x_position = x_position
        self.y_position = y_position
        self.sprite = sprite

构造函数(或任何其他非静态)方法的fist参数是对象的实例。

您想要将参数width分配给对象.width的成员self。因此必须是:

<object>.<member> = <parameter>
self.width = width

注意entity是类的名称,但是没有对象entity