Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Pygame: screen.fill and screen.blit not working, please help!

User8574

Active Coder
Python:
# Modules

import pygame, sys

# Init / Clock

pygame.init()
clock = pygame.time.Clock()

# Display Setup

screen_width = 1920
screen_height = 1080
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Coin Collector")

# Objects

coin = pygame.image.load("coin.png").convert()

# Colors

bg_color = pygame.Color(255, 255, 255)

# Event Loop

while True:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

# Object Draw / Blit

    screen.fill(bg_color)

    screen.blit(coin, (screen_width/2, screen_height/2))

# Screen Update

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

Both filling the background and making the coin appear on the screen doesn't work; why?
 
Your one-liner problem description is not very clear... what exactly doesn't work ? The combination of both actions ? Does it work when you onbly do onw of them ? What are the symptoms ?
When I run your code (after having downloaded an image for coin.png) I just get an entirely black screen. Is that what you get ?
 
Your one-liner problem description is not very clear... what exactly doesn't work ? The combination of both actions ? Does it work when you onbly do onw of them ? What are the symptoms ?
When I run your code (after having downloaded an image for coin.png) I just get an entirely black screen. Is that what you get ?
Yes
 
Yes I noticed that, and wonder why the screen went black. Seems we're missing something. I may have another look tomorrow. Maybe someone else will chip in before that.
 
The problem is that you have a double event loop, and that the page update command is outside of the loop. Try this, it works much better :

Python:
running = True
while running:
    screen.fill(bg_color)
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = False
    screen.blit(coin, (screen_width/2, screen_height/2))
    pygame.display.flip()
    clock.tick(60)
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom