1

I was making something in python that I am not smart enough to make, and I accidentally created an infinite loop, and I can't end it because I am using pygame, so it made a new window and I can't close it. I tried Ctrl+c and closing the lid of my laptop, is there any way other than restarting, because that will be annoying to do every time.

(No title)
import pygame

# initialize variables
width = 1366
height = 704
display_surface = pygame.display.set_mode((width, height)) 
screen = pygame.display.set_mode((width, height))  
# this is the block type list
items = [
# building blocks
["grass", "dirt", "stone", "ore", "chest", "item collector", "block placer", "item dropper"],
# technical blocks
["wires", "sensor", "AND gate", "OR gate", "NOT gate", "NOR gate", "XOR gate", "XNOR gate", "NAND gate", "gearbox", "gear - 24 tooth", "gear - 8 tooth", "item pipe", "filter pipe", "delete pipe", "motor", "joint", "bearing", "blueprints", "spring"],
]

# initiallize pygame sttuff
pygame.init() 

# begining of program
import pygame

def init_screen_and_clock():
    global screen, display, clock
    pygame.init()
    pygame.display.set_caption('Game')
    clock = pygame.time.Clock()


def create_fonts(font_sizes_list):
    "Creates different fonts with one list"
    fonts = []
    for size in font_sizes_list:
        fonts.append(
            pygame.font.SysFont("Arial", size))
    return fonts


def render(fnt, what, color, where):
    "Renders the fonts as passed from display_fps"
    text_to_show = fnt.render(what, 0, pygame.Color(color))
    screen.blit(text_to_show, where)


def display_fps():
    "Data that will be rendered and blitted in _display"
    render(
        fonts[0],
        what=str(int(clock.get_fps())),
        color="white",
        where=(0, 0))


init_screen_and_clock()
# This create different font size in one line
fonts = create_fonts([32, 16, 14, 8])

loop = 1
while True:  
    screen.fill((0, 0, 0))
    display_fps()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
    clock.tick(60)
    pygame.display.flip()

pygame.quit()
print("Game over")
3
  • 1
    You can just add a break clause in your loop. Commented Oct 11, 2022 at 22:17
  • @PApostol I could, except it is running at the moment and the X button won't make it close Commented Oct 11, 2022 at 22:20
  • 2
    On Windows ctrl+alt+delete will get you to the task manager from which you can kill the process Commented Oct 11, 2022 at 22:21

1 Answer 1

2

The issue is that you're setting loop to exit/not, but the code is not testing loop, it's just testing True... which unsurprisingly always evaluates to True.

loop = 1
while True:                         # <<-- HERE
    screen.fill((0, 0, 0))
    display_fps()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
    clock.tick(60)
    pygame.display.flip()

If you simply change this to test loop it will work ok:

loop = 1
while ( loop == 1 ):                         # <<-- HERE
    screen.fill((0, 0, 0))
    display_fps()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
    clock.tick(60)
    pygame.display.flip()

I laboured the syntax here to show exactly what's going on. You could also get away with while loop:. But less code isn't always better.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.