- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一款类似于《以撒的结合》的游戏。我想在屏幕周围放一些石头来阻挡玩家的移动。
制作岩石布局的地方:
" ",
" ",
" ",
" ",
" jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
]
rockxcoord = 0
rockycoord = 0
for row in level:
for col in row:
if col == "j":
rock = rocks(rockxcoord, rockycoord)
rockGroup.add(rock)
rockxcoord +=32
rockycoord += 50
rockxcoord = 0
边界在哪里设置
for rock in rockGroup:
screen.blit(rock.image, [rock.rect.x, rock.rect.y])
rockCollisionList = pygame.sprite.spritecollide(playerOne, rockGroup, False)
for rock in rockCollisionList:
if playerOne.rect.x < rock.rect.x:
playerOne.rect.x = rock.rect.x - 90
if playerOne.rect.x > rock.rect.x:
playerOne.rect.x = rock.rect.x + 80
我已经成功地在 x 轴上添加了边界。但是,当对 y 轴执行相同操作时,它无法正常工作。
最佳答案
使用PyGame's Sprites .有一个 good tutorial在他们身上。最初需要做更多的工作,但以后会节省时间。真的,花时间学习它。这是值得的投资。
你的墙可以很简单:
class WallSprite( pygame.sprite.Sprite ):
""" A stationay sprite"""
def __init__( self, position, image ):
pygame.sprite.Sprite.__init__( self )
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = position
def udpate( self ):
# does not move
pass
创建一堆墙。显然,将 10 x 1 单位的墙作为单个 Sprite 制作更有效,但为了举例,我们将制作乐高™ 风格的墙。
# Create 20 randomly-placed walls
wall_image = pygame.image.load("brick_32.png").convert_alpha()
wall_sprites = pygame.sprite.Group() # a group for all the wall sprites
for i in range(20):
# create a wall at a random position
new_wall = WallSprite( ( random.randrange( 0, WINDOW_WIDTH ), random.randrange( 0, WINDOW_HEIGHT ) ), wall_image )
wall_sprites.add( new_wall ) # put into the sprite group
在您的主循环中, Sprite 组可用于绘制 Sprite 和组碰撞函数以查看您的玩家是否撞到了任何墙。
# Main loop
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
# handle direction keys ...
# move / update the player sprite
player_sprite.update()
# handle player <-> wall sprite collisions (for *ALL* walls)
if ( len( pygame.sprite.spritecollide( player_sprite, wall_sprites, False ) ) > 0 ):
player_sprite.stop_moving()
# re-paint the window
screen.fill( GREEN )
wall_sprites.draw() # paints the entire sprite group
player_sprite.draw() # paint the player
pygame.display.flip()
pygame.quit()
关于python - 如何为墙壁添加边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58613724/
我目前在使用边界框使 sprite 显示为实体时遇到问题。碰撞与下面的代码一起工作得很好。 位置是主要角色在 map 上的位置。character2 是主角将与之发生碰撞的 18 x 28 Sprit
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我正在制作一个 roguelike 游戏,但在编码方面我是初学者。我已经让我的角色移动了,我的墙壁和地板 Sprite ,但我的代码中有一些错误,允许角色穿过墙壁。 我用了 block_path在地板
我使用的代码只是取自一个示例,它确实为我的场景建了一堵墙: /** This loop builds a wall out of individual bricks. */ public vo
我已经尝试解决平滑播放器-墙壁-碰撞的问题,使播放器沿着墙壁滑动。 我试过以下: playerBox->move(); if (playerBox->intersects(wall)) { c
我是一名优秀的程序员,十分优秀!