2014-06-18 37 views
-3

請幫我用這段代碼,它不工作,我已經失去了男人睡眠的夜晚:(我想我的精靈翻轉,我明白我有所有的代碼在這裏做,但有什麼阻止它?爲什麼我的精靈翻轉代碼在python中工作?

import pygame 



# Colours 
black=(0,0,0) 
white=(255,255,255) 
blue=(0,0,255) 
green=(0,255,0) 
red=(255,0,0) 
purple=(97,0,160) 
lightgrey=(173,173,173) 
grey=(87,83,85) 
bronze=(201,106,75) 
rock=(219,79,3) 
steel=(220,229,221) 
ice=(198,231,249) 
giygas=(128,0,16) 


class Wall(pygame.sprite.Sprite): 
    """This class represents the bar at the bottom that the player controls """ 

    def __init__(self, x, y, width, height, color): 
     """ Constructor function """ 

     # Call the parent's constructor 
     pygame.sprite.Sprite.__init__(self) 

     # Create a wall, of the size specified in the parameters 
     self.image = pygame.Surface([width, height]) 
     self.image.fill(color) 

     # Make our top-left corner the passed-in location. 
     self.rect = self.image.get_rect() 
     self.rect.y = y 
     self.rect.x = x 

class SpriteSheet(object): 
    """ Class used to grab images out of a sprite sheet. """ 
    # This points to our sprite sheet image 
    sprite_sheet = None 

    def __init__(self, filename): 
     """ Constructor. Pass in the file name of the sprite sheet. """ 

     # Load the sprite sheet. 
     self.sprite_sheet = pygame.image.load(filename).convert_alpha() # Set speed vector 



    def get_image(self, x, y, width, height): 
     """ Grab a single image out of a larger spritesheet 
      Pass in the x, y location of the sprite 
      and the width and height of the sprite. """ 

     # Create a new blank image 
     image = pygame.Surface([width, height]).convert() 
     image.fill(white) 
     image.set_colorkey(white) 

     # Copy the sprite from the large sheet onto the smaller image 
     image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))  

     # Return the image 
     return image 
