0

So I'm working on a project for my computer science class and I have very little knowledge of pygame. The tutorial I used told me to use the .blit function on every frame so that when a character moves it adds the background over the character and then re-adds the character. But currently, my game uses a tile-based background to mimic older styles of games, and having to add like 50 tiles every frame slows it down a lot. Is there an easier way to do this?

my code:

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            #player input
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.quit()
                sys.exit()
            if event.key == pygame.K_j:
                actions.open_menu()
            if event.key == pygame.K_LSHIFT:
                sprinting = True
            else:
                sprinting = False
    #movement
    keys = pygame.key.get_pressed()
    if sprinting:
        if pygame.time.get_ticks() - time_move > 100:
            if keys[pygame.K_a]:
                character_rect.x -= 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_d]:
                character_rect.x += 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_w]:
                character_rect.y -= 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_s]:
                character_rect.y += 50
                time_move = pygame.time.get_ticks()
    else:
        if pygame.time.get_ticks() - time_move > 300:
            if keys[pygame.K_a]:
                character_rect.x -= 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_d]:
                character_rect.x += 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_w]:
                character_rect.y -= 50
                time_move = pygame.time.get_ticks()
            if keys[pygame.K_s]:
                character_rect.y += 50
                time_move = pygame.time.get_ticks()

    #collision
    if character_rect.x <= 0:
        character_rect.x = 0
    if character_rect.right >= 800:
        character_rect.right = 800
    if character_rect.bottom >= 800:
        character_rect.bottom = 800
    if character_rect.top <= 0:
        character_rect.top = 0


    ##update
    screen.fill((250,250,250))
    #gets the tiles for the background
    tiles = draw_screen.draw_tiles()
    #add the tiles to the screen
    for tile in tiles:
        screen.blit(tile[0], tile[1])
    #draws the character
    screen.blit(character_surf, character_rect)
    #misc updates
    time = pygame.time.get_ticks()
    pygame.display.update()
    clock.tick(60)

I don't use pygame that much and have just started to learn it. Please let me know if I'm missing anything.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • 2
    Please [edit] your question to include a [mre]. It is unlikely that your `draw_tiles()` function is a significant bottleneck with only fifty tiles unless you're doing something suboptimal like loading them from file every frame. – import random Apr 18 '23 at 04:00

1 Answers1

-1

I don't think you need to update all the tiles every frame, just the ones the character moved off of and onto right? In that case instead of blitting every tile, keep track of just what needs to be updated.

RedKnite
  • 1,525
  • 13
  • 26