AttributeError:'元组'对象没有属性' image'错误信息

时间:2016-03-07 21:58:33

标签: python tuples

import pygame

#Colours used throughout the game
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BackGround = ('D:\Idea 2\sky_colour_image.jpg', [0,0])

# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

#Player class
class Player(pygame.sprite.Sprite):

#Player image
def __init__(self):



    super().__init__()

    # Create an image of the block, and fill it with a color.
    # This could also be an image loaded from the disk.
    width = 40
    height = 40
    self.image = pygame.Surface([width, height])
    self.image.fill(BLUE)

    # Set a referance to the image rect.
    self.rect = self.image.get_rect()

    # Set speed vector of player
    self.change_x = 0
    self.change_y = 0

    # List of sprites we can bump against
    self.level = None

def update(self):
    """ Move the player. """
    # Gravity
    self.calc_grav()

    # Move left/right
    self.rect.x += self.change_x

    # See if we hit anything
    block_hit_list =     pygame.sprite.spritecollide(self,self.level.platform_list, False)
    for block in block_hit_list:
        # If we are moving right,
        # set our right side to the left side of the item we hit
        if self.change_x > 0:
            self.rect.right = block.rect.left
        elif self.change_x < 0:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = block.rect.right

    # Move up/down
    self.rect.y += self.change_y

    # Check and see if we hit anything
    block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
    for block in block_hit_list:

        # Reset our position based on the top/bottom of the object.
        if self.change_y > 0:
            self.rect.bottom = block.rect.top
        elif self.change_y < 0:
            self.rect.top = block.rect.bottom

        # Stop our vertical movement
        self.change_y = 0

def calc_grav(self):

    if self.change_y == 0:
        self.change_y = 1
    else:
        self.change_y += .35

    # See if we are on the ground.
    if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
        self.change_y = 0
        self.rect.y = SCREEN_HEIGHT - self.rect.height

def jump(self):


    # move down a bit and see if there is a platform below us.
    # Move down 2 pixels because it doesn't work well if we only move down
    # 1 when working with a platform moving down.
    self.rect.y += 2
    platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
    self.rect.y -= 2

    # If it is ok to jump, set our speed upwards
    if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
        self.change_y = -10

# Player-controlled movement:
def go_left(self):   
    self.change_x = -6

def go_right(self):
    self.change_x = 6

def stop(self):
    self.change_x = 0


class Platform(pygame.sprite.Sprite):

def __init__(self, width, height):
    super().__init__()

    self.image = pygame.Surface([width, height])
    self.image.fill(GREEN)

    self.rect = self.image.get_rect()


class Level(object):


def __init__(self, player):

    self.platform_list = pygame.sprite.Group()
    self.enemy_list = pygame.sprite.Group()
    self.player = player


# Update everythign on this level
def update(self):
    self.platform_list.update()
    self.enemy_list.update()

def draw(self, screen):

    # Draw the background
    screen.fill(BLUE)

    # Draw all the sprite lists that we have
    self.platform_list.draw(screen)
    self.enemy_list.draw(screen)

# Background class
class Background(pygame.sprite.Sprite):

  def __init__(self, image_file, location):
    pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
    self.image = pygame.image.load("D:\Idea 2\sky_colour_image.jpg")
    self.rect = self.image.get_rect()
    self.rect.left, self.rect.top = location



# Create platforms for the level
class Level_01(Level):


def __init__(self, player):

    # Call the parent constructor
    Level.__init__(self, player)

    # Array with width, height, x, and y of platform
    level = [[210, 70, 500, 500],
             [210, 70, 200, 400],
             [210, 70, 600, 300],
             ]

    # Go through the array above and add platforms
    for platform in level:
        block = Platform(platform[0], platform[1])
        block.rect.x = platform[2]
        block.rect.y = platform[3]
        block.player = self.player
        self.platform_list.add(block)


def main():

pygame.init()

# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Platformer Jumper")

# Create the player
player = Player()

# Create all the levels
level_list = []
level_list.append( Level_01(player) )

# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]

active_sprite_list = pygame.sprite.Group()
player.level = current_level

player.rect.x = 340
player.rect.y = SCREEN_HEIGHT - player.rect.height
active_sprite_list.add(player)

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()



# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.go_left()
            if event.key == pygame.K_RIGHT:
                player.go_right()
            if event.key == pygame.K_UP:
                player.jump()


        screen.fill([255, 255, 255])
        screen.blit(BackGround.image, BackGround.rect)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and player.change_x < 0:
                player.stop()
            if event.key == pygame.K_RIGHT and player.change_x > 0:
                player.stop()

    # Update the player.
    active_sprite_list.update()

    # Update items in the level
    current_level.update()

    # If the player gets near the right side, shift the world left (-x)
    if player.rect.right > SCREEN_WIDTH:
        player.rect.right = SCREEN_WIDTH

    # If the player gets near the left side, shift the world right (+x)
    if player.rect.left < 0:
        player.rect.left = 0


    current_level.draw(screen)
    active_sprite_list.draw(screen)



    # Limit to 60 frames per second
    clock.tick(60)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()


if __name__ == "__main__":
main()

到目前为止,这应该是(没什么令人兴奋的)一个简单的平台跳线与天空背景。为计算项目创建游戏,而不是在python中体验过的游戏。目标是尽快添加移动平台等,如果有人可以帮助解决这个或错误,我会很感激。

1 个答案:

答案 0 :(得分:0)

基本上你要问一个物体(在这里看起来像self)并要求它找到它的一部分。但元组没有图像属性。

此处不确定,但我认为您可能会出现缩进错误。

def __init__()应缩进,因为它是您班级的一种方法。 Python不使用花括号,但空间很重要。