我试图创建更多平台,我想知道最新的方法,无论是创建for循环还是向代码中添加更多东西,比如更改Platform括号中的数字,也就是当这些代码在平台上播放时不分开我怎么去分开它们?这样你就可以单独和随机地看到这些平台,如https://forums.crackberry.com/blackberry-10-games-f275/fall-down-game-906376/
import pygame
pygame.init()
clock = pygame.time.Clock()
FPS = 60
DS = pygame.display.set_mode((800, 600))
sprite_image = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.circle(sprite_image, (90, 100, 200), [25, 25], 25)
W, H = DS.get_size()
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
super(Platform, self).__init__()
self.image = pygame.Surface((w, h))
self.image.fill((90, 90, 120))
self.rect = self.image.get_rect(topleft=(x, y))
class Sprite(pygame.sprite.Sprite):
def __init__(self, pos):
super(Sprite, self).__init__()
self.image = sprite_image
self.rect = self.image.get_rect()
self.rect.center = pos
self._vx = 0
self._vy = 0
self._spritex = pos[0]
self._spritey = pos[1]
self.level = None
self._gravity = .9
def update(self):
# Adjust the x-position.
self._spritex += self._vx
self.rect.centerx = self._spritex # And update the rect.
block_hit_list = pygame.sprite.spritecollide(self, self.level, False)
for block in block_hit_list:
if self._vx > 0:
self.rect.right = block.rect.left
elif self._vx < 0:
# Otherwise if we are moving left, do the opposite.
self.rect.left = block.rect.right
self._spritex = self.rect.centerx # Update the position.
# Adjust the y-position.
self._vy += self._gravity # Accelerate downwards.
self._spritey += self._vy
self.rect.centery = self._spritey # And update the rect.
# Check and see if we hit anything
block_hit_list = pygame.sprite.spritecollide(self, self.level, False)
for block in block_hit_list:
# Reset our position based on the top/bottom of the object.
if self._vy > 0:
self.rect.bottom = block.rect.top
elif self._vy < 0:
self.rect.top = block.rect.bottom
self._spritey = self.rect.centery # Update the position.
# Stop our vertical movement
self._vy = 0
def jump(self):
self.rect.y += 2
platform_hit_list = pygame.sprite.spritecollide(self, self.level, 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 >= H:
self._vy = -17
def go_left(self):
""" Called when the user hits the left arrow. """
self._vx = -3
def go_right(self):
""" Called when the user hits the right arrow. """
self._vx = 3
def stop(self):
""" Called when the user lets off the keyboard. """
self._vx = 0
sprite = Sprite([60, 60])
active_sprite_list = pygame.sprite.Group(sprite)
sprite.level = pygame.sprite.Group(
Platform(20, 400, 200, 20), Platform(220, 300, 20, 120),
Platform(220, 300, 300, 20), Platform(220, 300, 300, 20),
Platform(220, 300, 300, 20), Platform(220, 300, 300, 20))
active_sprite_list.add(sprite.level)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
sprite.go_left()
if event.key == pygame.K_RIGHT:
sprite.go_right()
if event.key == pygame.K_UP:
sprite.jump()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and sprite._vx < 0:
sprite.stop()
if event.key == pygame.K_RIGHT and sprite._vx > 0:
sprite.stop()
# If the player gets near the right side, shift the world left (-x)
if sprite.rect.right > W:
sprite.rect.right = W
# If the player gets near the left side, shift the world right (+x)
if sprite.rect.left < 0:
sprite.rect.left = 0
active_sprite_list.update()
DS.fill((50, 50, 50))
# Blit the sprite's image at the sprite's rect.topleft position.
active_sprite_list.draw(DS)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
答案 0 :(得分:1)
如果要将平台放置在其他位置,则必须更改平台的坐标。前两个参数是x
和y
(左上角)坐标,第三个和第四个是平台的宽度和高度:Platform(x, y, width, height)
。
如果要使用for循环创建平台,可以创建rects(或rect样式元组)列表,解压缩值,使用它们创建新平台实例并将实例添加到sprite组。
active_sprite_list = pygame.sprite.Group(sprite)
sprite.level = pygame.sprite.Group()
# A list with rect style tuples (x, y, width, height).
rects = [
(20, 450, 200, 20), (220, 300, 20, 120), (300, 240, 300, 20),
(20, 380, 200, 20), (220, 300, 150, 20), (520, 200, 300, 20),
]
# You can unpack the rect tuples directly in the head of the for loop.
for x, y, width, height in rects:
# Create a new platform instance and pass the coords, width and height.
platform = Platform(x, y, width, height)
# Add the new platform instance to the sprite groups.
sprite.level.add(platform)
active_sprite_list.add(platform)
要传递随机位置,只需致电random.randrange
,而不是传递x
和y
变量。
platform = Platform(random.randrange(800), random.randrange(600), width, height)