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()
