#๐ Why does pygame keep crashing?
61 messages ยท Page 1 of 1 (latest)
@proper scaffold
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.
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
try this code and paste the log here
https://paste.pythondiscord.com/4OIA
It still says not responding
I'm going to try running it for longer though
Thank you so much for helping
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
Maybe add some prints, too, might help narrow down where it's hanging
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
control + C raises an exception if you do it in the terminal, it's called keyboard interrupt
since it raises an exception you'll see where you're code at
I tried to do that but by the time I can see the window it is already not responding now
.
what does that mean?
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)
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
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
I've changed my rendering code a bit
New code:
https://paste.pythondiscord.com/UY5A
However its still not working and I don't know why, thank you for helping me so far
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
Thank you, I will look into that
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
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
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
nonono, you're mixing things
while you're on the loop, nothing happens other than the code in the loop
so how do I do both
!d threading
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.
!d multiprocessing
Source code: Lib/multiprocessing/
Availability: not Android, not iOS, not WASI.
This module is not supported on mobile platforms or WebAssembly platforms.
!d asyncio could be as well
Hello World!
import asyncio
async def main():
print('Hello ...')
await asyncio.sleep(1)
print('... World!')
asyncio.run(main())
```...
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
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
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.