class Player(pygame.sprite.Sprite): 

    change_x = 0 
    change_y = 0 
    sword_x = "off" 
    sword_y = "off" 
    shield_x = "off" 
    shield_y = "off" 

    toasterwalking_frames_l = [] 
    toasterwalking_frames_r = [] 
    #This holds all the images for the animated walk up/down 
    #of our player 
    toasterwalking_frames_u = [] 
    toasterwalking_frames_d = [] 

    # This holds all the images for the buttery coating walk left/right 
    #of our player 
    buttertoasterwalking_frames_l = [] 
    buttertoasterwalking_frames_r = [] 

    # This holds all the images for the buttery coating walk up/down 
    buttertoasterwalking_frames_u = [] 
    buttertoasterwalking_frames_d = [] 
    def __init__(self): 
     """ Constructor function """ 


     # Call the parent's constructor 
     pygame.sprite.Sprite.__init__(self) 


     '''sides''' 
     sprite_sheet = SpriteSheet("toasterwalksprite.png") 
     # Load all the right facing images into a list 
     image = sprite_sheet.get_image(0, 0, 13, 20) 
     self.toasterwalking_frames_r.append(image) 
     image = sprite_sheet.get_image(14, 0, 13, 20) 
     self.toasterwalking_frames_r.append(image) 



     # Load all the right facing images, then flip them 
     # to face left. 
     image = sprite_sheet.get_image(0, 0, 13, 20) 
     image = pygame.transform.flip(image, True, False) 
     self.toasterwalking_frames_l.append(image) 
     image = sprite_sheet.get_image(14, 0, 13, 20) 
     image = pygame.transform.flip(image, True, False) 
     self.toasterwalking_frames_l.append(image) 


     '''down''' 
     sprite_sheet = SpriteSheet("toasterwalksprite.png") 
     # Load all the right facing images into a list 
     image = sprite_sheet.get_image(0, 20, 13, 19) 
     self.toasterwalking_frames_d.append(image) 

     #Load all the down facing images, then flip them 
     #to face other way. 
     image = sprite_sheet.get_image(0, 20, 13, 19) 
     image = pygame.transform.flip(image, True, False) 
     self.toasterwalking_frames_d.append(image) 

     '''up''' 
     sprite_sheet = SpriteSheet("toasterwalksprite.png") 
     # Load all the right facing images into a list 
     image = sprite_sheet 
     image = sprite_sheet.get_image(14, 19, 13, 21) 
     self.toasterwalking_frames_u.append(image) 

     #Load all the down facing images, then flip them 
     #to face other way. 
     image = sprite_sheet.get_image(14, 19, 13, 21) 
     image = pygame.transform.flip(image, True, False) 
     self.toasterwalking_frames_u.append(image) 

     self.image = self.toasterwalking_frames_r[0] 


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




    def update(self): 
     """ Move the player. """ 

     # Move left/right/up/down 
     self.rect.x += self.change_x 
     pos = self.rect.x + self.level.world_shift 
     if self.direction == "R": 
      frame = (pos // 30) % len(self.toasterwalking_frames_r) 
      self.image = self.toasterwalking_frames_r[frame] 
     else: 
      frame = (pos // 30) % len(self.toasterwalking_frames_l) 
      self.image = self.toasterwalking_frames_l[frame] 

     self.rect.y += self.change_y 
     pos = self.rect.y + self.level.world_shift 
     if self.direction == "U": 
      frame = (pos // 30) % len(self.toasterwalking_frames_u) 
      self.image = self.toasterwalking_frames_u[frame] 

     self.rect.y += self.change_y 
     pos = self.rect.y + self.level.world_shift 
     if self.direction == "D": 
      frame = (pos // 30) % len(self.toasterwalking_frames_d) 
      self.image = self.toasterwalking_frames_d[frame] 


    # Player-controlled movement: 
    def go_left(self): 
     """ Called when the user hits the left arrow. """ 
     self.change_x = -2 
     self.direction = "L" 

    def go_right(self): 
     """ Called when the user hits the right arrow. """ 
     self.change_x = 2 
     self.direction = "R" 

    def go_down(self): 
     """ Called when the user hits the left arrow. """ 
     self.change_y = 1 
     self.direction = "D" 

    def go_up(self): 
     """ Called when the user hits the right arrow. """ 
     self.change_y = -1 
     self.direction = "U" 

    def stop(self): 
     """ Called when the user lets off the keyboard. """ 
     self.change_x = 0 
     self.change_y = 0 
     self.sword_x = "off" 
     self.sword_y = "off" 


    def changespeed(self, x, y): 
     """ Change the speed of the player. Called with a keypress. """ 
     self.change_x += x 
     self.change_y += y 

    def move(self, walls): 
     """ Find a new position for the player """ 

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

     # Collision detection 
     # Did this update cause us to hit a wall? 
     block_hit_list = pygame.sprite.spritecollide(self, walls, 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 
      else: 
       # Otherwise if we are moving left, do the opposite. 
       self.rect.left = block.rect.right 
      #music = pygame.mixer.Sound("RSE Bump.wav") 
      #music.play() 

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

     pos = self.rect.y 
     if self.direction == "U": 
      frame = (pos // 30) % len(self.toasterwalking_frames_u) 
      self.image = self.toasterwalking_frames_u[frame] 

     self.rect.y += self.change_y 
     pos = self.rect.y 
     if self.direction == "D": 
      frame = (pos // 30) % len(self.toasterwalking_frames_d) 
      self.image = self.toasterwalking_frames_d[frame] 

     # Check and see if we hit anything 
     block_hit_list = pygame.sprite.spritecollide(self, walls, 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 
      else: 
       self.rect.top = block.rect.bottom 

      #music = pygame.mixer.Sound("RSE Bump.wav") 
      #music.play() 

'''___________________Walls+Rooms_____________________'''     

class Room(object): 
    """ Base class for all rooms. """ 

    """ Each room has a list of walls, and of enemy sprites. """ 
    wall_list = None 
    enemy_sprites = None 

    def __init__(self): 
     """ Constructor, create our lists. """ 
     self.wall_list = pygame.sprite.Group() 
     self.enemy_sprites = pygame.sprite.Group() 
#Room 1 
class Room1(Room): 
    """This creates all the walls in room 1""" 
    def __init__(self): 
     Room.__init__(self) 
     # Make the walls. (x_pos, y_pos, width, height) 

     # This is a list of walls. Each is in the form [x, y, width, height] 
     walls = [[0, 0, 20, 350, lightgrey], 
       [0, 350, 20, 250, lightgrey], 
       [780, 0, 20, 250, lightgrey], 
       [780, 350, 20, 250, lightgrey], 
       [20, 0, 760, 20, lightgrey], 
       [20, 580, 760, 20,lightgrey], 
       [95, 20, 20, 500, lightgrey], 
       [175, 500, 20, 100, lightgrey], 
       [195, 410, 20, 110, lightgrey], 
       [215, 320, 20, 110, lightgrey], 
       [235, 245, 20, 100, lightgrey], 
       [235, 230, 120, 20,lightgrey], 
       [400, 230, 400, 20,lightgrey], 
       [335, 180, 20, 50, lightgrey], 
       [350, 180, 385, 20,lightgrey], 
       [335, 180, 20, 50, lightgrey], 
       [435, 245, 20, 95, lightgrey], 
       [455, 320, 20, 110, lightgrey], 
       [475, 400, 20, 110, lightgrey], 
       [575, 300, 20, 280, lightgrey], 
       [703, 250, 20, 280, lightgrey], 
        ] 

    # Loop through the list. Create the wall, add it to the list 
     for item in walls: 
      wall = Wall(item[0], item[1], item[2], item[3], item[4]) 
      self.wall_list.add(wall) 
# Room 2 
class Room2(Room): 
    """This creates all the walls in room 2""" 
    def __init__(self): 
     Room.__init__(self) 

     walls = [[0, 0, 20, 250, bronze], 
       [0, 350, 20, 250, bronze], 
       [780, 0, 20, 250, bronze], 
       [780, 350, 20, 250, bronze], 
       [20, 0, 760, 20, bronze], 
       [20, 580, 760, 20, bronze], 
       [80, 50, 18, 95, rock], 
       [191, 50, 18, 95, rock], 
       [80, 30, 129, 20, rock], 
       [335, 50, 18, 95, steel], 
       [446, 50, 18, 95, steel], 
       [335, 30, 129, 20, steel], 
       [591, 50, 18, 95, ice], 
       [703, 50, 18, 95, ice], 
       [591, 30, 130, 20, ice], 
       ] 



     for item in walls: 
      wall = Wall(item[0], item[1], item[2], item[3], item[4]) 
      self.wall_list.add(wall) 


#Room 3 
class Room3(Room): 
    """This creates all the walls in room 3""" 
    def __init__(self): 
     Room.__init__(self) 

     walls = [[0, 0, 20, 250, giygas], 
       [0, 350, 20, 250, giygas], 
       [780, 0, 20, 400, giygas], 
       [780, 350, 20, 250, giygas], 
       [20, 0, 760, 70, giygas], 
       [20, 530, 760, 80, giygas], 
       [20, 350, 20, 20, purple], 
       [40, 370, 20, 20, purple], 
       [60, 390, 20, 20, purple], 
       [80, 410, 20, 20, purple], 
       [100, 430, 20, 20, purple], 
       [120, 450, 20, 20, purple], 
       [140, 470, 20, 20, purple], 
       [160, 490, 20, 20, purple], 
       [180, 510, 20, 20, purple], 
       [760, 350, 20, 20, purple], 
       [740, 370, 20, 20, purple], 
       [720, 390, 20, 20, purple], 
       [700, 410, 20, 20, purple], 
       [680, 430, 20, 20, purple], 
       [660, 450, 20, 20, purple], 
       [640, 470, 20, 20, purple], 
       [620, 490, 20, 20, purple], 
       [600, 510, 20, 20, purple], 
       [20, 230, 20, 20, purple], 
       [40, 210, 20, 20, purple], 
       [60, 190, 20, 20, purple], 
       [80, 170, 20, 20, purple], 
       [100, 150, 20, 20, purple], 
       [120, 130, 20, 20, purple], 
       [140, 110, 20, 20, purple], 
       [160, 90, 20, 20, purple], 
       [180, 70, 20, 20, purple], 
       [760, 230, 20, 20, purple], 
       [740, 210, 20, 20, purple], 
       [720, 190, 20, 20, purple], 
       [700, 170, 20, 20, purple], 
       [680, 150, 20, 20, purple], 
       [660, 130, 20, 20, purple], 
       [640, 110, 20, 20, purple], 
       [620, 90, 20, 20, purple], 
       [600, 70, 20, 20, purple], 
       [200, 50, 400, 20, purple], 
       [200, 530, 400, 20, purple], 
       [20, 510, 160, 20, giygas], 
       [20, 490, 140, 20, giygas], 
       [20, 470, 120, 20, giygas], 
       [20, 450, 100, 20, giygas], 
       [20, 430, 80, 20, giygas], 
       [20, 410, 60, 20, giygas], 
       [20, 390, 40, 20, giygas], 
       [20, 370, 20, 20, giygas], 
       [20, 210, 20, 20, giygas], 
       [20, 190, 40, 20, giygas], 
       [20, 170, 60, 20, giygas], 
       [20, 150, 80, 20, giygas], 
       [20, 130, 100, 20, giygas], 
       [20, 110, 120, 20, giygas], 
       [20, 90, 140, 20, giygas], 
       [20, 70, 160, 20, giygas], 
       [620, 70, 160, 20, giygas], 
       [640, 90, 140, 20, giygas], 
       [660, 110, 120, 20, giygas], 
       [680, 130, 160, 20, giygas], 
       [700, 150, 180, 20, giygas], 
       [720, 170, 200, 20, giygas], 
       [740, 190, 210, 20, giygas], 
       [760, 210, 230, 20, giygas], 
       [760, 370, 230, 20, giygas], 
       [740, 390, 210, 20, giygas], 
       [720, 410, 190, 20, giygas], 
       [700, 430, 170, 20, giygas], 
       [680, 450, 150, 20, giygas], 
       [660, 470, 130, 20, giygas], 
       [640, 490, 180, 20, giygas], 
       [620, 510, 180, 20, giygas], 
       [760, 240, 20, 130, purple] 
        ]  

     for item in walls: 
      wall = Wall(item[0], item[1], item[2], item[3], item[4]) 
      self.wall_list.add(wall) 



def main(): 
    """ Main Program """ 

    # Call this function so the Pygame library can initialize itself 
    pygame.init() 

    # Create an 800x600 sized screen 
    screen = pygame.display.set_mode([800, 600]) 
    map1 = pygame.mixer.Sound("Encounter - Fire Emblem Gaiden.wav") 
    map1.play() 

    # Set the title of the window 
    pygame.display.set_caption('Toaster') 

    # Create the player paddle object 
    player = Player() 
    moving_sprites = pygame.sprite.Group() 
    moving_sprites.add(player) 

    rooms = [] 

    room = Room1() 
    rooms.append(room) 

    room = Room2() 
    rooms.append(room) 


    room = Room3() 
    rooms.append(room) 


    current_room_no = 0 
    current_room = rooms[current_room_no] 

    clock = pygame.time.Clock() 

    done = False 

    while not done: 

     # --- Event Processing --- 

    while not done: 
     for event in pygame.event.get(): # User did something 
      if event.type == pygame.QUIT: # If user clicked close 
       done = True # Flag that we are done so we exit this loop 

      if event.type == pygame.KEYDOWN: 
       #this means if the user holds down the key 
       '''walking actions''' 
       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.go_up() 
       if event.key == pygame.K_DOWN: 
        player.go_down() 

      if event.type == pygame.KEYUP: 
       #this means if the user lets off of the key 
       '''stopping walking actions''' 
       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() 
       if event.key == pygame.K_UP and player.change_y < 0: 
        player.stop() 
       if event.key == pygame.K_DOWN and player.change_y > 0: 
        player.stop() 

     # --- Game Logic --- 

     player.move(current_room.wall_list) 

     if player.rect.x < -15: 
      if current_room_no == 0: 
       current_room_no = 2 
       current_room = rooms[current_room_no] 
       player.rect.x = 790 
      elif current_room_no == 2: 
       current_room_no = 1 
       current_room = rooms[current_room_no] 
       player.rect.x = 790 
      else: 
       current_room_no = 0 
       current_room = rooms[current_room_no] 
       player.rect.x = 790 

     if player.rect.x > 801: 
      if current_room_no == 0: 
       current_room_no = 1 
       current_room = rooms[current_room_no] 
       player.rect.x = 0 
      elif current_room_no == 1: 
       current_room_no = 2 
       current_room = rooms[current_room_no] 
       player.rect.x = 0 
      else: 
       current_room_no = 0 
       current_room = rooms[current_room_no] 
       player.rect.x = 0 

     # --- Drawing --- 
     screen.fill(grey) 

     for y_offset in range(0,772,16): 
      pygame.draw.line(screen, black, [16+y_offset,0], [16+y_offset,600]) 
     for x_offset in range(0,732,16): 
      pygame.draw.line(screen, black, [0,16+x_offset], [800,16+x_offset]) 



     moving_sprites.draw(screen) 
     current_room.wall_list.draw(screen) 


     pygame.display.flip() 

     clock.tick(60) 

    pygame.quit() 

if __name__ == "__main__": 
    main() 
+2

那麼它在做什麼或不做什麼? –

+0

我試圖運行此代碼,但我得到了錯誤'AttributeError:'玩家'對象沒有屬性'方向'(第242行) – furas

+0

「不要問關於你沒有試圖找到答案的問題(顯示你的工作!)「 - http://stackoverflow.com/tour 爲什麼我們應該關心你的問題,如果你不這樣做? – msw

回答

1

我運行代碼,我得到了追溯

Traceback (most recent call last): 
    File "main.py", line 558, in <module> 
    main() 
    File "main.py", line 509, in main 
    player.move(current_room.wall_list) 
    File "main.py", line 242, in move 
    if self.direction == "U": 
AttributeError: 'Player' object has no attribute 'direction' 

也許你並沒有在控制檯中運行代碼/終端
,因爲你會發現這個問題,立即。

我在Player.__init__()中加self.direction = "",它工作。