Using infinite loops in Pygame

39 views Asked by At

I've got a slight problem. I had some working code to simulate a game. Now I'm trying to visualise that by using pygame but it doesn't seem to be able to cope with some of the infinite loops (at least that's my assesment). When I run the code the pygame window simply stops responding. How can I get it to actually work and respond to inputs?


#importing stuff
import pygame
import os
import random
#setting constants and variables
WIDTH,HEIGHT=900,500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
BOARD=pygame.image.load(os.path.join("pygame2","UrImage.jpeg"))
pygame.display.set_caption("Royal Game of Ur")
BOARD=pygame.transform.scale(BOARD,(900,500))
dwwtc=True
COLOUR=(1,255,1)
FPS=5
clock=pygame.time.Clock()
CIRCLES=[]
#importing and resizing pictures
FILES=["th.jpeg","R.jpeg","redc.jpeg","OIP.jpeg","OIP (2).jpeg","OIP (3).jpeg","OIP (4).jpeg","OIP (5).jpeg","OIP (6).jpeg","OIP (7).jpeg","OIP (8).jpeg","OIP (9).jpeg","OIP (10).jpeg","OIP (11).jpeg"]
for index,File in enumerate(FILES):
    Kreis=pygame.image.load(os.path.join("pygame2",File))
    CIRCLES.append(Kreis)
for index,Circle in enumerate(CIRCLES):
    CIRCLES[index]=pygame.transform.scale(Circle,(20,20))
#setting the coordinates corresponding to fields of the board
movement=[(10,10),(20,20),(30,30),(40,40),(50,50),(60,60),(70,70)]
#main game loop
while dwwtc:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            dwwtc=False
    WIN.fill(COLOUR)
    WIN.blit(BOARD,(0,0))
    pygame.display.update()
#positions of the figurines
    positions={0:[0,0,0,0,0,0,0],1:[0,0,0,0,0,0,0]}
#setting variables which need to be updated
    endofgame=0
    figurines_before_end=0
    go_to_next_player=0
    rosettelist=[4,8]
    #actual game
    while True:
        for index,(player,value) in enumerate(positions.items()):
            while True:
#set change
                #start=pygame.key.get_pressed
                #if start==pygame.key.get_pressed():
                a=random.randint(0,1)
                b=random.randint(0,1)
                c=random.randint(0,1)
                d=random.randint(0,1)
                change=a+b+c+d
                print(change)
#state opposing player
                if index==0:
                    opponent=1
                elif index==1:
                    opponent=0
#checking pressed key and corresponding figurine
                while True:
                    key=pygame.key.get_pressed()
                    if True in key:
                        if key[pygame.K_0]:
                            figurine=0
                            break
                        elif key[pygame.K_1]:
                            figurine=1
                            break
                        elif key[pygame.K_2]:
                            figurine=2
                            break
                        elif key[pygame.K_3]:
                            figurine=3
                            break
                        elif key[pygame.K_4]:
                            figurine=4
                            break
                        elif key[pygame.K_5]:
                            figurine=5
                            break
                        elif key[pygame.K_6]:
                            figurine=6
                            break
#checking conditions for figurine movement
                        if 0 <= figurine <= 6:
                            print("a")
                            if positions[index][figurine] <=14:
                                print("b")
                                if positions[index][figurine]+change <16:
                                    print("c")
# if positions[index][figurine]+change not in positions[index] and positions[index][figurine]<15:
                                        print("d")
                                        break
                                    elif positions[index][figurine]+change==15:
                                        print("z")
                                        break
                                    elif change==0:
                                        print("j")
                                        break
                                    elif positions[index][figurine]+change==7 and 7 in positions[opponent]:
                                        print("y")
                                        continue
#checking if it's possible to add the change without the figurine going further than the end field
                                else:
                                    for v in positions[index]:
                                        if v+change<16:
                                            figurines_before_end=1
                                            break
                                        else:
                                            figurines_before_end=2
                                if figurines_before_end==2:
                                    change=0
                                    break    
#If all conditions have been met, the change is added to the position
                        positions[index][figurine]+=change
#Show the player's circle at its new position
                        if opponent==0:
                            WIN.blit(CIRCLES[figurine],(movement[positions[index][figurine]]))
                        elif opponent==1:
                            WIN.blit(CIRCLES[figurine*2],(movement[positions[index][figurine]]))
                        pygame.display.update()
                        print(positions)
#If the figurine of the opponent is in the new position, they're sent back to the start
                        if positions[index][figurine] in positions[opponent] and 5<= positions[index][figurine] <=12:
                            for indexchen,werte in enumerate(positions[opponent]):
                                
                                if werte==positions[index][figurine]:
                                    positions[opponent][indexchen]=0
                                    if opponent==1:
                                        WIN.blit(CIRCLES[index],(movement[0]))
                                    elif opponent==0:
                                        WIN.blit(CIRCLES[index*2],(movement[0]))
                                    break
                                pygame.display.update()
                        print(positions)
#Check if player is entitled to a second go
                        if positions[index][figurine] not in rosettelist:
                            go_to_next_player=1
                            break
                if go_to_next_player==1:
                    break
#Check if all figurines have reached the end and react appropriately
            if all(value>14 for value in positions[index]):
                endofgame=1
                break
        if endofgame==1:
            break
        pygame.display.update()
pygame.quit()

I've written a few smaller programs, including one which could react to keyboard inputs. I then put this into an infinite loop and the pygame window stopped responding. Since I've got several infinite loops in my actual code, I guessed that they're the problem.

0

There are 0 answers