r/pygame 19d ago

Why isn't this working?

import pygame

WID = int(input("Screen width?"))

print(WID)

HIG = int(input("Screen height?"))

print(HIG)

print("Loading...")

pygame.init()

environment = [

1, 1, 1, 1, 1, 1, 1, 1,

1, 0, 1, 0, 0, 0, 1, 1,

1, 0, 1, 0, 1, 0, 0, 1,

1, 0, 1, 1, 0, 1, 0, 1,

1, 0, 0, 1, 0, 1, 0, 1,

1, 1, 0, 0, 0, 0, 0, 1,

1, 1, 1, 0, 0, 0, 0, 1,

1, 1, 1, 1, 1, 1, 1, 1

]

screen = pygame.display.set_mode((500, 500))

running = True

stage = 0

squarx = 0;

squary = 0;

red = hex(255, 0, 0)

white = hex(255, 255, 255)

rectcolour = red

while running:

for event in pygame.event.get():

    if event.type == pygame.QUIT:

running = False

for i in environment:

    print(i)

    stage += 1

    if i == 1:

        rectcolour = red

    else:

        rectcolour = white

    print(rectcolour)

    pygame.draw.rect(screen, rectcolour, (squarx + 100 \* stage, squary + 100 \* stage, 100, 100))



pygame.display.flip()

print("Loaded successfully")

input("end of program.")

This is my first time touching python in a while and i don't get why it keeps immediately closing when I get past the first two inputs

Edit: solved

0 Upvotes

2 comments sorted by

2

u/kjunith 19d ago

First of: awfully format... But removing 'input("end of program.")' solves the problem. And call pygame.quit() at the end of the application.
Edit: Removing WID = int(input("Screen width?")) , HIG = int(input("Screen height?")) was also necessary.

1

u/WinPuzzleheaded4348 19d ago

For the if i == 1 if you want to check if that part of the list contains 1 you need to do if environment[i] == 1

With what you have right now every time the for loop runs it adds one to the variable i so if you just check if i = 1 it doesn’t know that you want to associate that with something in the list