I am coding a Stacks game, but when I run it all it shows is a black screen

Viewed 28

I copied this code step by step from my teacher. The code ran smoothly for my teacher but when I tried running it, all it showed was the black background screen. I looked through other posts with similar problems but none of them fixed my problem. Does anyone know what the problem is?

import pygame
pygame.init()

S_WIDTH = 500
S_HEIGHT = 500
COLS = 10
ROWS = 20
B_HEIGHT = S_HEIGHT/ROWS
B_WIDTH = S_WIDTH/COLS

speed = 100
y = S_HEIGHT-B_HEIGHT 
left = 0
right = S_WIDTH
width = S_WIDTH
prev_left = left
prev_right = right
prev_width = width
color = (255, 0, 0)
direction = "l"

screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
pygame.display.set_caption("Stacks")

loop = True
while loop:
    pygame.time.delay(speed)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE]:
        if right <= prev_left or left >= prev_right:
                pygame.time.delay(1000)
                screen.fill((0,0,0))
                left = 0
                width = S_WIDTH
                right = S_WIDTH
                prev_left = left
                prev_right = right
                prev_width = width
                y = S_HEIGHT - B_HEIGHT
                direction = 'l'
        else:
            if left != prev_left: 
                left = max(left, prev_left)
                right = min(right, prev_right)
                width = right - left
            prev_left = left
            prev_right = right
            prev_width = width
            screen.fill((0,0,0),(0,y,S_WIDTH,B_HEIGHT))
            pygame.draw.rect(screen, color,(left, y, width, B_HEIGHT))
            y -= B_HEIGHT

    screen.fill((0,0,0), (0, y, S_WIDTH, B_HEIGHT))
    if left < 0-width+2*B_WIDTH:
        direction = 'r'
    elif left > S_WIDTH-2*B_WIDTH:
        direction = 'l'
    if direction == 'r':
        left+=B_WIDTH
        right+=B_WIDTH
    elif direction == 'l':
        left-=B_WIDTH
        right-=B_WIDTH
    pygame.draw.rect(screen, color, (left, y, width, B_HEIGHT))
    pygame.display.update

pygame.quit()
1 Answers

I tried out your code. I saw the above comment after I had troubleshooted the program. Along with that correction to "display.update()" I added in a couple of small tweaks that you may like to add to your test program. Following is a copy of the code with those tweaks.

import pygame
pygame.init()

S_WIDTH = 800
S_HEIGHT = 600
COLS = 10
ROWS = 20
B_HEIGHT = S_HEIGHT/ROWS
B_WIDTH = S_WIDTH/COLS

speed = 100
y = S_HEIGHT-B_HEIGHT
left = 0
right = S_WIDTH
width = S_WIDTH
prev_left = left
prev_right = right
prev_width = width
color = (255, 0, 0)
direction = "l"

screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
pygame.display.set_caption("Stacks")
sky_surface = pygame.image.load('Sky.png').convert()
sky_surface = pygame.transform.scale(sky_surface, (S_WIDTH, S_HEIGHT))    # Made it fit the screen size

loop = True
while loop:
    pygame.time.delay(speed)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE]:
        if right <= prev_left or left >= prev_right:
                pygame.time.delay(1000)
                screen.fill((0,0,0))
                left = 0
                width = S_WIDTH
                right = S_WIDTH
                prev_left = left
                prev_right = right
                prev_width = width
                y = S_HEIGHT - B_HEIGHT
                direction = 'l'
        else:
            if left != prev_left: 
                left = max(left, prev_left)
                right = min(right, prev_right)
                width = right - left
            prev_left = left
            prev_right = right
            prev_width = width
            screen.fill((0,0,0),(0,y,S_WIDTH,B_HEIGHT))
            pygame.draw.rect(screen, color,(left, y, width, B_HEIGHT))
            y -= B_HEIGHT

    #screen.fill((0,0,0), (0, y, S_WIDTH, B_HEIGHT))
    screen.blit(sky_surface, (0, 0))    # Adds in the sky blue background
    if left < 0-width+2*B_WIDTH:
        direction = 'r'
    elif left > S_WIDTH-2*B_WIDTH:
        direction = 'l'
    if direction == 'r':
        left+=B_WIDTH
        right+=B_WIDTH
    elif direction == 'l':
        left-=B_WIDTH
        right-=B_WIDTH
    pygame.draw.rect(screen, color, (left, y, width, B_HEIGHT))
    
    pygame.display.update() # Needed the parentheses

pygame.quit()

Instead of filling the screen with a monotone color, I placed a background image on the screen instead. That seemed to behave a bit better with your moving rectangle. Plus, this gives you some ideas for background/wallpaper for your game.

The result was that the rectangle was visible on the screen and moved back and forth.

Sample Screen

Anyway, that should give you some food for thought moving forward. Give that a try.

Related