Reputation: 213
Right now, I am working on a small RPG with 3 classes and the ability to have your own username. I have a problem with my main script. The problem is, the bolded if
statement needs to be a while loop in order to add my next piece, the main menu. However when I try to use the while keyword it causes the game to stop responding.
Thanks in advance to anyone who helps in any way.
Here is my code:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(green)
while User_name == None:
User_name = Username.ask(gameDisplay,"username")
print User_name + "'s run/dio"
if Class_Choice == None:
Choice_wall().paint(gameDisplay)
Class_Choice = Choice_wall().Choice(pygame.mouse.get_pos(
[0],0,pygame.mouse.get_pressed()[0],0,0)
pygame.display.update()
clock.tick(30)
Here are the functions:
def Choice(self, c1,c2 , p1,p2,p3):
while p1 == 0:
if 0 <= c1 <= 319 and p1 == 1 :
return "warrior"
elif 320 <= c1 <= 639 and p1 == 1:
return "archer"
elif 640 <= c1 <= 960 and p1 == 1:
return "mage"
def get_key():
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
def display_box(screen, message):
"Print a message in a box in the middle of the screen"
fontobject = pygame.font.Font(None,18)
pygame.draw.rect(screen, (0,0,0),
((screen.get_width() / 2) - 100,
(screen.get_height() / 2) - 10,
200,20), 0)
pygame.draw.rect(screen, (255,255,255),
((screen.get_width() / 2) - 102,
(screen.get_height() / 2) - 12,
204,24), 1)
if len(message) != 0:
screen.blit(fontobject.render(message, 1, (255,255,255)),
((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
pygame.display.flip()
def ask(screen, question):
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, question + ": " + string.join(current_string,""))
while 1:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, question + ": " + string.join(current_string,""))
return string.join(current_string,"")
Upvotes: 0
Views: 574
Reputation: 104702
The issue is almost certainly the loop in your Choice_Wall.Choice
method:
while p1 == 0:
Since p1
never changes in the loop (and it will never return if the loop condition was met), you're always going to loop forever if that condition is True
the first time the method is called. You probably need to move the calls to pygame.mouse.get_pos
and pygame.mouse.get_pressed
inside the loop for there to be any chance of it working the way you intend.
An alternative might be to remove the inner loop entirely, since the if Class_Choice == None
line in calling code will cause it to keep calling Choice
on each iteration of the main loop until a choice is made.
Some further notes unrelated to the infinite loop issue: It would also be a lot more natural to create a Class_Wall
instance just once, rather than twice on each frame it is needed. Of course, you might not even need a class at all if it doesn't contain any state (just one or more functions, like you're using for the username above). And as another user commented, it can be very helpful for other people's understanding if you follow a more typical naming convention in your code, such as lowercase_with_underscores
for your function, method and module names (reserving CapitalizedNames
for classes).
Upvotes: 1