#๐Ÿ”’ Why does pygame keep crashing?

61 messages ยท Page 1 of 1 (latest)

solar garnetBOT
#

@proper scaffold

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

proper scaffold
#

I'm trying to make an AI using PPO (this is the programs goal but is otherwise unrelated) the pygame window is black and when I click on it is says that it is not responding

proper scaffold
#

It still says not responding

#

I'm going to try running it for longer though

#

Thank you so much for helping

rapid niche
#

you could try to control + C it when it starts not responding to see where the code is

#

repeat a couple of times and if it's in the same section you know you got somewhere the program is getting stuck

sonic ore
#

Maybe add some prints, too, might help narrow down where it's hanging

proper scaffold
#

wait so is that shortcut ctr and C or ctr and + and C

#

also it only says "not responding" when I click on it

#

just added some prints and found that
"def train_model():
print("Starting training...")
total_timesteps = 2000000 # Set total timesteps
model.learn(total_timesteps=total_timesteps) # Train model
print("Training finished.")

def visualize():
obs = env.reset() # Reset environment
done = False # Initialize done flag
clock = pygame.time.Clock() # Create clock object

while not done:
    # Process events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    action, _states = model.predict(obs)  # Predict action with model
    obs, rewards, done, info = env.step(action)  # Take a step in environment
    env.render()  # Render environment
    clock.tick(60)  # Limit frame rate

env.close()  # Close environment
print("Visualized done")

if name == 'main':
train_model() # Train the model
visualize() # Visualize the trained model
print("code.finished")"

otherwise known as the end part is not running, how do I fix this and why is it not running

rapid niche
#

since it raises an exception you'll see where you're code at

proper scaffold
proper scaffold
#

what does that mean?

rapid niche
#

you don't only got the window, you also got the terminal

#

mainly, the terminal you're running your program from

#

or the one that opens when you double click (if you're clicking)

proper scaffold
#

Update:
I've been fooling around with the print statements and after placing them in the correct location I found that my code gets hung up when I try to render it.

#

this is my render code:

   self.screen.fill(self.white)  # Fill screen with white color
    x_end, y_end = self._calculate_joint_end()  # Calculate joint end position
    pygame.draw.line(self.screen, self.black, (self.x_start, self.y_start), (x_end, y_end), 5)  # Draw joint line
    x_end2, y_end2 = self._calculate_joint2_end()  # Calculate joint2 end position
    pygame.draw.line(self.screen, self.red, (self.x_start2, self.y_start2), (x_end2, y_end2), 5)  # Draw joint2 line
    self.screen.blit(self.Target, self.Target_rect)  # Draw target sprite

    pygame.draw.circle(self.screen, self.red, (x_end, y_end), 5)  # Draw circle at joint end
    pygame.draw.circle(self.screen, self.red, (x_end2, y_end2), 5)  # Draw circle at joint2 end

    score_text = self.font.render(f"Score: {self.score}", True, self.black)  # Render score text
    self.screen.blit(score_text, (10, 10))  # Blit score text

    pygame.display.flip()  # Update display
    print("Render Done")
#

what's wrong because I'm not getting any print statements after this eather

rapid niche
#

you sure it's in the render or any of it's callbacks?

#

the callbacks are just a couple of numbers multiplied and added

#

nevermind

proper scaffold
rapid niche
#

you got

    def randomize_Target_position(self):
        logging.info("Randomizing target position.")
        while True:
            self.Target_rect.topleft = (random.randint(50, self.width - 50),
                                        random.randint(50, self.height - 50))  # Set random target position
            joint_end_x, joint_end_y = self._calculate_joint_end()  # Calculate joint end position
            joint2_end_x, joint2_end_y = self._calculate_joint2_end()  # Calculate joint2 end position
            if not self.Target_rect.collidepoint(joint_end_x, joint_end_y) and not self.Target_rect.collidepoint(
                    joint2_end_x, joint2_end_y):  # Check if target position is valid
                break
        logging.info(f"Target randomized to: {self.Target_rect.topleft}")  # Log target position
        self.joint2_length = 100  # Reset joint2 length
        self.joint2_angle = 0  # Reset joint2 angle
        while self.joint2_position_loop == 1:  # Assign valid starting position to joint2
            self.x_start2, self.y_start2 = joint_end_x, joint_end_y
        print("Randomize Target Position Done")

which seems like the most possible place for your code to get stuck in

proper scaffold
#

Thank you, I will look into that

rapid niche
#
while self.joint2_position_loop == 1:  # Assign valid starting position to joint2
   self.x_start2, self.y_start2 = joint_end_x, joint_end_y

should be an infinite loop since you don't change the variable in the loop and it's not a property either

proper scaffold
#

That loop is trying to alway's set the starting position of joint2 to the end of joint1 because thier supposed to be connected. Unfortunately I didn't know any other way of doing this

rapid niche
#

joint2_position_loop is a variable of self, since no calls are being made to any function, only x_start2 and y_start2 are changing, then it makes it an infinite loop

#

it's not about what it does, it comes down to be a loop of the form

while a:
    b = c
#

since a does not change, the loop is infinite if it's ever reached

proper scaffold
#

that was the goal

#

because joint2 should alway's move to joint1

rapid niche
#

nonono, you're mixing things

#

while you're on the loop, nothing happens other than the code in the loop

proper scaffold
#

so how do I do both

rapid niche
#

!d threading

solar garnetBOT
#

Source code: Lib/threading.py

This module constructs higher-level threading interfaces on top of the lower level _thread module.

Changed in version 3.7: This module used to be optional, it is now always available.

rapid niche
#

!d multiprocessing

solar garnetBOT
rapid niche
#

!d asyncio could be as well

solar garnetBOT
#

Hello World!

import asyncio

async def main():
    print('Hello ...')
    await asyncio.sleep(1)
    print('... World!')

asyncio.run(main())
```...
rapid niche
#

but I don't think this is what you want for this especific example though

#

but look those up

#

I recomend threading, most begginer friendly

proper scaffold
#

ok I'm about to go to bed, its really late for me

#

also thankyou so much

#

this has stumped me for ages

#

good luck helping otther people

rapid niche
#

yeah I should as well

#

nearly 3 AM here

#

good night then

proper scaffold
#

geez

#

good night

#

thanks again

solar garnetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.