为什么赢得我的pygame图片加载

时间:2016-01-26 02:17:13

标签: python-2.7 pygame

我对python和pygame比较新,所以这肯定只是我自己的经验不足。但是,在我的游戏循环中,每当图像或形状 blitted 到屏幕时,它都不会消失,直到另一个图像或形状在其上闪烁。

game_loop函数

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)

我开始只是在pygame.display.update()函数中的任何地方调用game_loop函数来尝试修复它,但它似乎仍然没有帮助。

以下是行动中代码的图片: enter image description here

那些红线应该是正方形,汽车......不应该看起来像那样。如果有人能告诉我我做错了什么,那就太棒了。

2 个答案:

答案 0 :(得分:4)

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

 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

答案 1 :(得分:0)

有一种简单,便宜的方式将图像从屏幕上移开......  假设屏幕是400 x 400,只需将图片的坐标更改为x就像500,y就像600,这样它就会被放置在pygame画布之外