2016-03-07 62 views
-3
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中經歷的遊戲。目標是儘快加入移動平臺等,如果有人可以幫助,或者我會很感激的錯誤。AttributeError:'tuple'對象沒有屬性'image'錯誤信息

+1

哪裏出錯?你能不能先縮小它,而是發佈你的整個代碼? – aribeiro

+0

請[編輯]你的問題到[mcve],重點放在「最小」......你已經在我們身上拋棄了一段代碼,並且可能大部分與你所犯的錯誤沒有任何關係接收。相反,請包含_full_堆棧跟蹤。堆棧跟蹤是Python解釋器如何試圖解釋出了什麼問題,在哪裏以及爲什麼......哪些是你想要回答的問題。 (它也會告訴你哪些類和方法在錯誤發生時正在使用,因此很可能會在你的MCVE中結束。) –

回答

0

基本上,你問的是一個物體(看起來像self這裏),並要求它爲它的一部分的圖像。但是元組沒有圖像的屬性。

不確定在這裏,但我認爲你可能有一個縮進錯誤。

def __init__()應縮進,因爲它是您班上的一種方法。 Python不使用大括號,但空間很重要。

相關問題