2

Whenever I try to include "screen.fill(insert value here)" to my pygame code, the window instantly crashes. I can comment it out and my code works just fine. I'm really not sure why.

I've tried moving the line around, using other people's code in part - no luck.

import pygame
pygame.init()

win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")

img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")
win.blit(img,(450, 150))

running = True
while running:
    screen.fill(0, 0, 0)
    pygame.display.update()
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

pygame.quit()

When I run the code with that line in it, the window opens and then it instantly closes.

luis.parravicini
  • 1,214
  • 11
  • 19
Charlotte
  • 21
  • 1
  • 1
    No errors in the console? BTW, your loop will never end because you ask for `running` in the while loop and you change `run`. I wouldn't call that "works fine". – Matthias Oct 12 '19 at 20:35
  • No, none. At this point, this code is simply to display an image in pygame, and it does just that. It's the very beginning of my program, not a working game. – Charlotte Oct 12 '19 at 20:50
  • you should run it in console/terminal/cmd.exe - like `python script.py` - to see errors. – furas Oct 12 '19 at 20:51
  • Did you initialize `screen`? – csabinho Oct 12 '19 at 22:56

2 Answers2

1

If you are trying to fill the screen with black, the color must be passed as a tuple:

win.fill((0, 0, 0))

Also, you never assign screen, maybe you intended to use win?

The doc for Surface.fill.

luis.parravicini
  • 1,214
  • 11
  • 19
  • I've tried it that way too, with the double brackets, no luck. I'm not trying to turn it black, that's just a placeholder value from trying numerous values - I'm trying to turn it white. – Charlotte Oct 12 '19 at 20:45
  • Well, you never assign `screen`, try with `win`. (updated answer) – luis.parravicini Oct 12 '19 at 20:49
  • That's a start. It runs, but I can't see any images I put in anymore. – Charlotte Oct 12 '19 at 20:56
  • Are you using macOS and pygame 1.9? You might be having this issue: https://stackoverflow.com/questions/56319838/pygame-does-not-fill-surface-or-blit-image-on-display-update . And here some workarounds: https://stackoverflow.com/questions/52718921/problems-getting-pygame-to-show-anything-but-a-blank-screen-on-macos-mojave – luis.parravicini Oct 12 '19 at 21:01
  • @Charlotte You're not blitting the images in your example; you're only loading them into memory – Ted Klein Bergman Oct 12 '19 at 23:55
1

The color parameter to fill() has to be either a single grayscale value or a tuple of 3 RGB or 4 RGBA components. So it has to be either:

win.fill(0)

or

win.fill((0, 0, 0))

Further you've to blit() the image in the main loop. To continuously draw the scene in the main application loop, you've to:

  • handle the events
  • clear the window
  • draw the scene (blit the image)
  • update the display

Furthermore I recommend to use tick() of pygame.time.Clock rather than pygame.time.delay() tpo control the frames per second.

import pygame

pygame.init()

win = pygame.display.set_mode((1000, 800))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()

img=pygame.image.load("C:\\Users\\Charlotte\\Desktop\\tetris_grid.png")

running = True
while running:

    clock.tick(60)

    # handle the events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # clear the display
    win.fill(0)

    # draw the scene
    win.blit(img,(450, 150))

    # update the display
    pygame.display.update()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174