in Education by
When I try to run this pygame code it instantly closes?? The window doesn't close instantly when I stop drawing text, so I know I must've done something wrong. import pygame background_colour = (255, 255, 255) (width, height) = (1920, 1080) screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('fat') screen.fill(background_colour) font = pygame.font.Font(None, 32) color = pygame.Color('dodgerblue2') pygame.display.flip() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False text = "" while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: vanishingtext += event.unicode text += event.unicode elif event.type == pygame.K_BACKSPACE: text = text[:-1] elif event.type == pygame.K_RETURN: interpret(text) text = "" else: pass txt_surface = font.render(text, True, color) screen.blit(txt_surface, (50, 100)) i expect for a screen to appear that allows me to type and backspace, if i press enter the text should completely disappear and a function should run that will interpret the string. i haven't defined interpet as a function yet, but i'm doing it after i've figured out if i can even make it work on a screen. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The code is not calling pygame.init(). Also there's two event loops, the second of which will just drop straight-through once the running becomes False. import pygame pygame.init() (width, height) = ( 400, 200) screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('fat') background_colour = pygame.Color('white') color = pygame.Color('dodgerblue2') font = pygame.font.Font(None, 32) clock = pygame.time.Clock() running = True text = '' while running: # handle events and user-input for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if ( event.key >= pygame.K_SPACE and event.key <= pygame.K_z ): # Append key-stroke's character text += event.unicode elif ( event.key == pygame.K_BACKSPACE ): text = text[:-1] elif ( event.key == pygame.K_RETURN ): print("interpret(text) - NOT IMPLEMENTED") text = "" # repaint the screen screen.fill(background_colour) txt_surface = font.render(text, True, color) screen.blit(txt_surface, (50, 100)) pygame.display.flip() clock.tick_busy_loop(60) # limit FPS This code gives me a window titled "fat" with a white background. Typing on an english keyboard gives me blue letters, which can be backspaced. Pressing Enter is somewhat handled.

Related questions

0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have multiple xlsx files with data in it that i want to import to separate dataframes in Python. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to get the values of col1 in 3 different columns with separate headers. Date/Time col1 0 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I'm creating my first program on python. The objective is to get an output of trip cost. In the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am looking to write a pop-up window which asks the user to select a specific option, and if ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have a dataset, say: DateJoined Name Number DateLeft 11/03/2015 Tom 001199 11/03/2019 11/03/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I need to find a message the bot has previously posted, then see the reactions on it. I don't ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Is there a way to to disable intellisense in ax2012's code editor? sometimes when writing selecting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I am using the Google Sheets V4 Values collection and I am having trouble figuring out how to get each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I was developing one web application in cakephp 2.2.3. that application I was using CakeEmail. But Now ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want to log the user session. Currently the code is as follows (setting formatter and handlers is omitted ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
...