Pygame 移动图像不出现在移动背景上
Pygame moving image not appearing on moving background
所以,我的 pygame 有一个动态背景,效果很好。现在我想添加一个障碍物(如岩石),它位于屏幕底部并随背景移动。然而,图像(障碍物)出现几秒钟后就消失了。我想让石头一遍又一遍地出现,然而,它没有出现。无法弄清楚出了什么问题。请帮助,谢谢!
background = pygame.image.load('background.png')
backgroundX = 0
backgroundX2 = background.get_width()
obstacle = pygame.image.load('obstacle.png')
obstacleX = 0
obstacleX2 = obstacle.get_width()
# use procedure for game window rather than using it within loop
def redrawGameWindow():
# background images for right to left moving screen
screen.blit(background, (backgroundX, 0))
screen.blit(background, (backgroundX2, 0))
man.draw(screen)
screen.blit(obstacle, (obstacleX, 380))
screen.blit(obstacle, (obstacleX2, 380))
pygame.display.flip()
pygame.display.update()
主循环:
while run:
screen.fill(white)
clock.tick(30)
pygame.display.update()
redrawGameWindow() # call procedure
obstacleX -= 1.4
obstacleX2 -= 1.4
if obstacleX < obstacle.get_width() * -10:
obstacleX = obstacle.get_width
if obstacleX2 < obstacle.get_width() * -10:
obstacleX2 = obstacle.get_width()
surface.blit()
(即:screen.blit)函数获取图像和绘制位置的左上角坐标。
在提供的代码中,在 obstacleX
和 obstacleX2
处绘制了两个副本 obstacle
- 其中一个设置为 0
,另一个设置为宽度的图像。因此,这应该导致两个图像彼此相邻绘制,位于 window 的左侧,第 380 行。
如果这些图像在一段时间后不再被绘制,这可能是由于 -
- 变量
obstacleX
和 obstacleX2
被更改为屏幕外的位置
- 图像
obstacle
被更改为空白(或不可见)版本
上面的小代码示例中没有证据,但由于问题表明图像移动,我猜测绘图位置的 obstacleX
和 obstacleX2
坐标正在移动改为离屏。
编辑:
很明显,您的对象从位置 0
(window 左侧)开始,并且位置正在更新 obstacleX -= 1.4
,这使障碍物进一步移动 左。这就是为什么它们开始出现在屏幕上,但很快就消失了。
将您的屏幕尺寸设为常量,例如:
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
并使用这些而不是用数字来填充您的代码。如果您决定更改 window 大小,这会减少所需的更改次数,并且还允许基于 window 宽度进行计算。
所以就在屏幕外开始你的障碍。
obstacleX = WINDOW_WIDTH # off-screen
obstacleX2 = WINDOW_WIDTH + 100 # Further away from first obstacle
在主更新循环中,当物品的位置改变时,检查它们是否需要重新循环回到玩家面前:
# Move the obstacles 1-pixel to the left
obstacleX -= 1.4
obstacleX2 -= 1.4 # probably just 1 would be better
# has the obstacle gone off-screen (to the left)
if ( obstacleX < 0 - obstacle.get_width() ):
# move it back to the right (off-screen)
obstacleX = WINDOW_WIDTH + random.randint( 10, 100 )
# TODO - handle obstacleX2 similarly
所以,我的 pygame 有一个动态背景,效果很好。现在我想添加一个障碍物(如岩石),它位于屏幕底部并随背景移动。然而,图像(障碍物)出现几秒钟后就消失了。我想让石头一遍又一遍地出现,然而,它没有出现。无法弄清楚出了什么问题。请帮助,谢谢!
background = pygame.image.load('background.png')
backgroundX = 0
backgroundX2 = background.get_width()
obstacle = pygame.image.load('obstacle.png')
obstacleX = 0
obstacleX2 = obstacle.get_width()
# use procedure for game window rather than using it within loop
def redrawGameWindow():
# background images for right to left moving screen
screen.blit(background, (backgroundX, 0))
screen.blit(background, (backgroundX2, 0))
man.draw(screen)
screen.blit(obstacle, (obstacleX, 380))
screen.blit(obstacle, (obstacleX2, 380))
pygame.display.flip()
pygame.display.update()
主循环:
while run:
screen.fill(white)
clock.tick(30)
pygame.display.update()
redrawGameWindow() # call procedure
obstacleX -= 1.4
obstacleX2 -= 1.4
if obstacleX < obstacle.get_width() * -10:
obstacleX = obstacle.get_width
if obstacleX2 < obstacle.get_width() * -10:
obstacleX2 = obstacle.get_width()
surface.blit()
(即:screen.blit)函数获取图像和绘制位置的左上角坐标。
在提供的代码中,在 obstacleX
和 obstacleX2
处绘制了两个副本 obstacle
- 其中一个设置为 0
,另一个设置为宽度的图像。因此,这应该导致两个图像彼此相邻绘制,位于 window 的左侧,第 380 行。
如果这些图像在一段时间后不再被绘制,这可能是由于 -
- 变量
obstacleX
和obstacleX2
被更改为屏幕外的位置 - 图像
obstacle
被更改为空白(或不可见)版本
上面的小代码示例中没有证据,但由于问题表明图像移动,我猜测绘图位置的 obstacleX
和 obstacleX2
坐标正在移动改为离屏。
编辑:
很明显,您的对象从位置 0
(window 左侧)开始,并且位置正在更新 obstacleX -= 1.4
,这使障碍物进一步移动 左。这就是为什么它们开始出现在屏幕上,但很快就消失了。
将您的屏幕尺寸设为常量,例如:
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
并使用这些而不是用数字来填充您的代码。如果您决定更改 window 大小,这会减少所需的更改次数,并且还允许基于 window 宽度进行计算。
所以就在屏幕外开始你的障碍。
obstacleX = WINDOW_WIDTH # off-screen
obstacleX2 = WINDOW_WIDTH + 100 # Further away from first obstacle
在主更新循环中,当物品的位置改变时,检查它们是否需要重新循环回到玩家面前:
# Move the obstacles 1-pixel to the left
obstacleX -= 1.4
obstacleX2 -= 1.4 # probably just 1 would be better
# has the obstacle gone off-screen (to the left)
if ( obstacleX < 0 - obstacle.get_width() ):
# move it back to the right (off-screen)
obstacleX = WINDOW_WIDTH + random.randint( 10, 100 )
# TODO - handle obstacleX2 similarly