-1

Here is the code with the background in the main code. I cannot figure out what is wrong.

    import pygame
    import random
    pygame.init()

    width, height = 800, 400
    win = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Dungeon Crawler")

    bg_img = pygame.image.load('background_image.png')
    bg_img = pygame.transform.scale(bg_img, (width, height))

    player_x = 100
    player_y = 100
    player_width = 50
    player_height = 50
    player_speed = 5


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

    while run:
        clock.tick(60)  # 60 FPS
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        win.blit(bg_img, (0, 0))
        pygame.display.update()
    
         keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_speed
        if keys[pygame.K_RIGHT] and player_x < width - player_width:
            player_x += player_speed
        if keys[pygame.K_UP] and player_y > 0:
            player_y -= player_speed
        if keys[pygame.K_DOWN] and player_y < height - player_height:
            player_y += player_speed



        pygame.draw.rect(win, (0, 255, 0), (player_x, player_y, player_width, player_height))

        pygame.display.update()

    pygame.quit()

The cubes flicker in and out with the background, whereas when you move the win.blit(bg_img, (0, 0)) and pygame.display.update() out of the main code, (just under num_enemies = 6), the cube becomes like a long, stretched green snake... Please can somebody tell me what is wrong with the code? I am a noob still, I have tried chatGPT but it does not supply the level of knowledge I can get from other coders. Please help, this is my first time using pygame (within the last couple of weeks) and I really need some help. If you have more information about pygame that I could use to make a game, please tell me. Anyway, thanks in advance. (Yes you can copy and paste into idle or such, it is not too far into development, and thanks)

I tried doing a number of things, such as moving it around to other locations in the code, figuring out why but it did not work. Help please.

Sven
  • 7
  • 5

1 Answers1

0

That's because you are calling pygame.display.update() more than once in your game loop. You should only call this function after you're done drawing on the window. Here is the fixed version:

import pygame
import random
pygame.init()

width, height = 800, 400
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Dungeon Crawler")

bg_img = pygame.image.load('background_image.png')
bg_img = pygame.transform.scale(bg_img, (width, height))

player_x = 100
player_y = 100
player_width = 50
player_height = 50
player_speed = 5


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

while run:
    clock.tick(60)  # 60 FPS
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.blit(bg_img, (0, 0))
    # Removed pygame.display.update() here

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < width - player_width:
        player_x += player_speed
    if keys[pygame.K_UP] and player_y > 0:
        player_y -= player_speed
    if keys[pygame.K_DOWN] and player_y < height - player_height:
        player_y += player_speed



    pygame.draw.rect(win, (0, 255, 0), (player_x, player_y, player_width, player_height))

    pygame.display.update()

pygame.quit()
Zenthm
  • 104
  • 1
  • 8