No working code?

Question:
Code dont work any idea why?
Repl link:
https://replit.com/@bendiereal/My-first-project#display.py

import pygame
import sys
import importlib.util
import threading
from io import StringIO
import sys as stdsys

# Initialize Pygame
pygame.init()

# Set up the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("User Input and Console Output")

# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up fonts
font = pygame.font.SysFont(None, 36)

# Function to run your main file
def run_main_file():
    main_spec = importlib.util.spec_from_file_location("main", "main.py")
    main_module = importlib.util.module_from_spec(main_spec)
    main_spec.loader.exec_module(main_module)

    # Redirect the standard output to capture console output
    old_stdout = stdsys.stdout
    stdsys.stdout = mystdout = StringIO()

    try:
        main_module.main()
    except Exception as e:
        print(e)

    # Restore the standard output
    stdsys.stdout = old_stdout

    return mystdout.getvalue()

# Create a thread to run the main file
main_thread = threading.Thread(target=run_main_file)
main_thread.start()

# Main loop
running = True
user_input = ""
console_output = "Welcome to Pygame!"

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                console_output = user_input
                user_input = ""
            elif event.key == pygame.K_BACKSPACE:
                user_input = user_input[:-1]
            else:
                user_input += event.unicode

    # Clear the screen
    screen.fill(WHITE)

    # Get console output from the main file
    console_output = run_main_file()

    # Render text
    input_text = font.render("Input: " + user_input, True, BLACK)
    output_text = font.render("Output: " + console_output, True, BLACK)

    # Draw text on the screen
    screen.blit(input_text, (50, 50))
    screen.blit(output_text, (50, 100))

    # Update the display
    pygame.display.flip()

# Quit Pygame
pygame.quit()
sys.exit

Please provide error output/description and perhaps some screenshots so we understand what exactly needs debugging or improvement.

ALso,

this block of code makes no sense, and you should probably delete it.

it was for running my main file

That is the output tab, which is capable of showing graphical/pixel output, not the console tab, which show the various logs from your python program.

ok well i just give up

2 Likes

Please don’t give up! Failure is a major part of programming, for without it we’ll never improve.

4 Likes

i dont give up on coding i give up on displaying it

Please just clarify this one thing for me: What type of content are ou trying to display?

the console. I dont want to share replit due to some “issues” so I was trying to make a dev link in HTML so they can play it without editing the code to cheat.

Replit does not provide such functionality unfotunately, but you might want to look into using pyscript

whats that? Is it usable on replit?

You can’t share the native console (not usable), but you can try to use pyscript to make a website which runs python.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.