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.