+ 2

Why does this code freeze? {Pros only} Introducing an entry box ---Part 2

PART 1 [SOLVED] Hey there everyone. I need your help. I have been working on a school project lately with Pygame, but have reached a point where I don't know what to do. Here is the code: [UPDATED --------- PART 2] Now I need to introduce the code of the the entry box on this code, but without causing any clashes. Any help? import pygame import random pygame.init() clock = pygame.time.Clock() surface = pygame.display.set_mode((600, 400)) pygame.display.set_caption("Projecte MatZanfe") font = pygame.font.SysFont('comicsans', 50) base_font = pygame.font.Font(None, 32) user_text = '' color_active = pygame.Color('lightskyblue3') running = True def start_the_game(): x = random.randint(0, 10) y = random.randint(0, 10) is_correct = False return x, y def display_the_game(x, y): # Variables points = 0 z = x + y surface.fill((255, 70, 90)) text = font.render(str(x) + "+" + str(y), True, (255, 255, 255)) input_rect = pygame.Rect(200, 200, 180, 50) pygame.draw.rect(surface, color_active, input_rect) text_surface = base_font.render(user_text, True, (255, 255, 255)) surface.blit(text_surface, input_rect) surface.blit(text, (260, 120)) input_rect.w = max(100, text_surface.get_width() + 10) x, y = start_the_game() while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False display_the_game(x, y) pygame.display.update() pygame.quit()

