I’m currently working on a mini-game in Python to better understand how buttons function. My challenge is figuring out how to simulate the computer “clicking” the button that corresponds to its choice. I’m not sure how to approach this and would appreciate any guidance or suggestions.
Thank you in advance!
down here is my code that im working with the game logic works fine
from tkinter import *
import tkinter.font as tkFont
import random
moves = ['rock', 'paper', 'scissor']
first = Tk()
first.geometry("1800x1200")
first.config(background="#453e3d")
# Images
Scissor = PhotoImage(file=r"C:UsersSetupGamePicturescute-scissors-icon-png-removebg-preview.png")
Rock = PhotoImage(file=r"C:UsersSetupGamePicturesThe-Rock-PNG-Photo-removebg-preview.png")
Paper = PhotoImage(file=r"C:UsersSetupGamePicturespng-clipart-old-paper-old-paper-parchment-thumbnail-removebg-preview.png")
# Fonts
new_font_r = tkFont.Font(family="Rocks Personal Use", size=80)
new_font_p = tkFont.Font(family="Paperkind", size=50)
new_font_s = tkFont.Font(family="Edward Scissorhands", size=90)
# Variables to store moves
player_choice = None
computer_choice = None
# Functions to set player moves
def set_rock():
global player_choice
player_choice = 'rock'
computer_move()
game_logic()
def set_paper():
global player_choice
player_choice = 'paper'
computer_move()
game_logic()
def set_scissor():
global player_choice
player_choice = 'scissor'
computer_move()
game_logic()
def computer_move():
global computer_choice
computer_choice = random.choice(moves)
print(f"The computer chose: {computer_choice}")
def game_logic():
if player_choice == computer_choice:
print("It's a TIE!")
elif (player_choice == 'rock' and computer_choice == 'scissor') or
(player_choice == 'scissor' and computer_choice == 'paper') or
(player_choice == 'paper' and computer_choice == 'rock'):
print("The Winner is You!")
else:
print("The Winner is The Computer!")
# Buttons
button_rock = Button(first, text="Rock", font=new_font_r, image=Rock, compound=BOTTOM, command=set_rock, width=500, height=600)
button_rock.place(x=0, y=50)
button_paper = Button(first, text="Paper", font=new_font_p, image=Paper, compound=BOTTOM, command=set_paper, width=500, height=600)
button_paper.place(x=600, y=50)
button_scissor = Button(first, text="Scissor", font=new_font_s, image=Scissor, compound=BOTTOM, command=set_scissor, width=500, height=600)
button_scissor.place(x=1200, y=50)
first.mainloop()