python - 在每个级别开始时更改玩家位置
问题描述
我正在 pygame 中制作平台类型的游戏。我希望玩家和敌人的坐标在每个关卡开始时都不同,但我不知道该怎么做。
这是代码:
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
LIGHTRED = (191, 52, 52)
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 500
class SpriteSheet(object):
def __init__(self, file_name):
self.sprite_sheet = pygame.image.load(file_name).convert()
def get_image(self, x, y, width, height, colour):
image = pygame.Surface([width, height]).convert()
image.set_colorkey(colour)
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
return image
class Skeleton(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# -- Methods
def __init__(self):
""" Constructor function """
super().__init__()
sprite_sheet = SpriteSheet("enemy.png")
self.image = sprite_sheet.get_image(0, 0, 64, 64, LIGHTRED)
self.image = pygame.transform.scale(self.image, [32, 32])
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):
""" Calculate effect of gravity. """
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .47
# 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):
""" Called when user hits 'jump' button. """
# 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):
""" Called when the user hits the left arrow. """
self.change_x = 6
def go_right(self):
""" Called when the user hits the right arrow. """
self.change_x = -6
def stop(self):
""" Called when the user lets off the keyboard. """
self.change_x = 0
class Player(pygame.sprite.Sprite):
""" This class represents the bar at the bottom that the player
controls. """
# -- Methods
def __init__(self):
""" Constructor function """
super().__init__()
sprite_sheet = SpriteSheet("Player (1).png")
self.image = sprite_sheet.get_image(0, 0, 64, 64, BLACK)
self.image = pygame.transform.scale(self.image, [32, 32])
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):
""" Calculate effect of gravity. """
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .47
# 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):
""" Called when user hits 'jump' button. """
# 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):
""" Called when the user hits the left arrow. """
self.change_x = -6
def go_right(self):
""" Called when the user hits the right arrow. """
self.change_x = 6
def stop(self):
""" Called when the user lets off the keyboard. """
self.change_x = 0
class Portal(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
sprite_sheet = SpriteSheet("portal.png")
self.image = sprite_sheet.get_image(0, 100, 118, 197, BLACK)
self.image = pygame.transform.scale(self.image, [72, 72])
self.rect = self.image.get_rect()
class Platform(pygame.sprite.Sprite):
""" Platform the user can jump on """
def __init__(self, width, height):
""" Platform constructor. Assumes constructed with user passing in
an array of 5 numbers like what's defined at the top of this
code. """
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(GREEN)
self.rect = self.image.get_rect()
class Level(object):
""" This is a generic super-class used to define a level.
Create a child class for each level with level-specific
info. """
def __init__(self, skeleton, player):
""" Constructor. Pass in a handle to player. Needed for when moving platforms
collide with the player. """
self.platform_list = pygame.sprite.Group()
self.enemy_list = pygame.sprite.Group()
self.skeleton = skeleton
self.player = player
# Background image
self.background = None
# Update everythign on this level
def update(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
def draw(self, screen):
""" Draw everything on this level. """
# Draw all the sprite lists that we have
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
# Create platforms for the level
class Level_01(Level):
""" Definition for level 1. """
def __init__(self, skeleton, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, skeleton, player)
# Array with width, height, x, and y of platform
level = [[59, 500, 0, 0],
[1000-59, 56, 59, 0],
[59, 500, 1000-59, 0],
[1000, 59, 0, 500-55],
[185, 56, 0, 278],
[3, 26, 185, 284],
[62, 59, 188, 278+56],
[55, 59*4+42, 317, 0],
[68, 56, 188+61, 59*3-10],
[62*4, 120, 317+60, 334],
[62*2+3, 56, 814, 278],
[62, 56, 750, 334],
[55, 56*3, 567, 56],
[62*2+2, 56, 622, 167],
[55, 56, 629, 223],
]
# 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.skeleton = self.skeleton
block.player = self.player
self.platform_list.add(block)
class Level_02(Level):
""" Definition for level 1. """
def __init__(self, skeleton, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, skeleton, player)
# Array with width, height, x, and y of platform
level = [[59, 500, 0, 0],
[1000-59, 56, 59, 0],
[59, 500, 1000-59, 0],
[1000, 59, 0, 500-55],
[310, 56, 0, 167],
[130, 53, 817, 171],
[183, 50, 503, 395],
[58*4+13, 54, 378, 224],
[55, 56, 379, 278],
[58, 55, 690, 169],
]
# 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.skeleton = self.skeleton
block.player = self.player
self.platform_list.add(block)
def main():
""" Main Program """
pygame.init()
# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
lvl_2 = pygame.image.load("lvl_2.png")
lvl_1 = pygame.image.load("lvl_1.png")
level_list = []
skeleton = Skeleton()
player = Player()
portal = Portal()
level_list.append(Level_01(skeleton, player) )
level_list.append(Level_02(skeleton, player) )
current_level_no = 0
current_level = level_list[current_level_no]
lvl_2 = pygame.transform.scale(lvl_2, [1000, 500])
lvl_1 = pygame.transform.scale(lvl_1, [1000, 500])
pygame.display.set_caption("Platformer Jumper")
active_sprite_list = pygame.sprite.Group()
player_list = pygame.sprite.Group()
portal_list = pygame.sprite.Group()
skeleton.level = current_level
player.level = current_level
skeleton.rect.x = 340
skeleton.rect.y = SCREEN_HEIGHT - skeleton.rect.height - 50
player.rect.x = 340
player.rect.y = SCREEN_HEIGHT - player.rect.height-50
portal.rect.x = 460
portal.rect.y = 260
active_sprite_list.add(skeleton)
active_sprite_list.add(player)
active_sprite_list.add(portal)
portal_list.add(portal)
player_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()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
skeleton.go_left()
player.go_left()
if event.key == pygame.K_RIGHT:
skeleton.go_right()
player.go_right()
if event.key == pygame.K_UP:
skeleton.jump()
player.jump()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and skeleton.change_x > 0:
skeleton.stop()
if event.key == pygame.K_RIGHT and skeleton.change_x < 0:
skeleton.stop()
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()
screen.fill(WHITE)
if current_level_no == 0:
screen.blit(lvl_1, [0, 0])
if current_level_no == 1:
screen.blit(lvl_2, [0, 0])
portal.rect.x = 570
portal.rect.y = 320
#screen.blit(bg, [0, 0])
active_sprite_list.update()
if pygame.sprite.groupcollide(portal_list, player_list, False, False):
current_level_no += 1
print(current_level_no)
current_level = level_list[current_level_no]
player.level = current_level
skeleton.level = current_level
#Update items in the level
current_level.update()
# If the player gets near the right side, shift the world left (-x)
if skeleton.rect.right > SCREEN_WIDTH:
skeleton.rect.right = SCREEN_WIDTH
# If the player gets near the left side, shift the world right (+x)
if skeleton.rect.left < 0:
skeleton.rect.left = 0
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
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
active_sprite_list.draw(screen)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
if __name__ == "__main__":
main()
有人可以帮帮我吗?
(另外请不要阅读我在代码中写的评论,他们都搞砸了)。
解决方案
我会说在玩家和敌人类中创建一个函数,如下所示:
def set_pos(self,x_pos, y_pos):
self.rect.x = x_pos
self.rect.y = y_pos
然后在定义一个级别时,在 init 函数的末尾执行(用您在每个级别中想要的位置替换争论):
self.player.set_pos(desired_player_x, desired_player_y)
self.skeleton.set_pos(desired_skel_x, desired_skel_y)
作为一些额外的建议,您可能需要考虑将 Skeleton 和 Player 的父类设置为update
, calc_grav
, 并且stop
看起来都是一样的,并且您可以在更新循环中调用 skeleton.go_right() 而不是 go_left() 反之亦然.
另外我认为您可以在此处添加 elif 语句:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and skeleton.change_x > 0:
skeleton.stop()
elif event.key == pygame.K_RIGHT and skeleton.change_x < 0:
skeleton.stop()
if event.key == pygame.K_LEFT and player.change_x < 0:
player.stop()
elif event.key == pygame.K_RIGHT and player.change_x > 0:
player.stop()
和这里:
if current_level_no == 0:
screen.blit(lvl_1, [0, 0])
elif current_level_no == 1:
screen.blit(lvl_2, [0, 0])
portal.rect.x = 570
portal.rect.y = 320