Pygame Nash Equlibruim

Viewed 35

I'm a beginner in Python, I'm trying to create a game based on the concept of the prisoner's dilemma from the game theory. My goal is then to incorporate an AI(Q-learning...) for player 2 to see if he ends up performing that the dominant strategy.

For the moment I am trying to make the game work by making player 2 choose his strategy randomly.

However I can't make the game work (player 2 doesn't choose any choice after player 1 has chosen his strategy).

Moreover, the score does not update. Thank you for your help.

images: Bandit: https://zupimages.net/viewer.php?id=22/37/b4qj.png Board https://zupimages.net/viewer.php?id=22/37/iz8k.png

#Initialisation

pygame.init()

#Création de la fenêtre

width = 600
height = 600 
screen = pygame.display.set_mode((width,height))

pygame.display.set_caption("Nash Equilibrium")

#Importation des images

Bandit = pygame.image.load("Images/Bandit.png")
Board = pygame.image.load("Images/Board.png")

#Fps

timer = pygame.time.Clock()
fps=60

#Variables

Button_Cooperate_P1_enabled = True
Button_Defect_P1_enabled = True
Button_Cooperate_P2_enabled = True
Button_Defect_P2_enabled= True

new_press = True

Score_B = 0
Score_R = 0

Turn_B = False 
Turn_R = False

Result_B=0
Result_R=0

C_or_D = 0

#Font

font = pygame.font.Font(None, 30)

#Surfaces

text_surface_SB = font.render("Score:" + str(Score_B), True, "Blue")
text_surface_SR = font.render("Score:" + str(Score_R), True, "Red")

text_surface_P1 = font.render("Prisoner 1", True, "Blue")
text_surface_P2 = font.render("Prisoner 2", True, "Red")


#Fonction

#Class
    
    
class Button_B:
    def __init__(self, text, x_pos, y_pos, enabled):
        self.text = text
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.enabled = enabled

        
    def check_click_B(self):
        mouse_pos = pygame.mouse.get_pos()
        left_click = pygame.mouse.get_pressed()[0]
        button_rect = pygame.rect.Rect((self.x_pos, self.y_pos),(200,75)) 
        if left_click and button_rect.collidepoint(mouse_pos) and self.enabled:
            return True
        else:
            return False
                
    def draw_B(self):
        button_text = font.render(self.text , True, "Blue")
        button_rect = pygame.rect.Rect((self.x_pos, self.y_pos),(200,75))
        if self.enabled:
            if self.check_click_B():
                pygame.draw.rect(screen, "Blue", button_rect, 0 , 5)
                return True
            else:
                pygame.draw.rect(screen, "White", button_rect, 0 , 5)

        else:
            pygame.draw.rect(screen, "Black", button_rect, 0 , 5)

        pygame.draw.rect(screen, "Blue", button_rect, 2 , 5)  
        screen.blit(button_text, (self.x_pos + 3, self.y_pos + 3))
        
        
           
    def action_B(self):
        global Score_B
        if self.draw_B():
            
            Turn_B = True
            if self.text == "Cooperate":
                Result_B = "Cooperate"

            if self.text == "Defect":
                result_B = "Defect"
                

        
        
        
        
class Button_R:
    def __init__(self, text, x_pos, y_pos, enabled):
        self.text = text
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.enabled = enabled
    

    
        
    def draw_R(self):
        button_text = font.render(self.text , True, "Red")
        button_rect = pygame.rect.Rect((self.x_pos, self.y_pos),(200,75))
        if self.enabled:
            if Result_R == self.text:
                pygame.draw.rect(screen, "Red", button_rect, 0 , 5)
            else:
                pygame.draw.rect(screen, "White", button_rect, 0 , 5)

        else:
            pygame.draw.rect(screen, "Black", button_rect, 0 , 5)

        pygame.draw.rect(screen, "Red", button_rect, 2 , 5)  
        screen.blit(button_text, (self.x_pos + 3, self.y_pos + 3))
    
    
            
#Affichage des images et textes
    
screen.blit(Bandit,(250,30))
screen.blit(Bandit,(250,470))

screen.blit(Board,(50,150))
    
screen.blit(text_surface_P1,(245,570))
screen.blit(text_surface_P2,(245,10))
    
        
# Game Loop

run = True 
while run == True:
    
    #Quit
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()   
            
    #Instances  

    Button_Cooperate_P1 = Button_B("Cooperate", 25, 450, Button_Cooperate_P1_enabled)
    Button_Defect_P1 = Button_B("Defect", 350, 450, Button_Defect_P1_enabled)
        
    Button_Cooperate_P2 = Button_R("Cooperate", 25, 50, Button_Cooperate_P2_enabled)
    Button_Defect_P2 = Button_R("Defect", 350, 50, Button_Defect_P2_enabled)
    
    #Fonctions de mise à jour
    
    Button_Cooperate_P1.check_click_B()
    Button_Defect_P1.check_click_B()
    
    #Fonctions logiques
    
    if pygame.mouse.get_pressed()[0] and new_press:
#        new_press = False
        if Button_Cooperate_P1.check_click_B():
            if Button_Defect_P1_enabled:
                Button_Defect_P1_enabled = False
        
    if pygame.mouse.get_pressed()[0] and new_press:
        if Button_Defect_P1.check_click_B():
            if Button_Cooperate_P1_enabled:
                Button_Cooperate_P1_enabled = False

    
    #Dessin
    
    Button_Cooperate_P1.draw_B()
    Button_Defect_P1.draw_B()

    #Delay Framerate
    
    #Mise à jour de l'écran    
    
    Button_Cooperate_P1.action_B()
    Button_Defect_P1.action_B()
    
    if Turn_B == True:
        C_or_D = random.randint(1,2)
        print(C_or_D)
        if C_or_D == 1:
            Result_R = "Cooperate"
            Turn_R = True
        if C_or_D == 2:
            Result_R = "Defect"
            Turn_R = True
    
    Button_Cooperate_P2.draw_R()
    Button_Defect_P2.draw_R()
        
    
    if Turn_R == True:
        Result = str(Result_B)+str(Result_R)
        Turn_B = False
        Turn_R = False
        if Result == "CooperateCooperate":
            Score_B = Score_B - 3
            Score_R = Score_R - 3
        if Result == "CooperateDefect":
            Score_B = Score_B
            Score_R = Score_R - 5
        if Result == "DefectCooperate":
            Score_B = Score_B - 5
            Score_R = Score_R
        if Result == "DefectDefect":
            Score_B = Score_B - 1
            Score_R = Score_R - 1
            
    screen.blit(text_surface_SB,(10,10))
    screen.blit(text_surface_SR,(460,10))
    

    
    pygame.display.update()
0 Answers
Related