我正在与Pygame一起练习,试图学习类,对象等的一些基本用法。
现在,在http://programarcadegames.com/的帮助下,我正在制作一个基本的平台
我有一个精灵播放器和精灵平台。这些平台也可以是移动平台,它应该在碰撞时移动玩家,但是当玩家移动到其他平台时,会发生一些奇怪的传送,我现在没有看到问题。
基本上,当被推时,玩家在逻辑上没有被移动(至少对我来说是这样)。
非常感谢任何帮助!玩家精灵和平台精灵的代码如下。它们应该包含任何形式的碰撞。也完整的代码,如果有人想运行它(只需要任何“背景。jpg”图像文件)
玩家精灵:
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.player_width = 25
self.player_height = 50
self.image = pygame.Surface([self.player_width,self.player_height])
self.image.fill([255,0,0])
self.rect = self.image.get_rect()
self.change_x = 0
self.change_y = 0
self.double_jump = 0
self.level = None
def update(self):
self.calc_gravity()
self.rect.x += self.change_x
collision_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for platform in collision_list:
if self.change_x > 0:
self.rect.right = platform.rect.left
elif self.change_x < 0:
self.rect.left = platform.rect.right
self.rect.y += self.change_y
collision_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for platform in collision_list:
if self.change_y > 0:
self.rect.bottom = platform.rect.top
self.double_jump = 0
elif self.change_y < 0:
self.rect.top = platform.rect.bottom
self.change_y = 0
def calc_gravity(self):
if self.change_y == 0:
self.change_y = 2
else:
self.change_y += 0.35
if self.rect.y >= screen_height - self.player_height - 32 and self.change_y >= 0:
self.change_y = 0
self.rect.y = screen_height - self.player_height - 32
平台和移动平台精灵:
class Platform(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill([173,255,47])
self.rect = self.image.get_rect()
class MovingPlatform(Platform):
change_x = 0
change_y = 0
boundary_top = 0
boundary_bottom = 0
boundary_left = 0
boundary_right = 0
def update(self):
self.rect.x += self.change_x
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_x < 0:
self.player.rect.right = self.rect.left
elif self.change_x > 0:
self.player.rect.left = self.rect.right
self.rect.y += self.change_y
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
if self.rect.bottom >= self.boundary_bottom or self.rect.top <= self.boundary_top:
self.change_y *= -1
cur_pos = self.rect.x - self.level.world_shift
if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
self.change_x *= -1
完整代码:
import pygame
screen_width = 800
screen_height = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.player_width = 25
self.player_height = 50
self.image = pygame.Surface([self.player_width,self.player_height])
self.image.fill([255,0,0])
self.rect = self.image.get_rect()
self.change_x = 0
self.change_y = 0
self.double_jump = 0
self.level = None
def update(self):
self.calc_gravity()
self.rect.x += self.change_x
collision_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for platform in collision_list:
if self.change_x > 0:
self.rect.right = platform.rect.left
elif self.change_x < 0:
self.rect.left = platform.rect.right
self.rect.y += self.change_y
collision_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
for platform in collision_list:
if self.change_y > 0:
self.rect.bottom = platform.rect.top
self.double_jump = 0
elif self.change_y < 0:
self.rect.top = platform.rect.bottom
self.change_y = 0
def calc_gravity(self):
if self.change_y == 0:
self.change_y = 2
else:
self.change_y += 0.35
if self.rect.y >= screen_height - self.player_height - 32 and self.change_y >= 0:
self.change_y = 0
self.rect.y = screen_height - self.player_height - 32
self.double_jump = 0
def jump(self):
self.rect.y += 2
collision_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
self.rect.y -= 2
if len(collision_list) > 0 or self.rect.bottom >= screen_height - 32:
self.change_y = -10
elif self.double_jump == 0:
self.change_y = -10
self.double_jump = 1
def go_left(self):
self.change_x = -5
def go_right(self):
self.change_x = 5
def stop(self):
self.change_x = 0
class Platform(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill([173,255,47])
self.rect = self.image.get_rect()
class MovingPlatform(Platform):
change_x = 0
change_y = 0
boundary_top = 0
boundary_bottom = 0
boundary_left = 0
boundary_right = 0
def update(self):
self.rect.x += self.change_x
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_x < 0:
self.player.rect.right = self.rect.left
elif self.change_x > 0:
self.player.rect.left = self.rect.right
self.rect.y += self.change_y
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
if self.rect.bottom >= self.boundary_bottom or self.rect.top <= self.boundary_top:
self.change_y *= -1
cur_pos = self.rect.x - self.level.world_shift
if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
self.change_x *= -1
class Level():
def __init__(self, player):
self.platform_list = pygame.sprite.Group()
self.player = player
self.world_shift = 0
def update(self):
self.platform_list.update()
def draw(self, screen, background):
screen.blit(background, [0,0])
self.platform_list.draw(screen)
def shift_world(self, shift_x):
self.world_shift += shift_x
for platform in self.platform_list:
platform.rect.x += shift_x
class Level_01(Level):
def __init__(self, player):
Level.__init__(self, player)
self.level_limit = -140
level = [[200, 25, 500, 500],
[100, 25, 100, 300]
]
for platform in level:
block = Platform(platform[0], platform[1])
block.rect.x = platform[2]
block.rect.y = platform[3]
block.player = self.player
self.platform_list.add(block)
block = MovingPlatform(150, 25)
block.rect.x = 200
block.rect.y = 300
block.boundary_top = 0
block.boundary_bottom = screen_height - 30
block.change_y = 1
block.player = self.player
block.level = self
self.platform_list.add(block)
block = MovingPlatform(150, 25)
block.rect.x = 100
block.rect.y = 350
block.boundary_left = 0
block.boundary_right = 600
block.change_x = 2
block.player = self.player
block.level = self
self.platform_list.add(block)
class Level_02(Level):
def __init__(self, player):
Level.__init__(self, player)
self.level_limit = -1000
level = [[200, 25, 500, 500],
[100, 25, 100, 100]
]
for platform in level:
block = Platform(platform[0], platform[1])
block.rect.x = platform[2]
block.rect.y = platform[3]
block.player = self.player
self.platform_list.add(block)
def main():
screen = pygame.display.set_mode([screen_width,screen_height])
clock = pygame.time.Clock()
done = False
background = pygame.image.load("background.jpg")
background = pygame.transform.scale(background, [800,600])
camera_left = 250
camera_right = 550
active_sprite_list = pygame.sprite.Group()
player = Player()
level_list =[]
level_list.append(Level_01(player))
level_list.append(Level_02(player))
current_level_no = 0
current_level = level_list[current_level_no]
player.level = current_level
player.rect.x = 100
player.rect.y = screen_height - player.player_height - 25
active_sprite_list.add(player)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#---Key presses---
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
player.go_left()
elif event.key == pygame.K_d:
player.go_right()
elif event.key == pygame.K_SPACE:
player.jump()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a and player.change_x < 0:
player.stop()
elif event.key == pygame.K_d and player.change_x > 0:
player.stop()
#---Move camera---
if player.rect.left <= camera_left:
diff = camera_left - player.rect.left
player.rect.left = camera_left
current_level.shift_world(diff)
if player.rect.right >= camera_right:
diff = player.rect.right - camera_right
player.rect.right = camera_right
current_level.shift_world(-diff)
#---Change level---
current_position = player.rect.x + current_level.world_shift
if current_position < current_level.level_limit:
player.rect.x = 120
if current_level_no < len(level_list)-1:
current_level_no += 1
current_level = level_list[current_level_no]
player.level = current_level
#---Update sprites---
current_level.update()
active_sprite_list.update()
#---Draw sprites---
current_level.draw(screen, background)
active_sprite_list.draw(screen)
clock.tick(60)
pygame.display.flip()
pygame.quit()
main()
该问题与< code > moving platform . update 有关:
class MovingPlatform(Platform):
# [...]
def update(self):
self.rect.x += self.change_x
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_x < 0:
self.player.rect.right = self.rect.left
elif self.change_x > 0:
self.player.rect.left = self.rect.right
# [...]
当玩家站在一个平台上上下移动并撞到另一个平台时,玩家会在该平台的两侧移动。
在问如何解决问题之前,必须问在这种特殊情况下应该发生什么。
一种可能的解决方案是简单地删除上面的代码:
或者,您可以添加一个附加条件,检查玩家是否在平台的左侧或右侧,但不在中间:
class MovingPlatform(Platform):
# [...]
def update(self):
self.rect.x += self.change_x
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
if self.change_x < 0 and self.player.rect.left < self.rect.left:
self.player.rect.right = self.rect.left
elif self.change_x > 0 and self.player.rect.right > self.rect.right:
self.player.rect.left = self.rect.right
# [...]
但是,如果玩家被2个平台挤压,这将无法解决问题。
我在PyGame中创建了两个简单的精灵,其中一个是雨伞,另一个是雨滴。雨滴被添加到一个名为< code>all_sprites的sprite组中。伞精灵有自己的组,名为< code>Umbrella_sprite 雨滴从屏幕顶部“落下”,如果其中一个碰到雨伞/与雨伞碰撞..雨滴应该被删除了。但是除了特定雨滴之外,所有其他雨滴都受此影响。
我一直在学习一些pygame教程,并且基于这些教程开发了自己的代码。具体如下: 所以我想出了如何让我的家伙跳跃,但是我已经在天空中放置了一个块并试图这样做: 试图让角色停止跳跃。有没有一个内置的函数来测试这一点,或者有没有一个适合我的方法。顺便说一下,这是行不通的,我似乎也不知道该怎么做。非常感谢任何帮助:)
问题内容: 在pygame中,有没有一种方法来寻找某一方之间的碰撞 一个精灵和另一个精灵的特殊一面?例如,如果 精灵A的顶部与精灵B的底部碰撞,返回True。 我肯定有办法做到这一点,但我找不到任何特别的方法 在文档中。 谢谢! 问题答案: 在PyGame中没有函数来获取侧面碰撞。 但你可以试着用 pygame.Rect.CollipePoint公司 测试是否,,, ,,,, 在里面 (pygam
在开始学习相关知识点之前,我们有必要先学习精灵和碰撞检测的含义。 精灵(英文译为 Sprite),其实在一个游戏程序中,精灵本质指的是一张张小尺寸的图片,比如游戏中的各种道具、人物、场景装饰等,它们都可以看做成一张张小的“精灵”图。除此之外,人物的移动也可以看做是一系列小精灵图构成的序列(按帧组成的序列),如下图所示: 图1:动作逐帧分解图 如果将逐帧分解后的动作,按照一定的频率播放,那么就形成了
我正在用python和pygame开发一个平台游戏。完整代码可以在“https://github . com/C-Kimber/FBLA _ Game”找到。我遇到的问题是玩家精灵和墙壁精灵之间的碰撞,特别是角落。当玩家按下x移动键并跳跃时,玩家要么不动,要么被卡住。以下是碰撞示例: 我尝试过其他方法,比如用速度来确定碰撞的因素,但这是迄今为止效果最好的方法。如果你能提供一个解决方案,我将不胜感激
这就是正在发生的事情,游戏不断停止,因为两个圆圈相互碰撞,我正在尝试通过确保ID是正方形ID来防止这种情况,我试图让圆圈碰撞在一起,而忽略碰撞,但它没有这样做,我不知道如何确切地阻止它相互碰撞,只允许它与正方形碰撞。 基本上我想阻止这个圆与另一个圆碰撞,并允许它与正方形碰撞。 任何帮助都会很好谢谢 游戏引擎引擎类 这是我在扩展game.engine.engine包的game类中检查冲突的地方。 游