【问题标题】:Why won't my pygame images load为什么我的 pygame 图像无法加载
【发布时间】:2016-01-26 02:17:12
【问题描述】:

我对 python 和 pygame 比较陌生,所以这肯定只是我自己的经验不足。然而,在我的游戏循环中,每次图像或形状blitted 显示在屏幕上时,它不会消失,直到另一个图像或形状被快速覆盖。

游戏循环函数

def game_loop():

road
carImg

pygame.init()
pygame.mixer.init()
pygame.mixer.music.load('Rally-X.mp3')
pygame.mixer.music.play()

x = (display_width * 0.45)
y = (display_height * 0.8)

x_change = 0

road_starty = -23
road_speed = 4

thing_starty = -600
thing_speed = 4
thing_width = 100
thing_height = 100
thing_startx = random.randrange(0, display_width)

carImgCount = 1

dodged = 0

gameExit = False

pygame.display.update()

while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            pygame.display.update()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                x_change = -5
            if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                x_change = 5
            if event.key == pygame.K_p:
                paused()
            pygame.display.update()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0
            pygame.display.update()
    x += x_change

    gameDisplay.blit(road,(0,road_starty))

    car(x,y)
    #things(thingx, thingy, thingw, thingh, color)
    things(thing_startx, thing_starty, thing_width, thing_height)

    road_starty += road_speed

    thing_starty += thing_speed
    things_dodged(dodged)

    if x > display_width - car_width or x < 0:
        crash()

    if thing_starty > display_height:
        thing_starty = 0 - thing_height
        thing_startx = random.randrange(0,display_width)
        dodged += 1
        if thing_speed <= 10:
            thing_speed += 1
        pygame.display.update()

    if y < thing_starty+thing_height:
        #print('y crossover')

        if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
            #print('x crossover')
            crash()
        pygame.display.update()
    if road_starty == 0:
        road_starty = -23

    if dodged > 9:
        things(thing_startx, thing_starty, thing_width, thing_height)

    pygame.display.update()
    clock.tick(60)

我开始只是在 game_loop 函数的几乎所有位置调用 pygame.display.update() 函数来尝试修复它,但它似乎仍然没有帮助。

下面是运行代码的图片:

那些红线应该是正方形的,而汽车……不应该是那样的。如果有人能告诉我我做错了什么,那就太好了。

【问题讨论】:

  • 我不确定为什么图片没有显示。我试图上传它,但我想它不会让我上传。
  • 从图片上看,您似乎没有在滴答声之间清除屏幕。您正在使用我不太熟悉的pygame.display.update,但是您需要在某物移动后重新绘制背景,或者在每个滴答声中清除并重新绘制整个屏幕。后者更容易imo,我用Surface.fill((0, 0, 0))
  • 在循环中只使用一次pygame.display.update()

标签: python-2.7 pygame


【解决方案1】:

你必须在绘制新框架之前清除屏幕。

 gameDisplay.fill((0,0,0))

清屏后你必须重新绘制所有元素。

您必须将代码重组为filldrawupdate 一处

你可以这样组织mainloop(简单例子)

# --- mainloop ---

clock = pygame.time.Clock()
is_running = True

while is_running:

    # --- events ---

    for event in pygame.event.get():

        # --- global events ---

        if event.type == pygame.QUIT:
            is_running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                is_running = False

        # --- objects events (without draws)---

        '''
        player.handle_event(event)
        '''

    # --- updates (without draws)---

    '''
    player.update()
    '''

    # --- draws (without updates) ---

    screen.fill(BLACK) # only once in loop

    '''
    player.draw(screen)
    '''

    pygame.display.update() # only once in loop

    # --- FPS ---

    clock.tick(25)

完整示例:PyGame simple templateother templates

【讨论】:

  • 非常感谢。这有帮助。它现在有效。我现在可以继续我的工作了。
【解决方案2】:

有一种简单、廉价的方法可以让图像脱离屏幕... 假设屏幕是 400 x 400 只需将图片的坐标更改为 x 为 500 和 y 为 600 的位置,以便将其放置在 pygame 画布之外

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2016-02-19
    • 2021-11-11
    相关资源
    最近更新 更多