gpt4 book ai didi

python - 在 pygame 中创建单独的随机平台

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:17 25 4
gpt4 key购买 nike

我正在尝试创建更多平台,我想知道执行此操作的正确方法是什么,是创建 for 循环还是向代码添加更多内容,例如更改平台括号中的数字,以及在平台上播放此代码时不分开我该如何分开它们?这样您就可以在随机位置单独看到平台,如下所示 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()

最佳答案

如果您想将平台放置在其他地方,则必须更改平台的坐标。前两个参数是 xy (左上)坐标,第三个和第四个是平台的宽度和高度:Platform(x, y, width, height) .

如果您想使用 for 循环来创建平台,您可以创建一个矩形列表(或矩形样式元组),解压缩值,使用它们创建新的平台实例并将实例添加到 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 而不是传递 xy变量。

platform = Platform(random.randrange(800), random.randrange(600), width, height)

关于python - 在 pygame 中创建单独的随机平台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com