如果玩家与精灵发生碰撞,如何更改玩家的图像

时间:2013-12-29 02:06:14

标签: python windows image pygame sprite

我制作了游戏的第一级。我想到的第二个层面可能是,当玩家碰撞或触摸汽车时,当前玩家的图像将消失,玩家的新图像将成为汽车,就像玩家在车内并驾驶汽车一样。汽车是否也可以像玩家左,右,上,下等一样具有相同的功能和控制......如何更改玩家图像并仍然获得与玩家相同的控制?我为汽车和玩家上过课,但我不知道下一步该做什么。任何帮助将不胜感激。 Python 2.6,Pygame sprites,windows 7。

这是汽车的课程。

class Car(pygame.sprite.Sprite):
change_x = 0
change_y = 0
movingPlatform_list = None
frame_since_collision = 0

def __init__(self, color, width, height):
    pygame.sprite.Sprite.__init__(self)

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

    self.rect = self.image.get_rect()

def update(self, player, block_list, movingPlatform_list):
    # Move left/right
    self.rect.x += self.change_x

    # See if we hit anything
    block_hit_list = pygame.sprite.spritecollide(self, block_list, False)
    movingPlatform_hit_list = pygame.sprite.spritecollide(self, movingPlatform_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
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = block.rect.right

    for movingPlatform in movingPlatform_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 = movingPlatform.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = movingPlatform.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, block_list, False)
    movingPlatform_hit_list = pygame.sprite.spritecollide(self, movingPlatform_list, False)

    for block in block_hit_list:

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

        # 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

        # Stop our vertical movement
        self.change_y = 0

    for movingPlatform in movingPlatform_hit_list:

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

        self.rect.x += movingPlatform.change_x

    # Increment frame counter
    self.frame_since_collision += 1

# Calculate effect of gravity.
def calc_grav(self):
    self.change_y += .35

    # See if we are on the ground.
    if self.rect.y >= 485 and self.change_y >= 0:
        self.change_y = 0
        self.rect.y = 485
        self.frame_since_collision = 0

这是玩家的课程。玩家类中的大部分(def更新)都是sprite collides命令。

class Player(pygame.sprite.Sprite): 

# -- Attributes 
# Set speed vector of player
change_x = 0
change_y = 0

# Set to true if it is ok to jump
jump_ok = True

# Moving Platform determining if player is on the moving platform
movingPlatform_list = None

# Frames
frame = 0

# Count of frames since the player 
# collided against something. Used to prevent jumping
# when we haven't hit anything.
frame_since_collision = 0

# -- Methods 
# Constructor function 
def __init__(self, x, y): 
    # Call the parent's constructor 
    pygame.sprite.Sprite.__init__(self) 

    # List that the cat images will be saved in.
    self.images=[]
    # Load all the cat images, from cat1.png to cat8.png.
    for i in range(1,9):
        img = pygame.image.load("cat"+str(i)+".png").convert()
        img.set_colorkey(white)
        self.images.append(img)

    # By default, use image 0
    self.image = self.images[0]

    # Fetch the rectangle object that has the dimensions of the image
    # image.
    # Update the position of this object by setting the values 
    # of rect.x and rect.y

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

# Change the speed of the player
def changespeed(self,x,y):
    self.change_x+=x
    self.change_y+=y