16th Aug 2021, 3:43 PM
Pol C
Pol C - avatar
31 Respostas
+ 4
UPDATED CODE 2 import pygame import random pygame.init() clock = pygame.time.Clock() surface = pygame.display.set_mode((600, 400)) pygame.display.set_caption("Projecte MatZanfe") font = pygame.font.SysFont('comicsans', 50) base_font = pygame.font.Font(None, 32) user_text = '' color_active = pygame.Color('lightskyblue3') def start_the_game(): # Variables is_correct = False points = 0 x = random.randint(0,10) y = random.randint(0,10) z = x + y surface.fill((255,70,90)) text = font.render (str(x) + "+" + str(y), True, (255,255,255)) input_rect = pygame.Rect(200,200,180,50) pygame.draw.rect(surface,color_active,input_rect) text_surface = base_font.render(user_text,True,(255,255,255)) surface.blit(text_surface, input_rect) surface.blit(text,(260,120)) input_rect.w = max(100,text_surface.get_width()+10) running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False start_the_game() pygame.display.update() pygame.quit() I just need to put the if statement in the loop, but don't know how to do it without causing problems. @FunOfCoding
17th Aug 2021, 4:19 PM
Pol C
Pol C - avatar
+ 6
I see you adding school project in your question everytime but i don't understand how does that relates to whatever your question is about ?
16th Aug 2021, 4:05 PM
Abhay
Abhay - avatar
+ 5
Pol CaƱadas Costa Unfortunately, SL doesn't support pygame.
16th Aug 2021, 3:46 PM
Calvin Thomas
Calvin Thomas - avatar
+ 5
Pol CaƱadas Costa This won't be possible in SoloLearn. You may try using IDEs like PyCharm and all.
16th Aug 2021, 3:49 PM
Calvin Thomas
Calvin Thomas - avatar
+ 4
I know, this Code is made on Pycharm. I only need someone to help me fix snd upgrade the code.
16th Aug 2021, 3:54 PM
Pol C
Pol C - avatar
+ 3
But could any expert try to help me? I really need some help.
16th Aug 2021, 3:48 PM
Pol C
Pol C - avatar
+ 2
This function is only a little part of a program that I am building. Every question that I make is somehow related to the others.
16th Aug 2021, 4:07 PM
Pol C
Pol C - avatar
+ 2
Can you describe what the issue currently is and what you want to achieve?
16th Aug 2021, 10:41 PM
Kevin
Kevin - avatar
+ 2
@FunOfCoding I have tried what you told me. I have edited the code, and now at least the program stops when it generates the 2 random number. @Kevin I have explained what I need at the updated version of the question. I have updated the code everyone.
17th Aug 2021, 9:04 AM
Pol C
Pol C - avatar
+ 2
I didnt'want to, but for some reason if I don't put another while loop the window would not even open.
17th Aug 2021, 9:11 AM
Pol C
Pol C - avatar
+ 2
FunOfCoding Where should I put the current Code on your solution?
17th Aug 2021, 9:18 AM
Pol C
Pol C - avatar
+ 2
FunOfCoding It says that the if event.type() == Pygame.QUIT Int is not reachable, and the window not even opens.
17th Aug 2021, 9:26 AM
Pol C
Pol C - avatar
+ 2
Now It does no Freeze, but It is only a black background. FunOfCoding
17th Aug 2021, 9:33 AM
Pol C
Pol C - avatar
+ 2
What does It need? FunOfCoding
17th Aug 2021, 9:45 AM
Pol C
Pol C - avatar
+ 2
Alright. Thanks.FunOfCoding
17th Aug 2021, 9:51 AM
Pol C
Pol C - avatar
+ 2
The only problem is that you use the 'while true' twice in the same place
18th Aug 2021, 12:16 PM
Chris Jonathan
Chris Jonathan - avatar
+ 2
Part 2: Entry Box Hello everyone. Thank you all for trying to help me with part 1. However, part 1 was just half of the problem. I need to put an entry box at the rectangle zone from the code. I have been looking at the internet, and I have modified an old code to display the box entry. I will now input the code.
18th Aug 2021, 3:08 PM
Pol C
Pol C - avatar
+ 2
ENTRY BOX (Part 1 of the code, because I exceeded the limit) import pygame pygame.init() surface = pygame.display.set_mode((600, 400)) COLOR_INACTIVE = pygame.Color('lightskyblue3') COLOR_ACTIVE = pygame.Color('dodgerblue2') FONT = pygame.font.SysFont('comicsans', 32) base_font = pygame.font.Font(None, 32) color_active = pygame.Color('lightskyblue3') user_text = '' class InputBox: def __init__(self, x, y, w, h, text=''): self.rect = pygame.Rect(x, y, w, h) self.color = COLOR_INACTIVE self.text = text self.txt_surface = FONT.render(text, True, self.color) self.active = False def handle_event(self, event): if event.type == pygame.MOUSEBUTTONDOWN: # If the user clicked on the input_box rect. if self.rect.collidepoint(event.pos): # Toggle the active variable. self.active = not self.active else: self.active = False # Change the current color of the input box. self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE if event.type == pygame.KEYDOWN: if self.active: if event.key == pygame.K_RETURN: print(self.text) self.text = '' elif event.key == pygame.K_BACKSPACE: self.text = self.text[:-1] else: self.text += event.unicode # Re-render the text. self.txt_surface = FONT.render(self.text, True, self.color) def update(self): # Resize the box if the text is too long. width = max(200, self.txt_surface.get_width()+10) self.rect.w = width def draw(self, screen): # Blit the text. screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5)) # Blit the rect. pygame.draw.rect(screen, self.color, self.rect, 2)
18th Aug 2021, 3:10 PM
Pol C
Pol C - avatar
+ 2
ENTRY BOX (Part 2 of the code) def main(): clock = pygame.time.Clock() input_box2 = InputBox(190, 250, 200, 32) input_boxes = [input_box2] done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True for box in input_boxes: box.handle_event(event) for box in input_boxes: box.update() surface.fill((255, 70, 90)) for box in input_boxes: box.draw(surface) pygame.display.flip() clock.tick(30) if __name__ == '__main__': main() pygame.quit()
18th Aug 2021, 3:10 PM
Pol C
Pol C - avatar
+ 1
I could be wrong, but I think the problem is that you didn't set a delay in the while loop, which could cause the code to crash. You also defined a clock variable, which can be used in the while loop to prevent the code from freezing. Here is an example - clock = pygame.time.Clock() while True: # 60 Frames Per Second (FPS) clock.tick(60) # And add this too (very important to update your screen so the most recent things added will be displayed on top of the old ones) pygame.display.update() ( Again, I could be wrong! )
17th Aug 2021, 5:58 AM
Baspberry