# Find a new position for the player 
def update(self, blocks, tables, chairLeft, chairRight, carMode): 

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

    # If we are moving right to left
    if self.change_x < 0:
        # Update our frame counter
        self.frame += 1

        # We go from 0...3. If we are above image 3, reset to 0
        # Multiply by 4 because we flip the image every 4 frames
        if self.frame > 3*4:
            self.frame = 0

        # Grab the image, do floor division by 4 because we flip
        # every 4 frames. 
        # Frames 0...3 -> image[0]
        # Frames 4...7 -> image[1]
        # etc.
        self.image = self.images[self.frame//4]

    # Move left to right. About the same as before, but use
    # images 4...7 instead of 0...3. Note that we add 4 in the last
    # line to do this.
    if self.change_x > 0:
        self.frame += 1
        if self.frame > 3*4:
            self.frame = 0
        self.image = self.images[self.frame//4+4]

    # See if we hit anything
    block_hit_list = pygame.sprite.spritecollide(self, blocks, False)
    movingPlatform_hit_list = pygame.sprite.spritecollide(self, movingPlatform_list, False)
    table_hit_list = pygame.sprite.spritecollide(self, table_list, False)
    chairLeft_hit_list = pygame.sprite.spritecollide(self, chairLeft_list, False)
    chairRight_hit_list = pygame.sprite.spritecollide(self, chairRight_list, False)
    computerBlock_hit_list = pygame.sprite.spritecollide(self, computerBlock_list, False)
    shooter_hit_list = pygame.sprite.spritecollide(self, shooter_list, False)
    explosion_hit_list = pygame.sprite.spritecollide(self, explosion_list, False)
    door_hit_list = pygame.sprite.spritecollide(self, door_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
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = block.rect.right

    for movingPlatform in movingPlatform_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 = movingPlatform.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = movingPlatform.rect.right

    for table in table_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 = table.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = table.rect.right

    for chairLeft in chairLeft_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 = chairLeft.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = chairLeft.rect.right

    for chairRight in chairRight_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 = chairRight.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = chairRight.rect.right

    for shooter in shooter_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 = shooter.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = shooter.rect.right

    for explosion in explosion_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 = explosion.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = explosion.rect.right

    for computerBlock in computerBlock_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 = computerBlock.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = computerBlock.rect.right

    for door in door_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 = door.rect.left
        else:
            # Otherwise if we are moving left, do the opposite.
            self.rect.left = door.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, blocks, False)
    movingPlatform_hit_list = pygame.sprite.spritecollide(self, movingPlatform_list, False)
    table_hit_list = pygame.sprite.spritecollide(self, table_list, False)
    chairRight_hit_list = pygame.sprite.spritecollide(self, chairRight_list, False)
    chairLeft_hit_list = pygame.sprite.spritecollide(self, chairLeft_list, False)
    computerBlock_hit_list = pygame.sprite.spritecollide(self, computerBlock_list, False)
    shooter_hit_list = pygame.sprite.spritecollide(self, shooter_list, False)
    explosion_hit_list = pygame.sprite.spritecollide(self, explosion_list, False)
    door_hit_list = pygame.sprite.spritecollide(self, door_list, False)

    for block in block_hit_list:

        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

        # 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

        # Stop our vertical movement
        self.change_y = 0

    # If we haven't hit anything in a while, allow us jump
    if self.frame_since_collision > 10:
        self.jump_ok = False

    for movingPlatform in movingPlatform_hit_list:

        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

        self.rect.x += movingPlatform.change_x

    # Increment frame counter
    self.frame_since_collision += 1

    for table in table_hit_list:
        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

    # Increment frame counter
    self.frame_since_collision += 1

    for chairLeft in chairLeft_hit_list:
        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

    # Increment frame counter
    self.frame_since_collision += 1

    for chairRight in chairRight_hit_list:
        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

    # Increment frame counter
    self.frame_since_collision += 1

    for shooter in shooter_hit_list:
        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

    # Increment frame counter
    self.frame_since_collision += 1

    for explosion in explosion_hit_list:
        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

    for computerBlock in computerBlock_hit_list:

        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

     # Increment frame counter
    self.frame_since_collision += 1

    for door in door_hit_list:

        # We hit something below us. Set the boolean to flag that we can jump
        if self.change_y > 0:
            self.jump_ok = True

        # Keep track of the last time we hit something
        self.frame_since_collision = 0

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

        # Stop our vertical movement
        self.change_y = 0

     # Increment frame counter
    self.frame_since_collision += 1

# Calculate effect of gravity.
def calc_grav(self):
    self.change_y += .35

    # See if we are on the ground.
    if self.rect.y >= 460 and self.change_y >= 0:
        self.change_y = 0
        self.rect.y = 460
        self.frame_since_collision = 0
        self.jump_ok = True

# Called when user hits 'jump' button
def jump(self,blocks):

    # If it is ok to jump, set our speed upwards
    if self.jump_ok:
        self.change_y = -8

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

当玩家触摸汽车时,为玩家设置不同的图像。然后用另一个按钮将其更改回来。当您只更改图像时,控件将保持不变。