0

I coded this game myself. It is a guessing game regarding 2 players.

from random import randint
from tkinter import *

p1 = 0 
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)



 if p1 == 3:
   label["text"] = "Player 1 won!"
   exit()
 elif p2 == 3:
   label["text"] = "Player 2 won!"
   exit()
 else:
   continue

Before I added this "continue" , I could print Tk properly but could not loop. After adding "continue", my Tk can't even load now. Why? Please help. Thanks.

mainloop()

The reason why I did not use PyGame is because I wanted my game to be a "Answer first, get point" type of game.

Devansh Soni
  • 771
  • 5
  • 16
Brandon
  • 3
  • 6
  • It is because `continue` will skip the code followed and start from the beginning of `while` loop. That means `mainloop()` will be skipped when the first two if conditions return False. – acw1668 Apr 20 '20 at 09:33
  • that means my mainloop() should be above continue? – Brandon Apr 20 '20 at 09:36
  • Check what I have put that should work for you – Zack Turner Apr 20 '20 at 09:38
  • 1
    Actually you should not put that if block there because it is useless. You should note that `tkinter` is event-driven. – acw1668 Apr 20 '20 at 09:39
  • Currently, the question and the options are all the same. Like this my game is no fun as the players will know the answers once they click the correct one. As such, I need to edit my code such that the question and the options change. But I do not know how to do it. Can someone help? Thanks :) – Brandon Apr 20 '20 at 13:19

4 Answers4

1

Have you tried adding master.mainloop() at the end and getting rid of continue

from random import randint
from tkinter import *

p1 = 0
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)

 if p1 == 3:
   label["text"] = "Player 1 won!"
   exit()
 elif p2 == 3:
   label["text"] = "Player 2 won!"
   exit()

 master.mainloop()

This works for me.

Zack Turner
  • 216
  • 3
  • 14
1

You need to call mainloop() at some point to get the window to show, your cord should look something like this:


from random import randint
from tkinter import *

p1 = 0 
p2 = 0
while p1 < 3 and p2 < 3:
 answer = randint(0,16)
 wrong_answer = randint(0,16)
 wrong_answer1 = randint(0,16)


 s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
 hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
 secret_word = s_list[answer]
 hint = f'Hint:{hint_list[answer]}'
 incorrect = s_list[answer]
 incorrect1 = s_list[answer]


 master = Tk()
 master.title("2 Players Guessing Game!")
 master.geometry('700x900+90+90')

 def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2

 def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2

 label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
 label2 = Label(master, text="<- P1", font = "Arial 14")
 label3 = Label(master, text="P2 ->", font = "Arial 14")
 button = Button(master, text=secret_word, font = "Arial 14", command=random1)
 button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
 button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
 button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
 button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
 button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
 label4 = Label(master, text=hint, font = "Arial 14")


 label.pack()
 label2.pack()
 label3.pack()
 label4.pack()
 button.pack(side=LEFT)
 button2.pack(side=LEFT)
 button3.pack(side=LEFT)
 button4.pack(side=RIGHT)
 button5.pack(side=RIGHT)
 button6.pack(side=RIGHT)

 master.mainloop()

Tkinter understanding mainloop I hope this helps

andypaling1
  • 142
  • 12
0

GUI toolkits like tkinter are event-driven. To work properly, the mainloop must continuously be able to process keyboard and mouse events.

So they work quite differently from normal Python scripts that just run from top to bottom.

A tkinter program runs within the mainloop. So there are only three things you do before starting the mainloop.

  • Create a window with some widgets on it.
  • Initialize global program state.
  • Define functions that can be run from the mainloop as callbacks or idle tasks.

A callback is called in response to activating a control (like clicking on a button).

An idle task is started by the system after a specified number of milliseconds when the system is not busy processing events. You can schedule idle tasks with the Tk.after() method.

Basically, the callbacks and idle tasks are your program.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

You should note that tkinter is event-driven, and you should not apply procedural/sequential programming on it.

For your case, you don't need the while loop and you should put the checking winner logic in a function and call it inside those randomX() functions:

from random import randint
from tkinter import *

p1 = 0 
p2 = 0

answer = randint(0,16)
wrong_answer = randint(0,16)
wrong_answer1 = randint(0,16)

s_list =    ["apple","book","phone","sheep","ruler","pen","eraser","knife","cement","Google","file","stapler","thermometer","box","glue","yes","no"]
hint_list = ["Fruit","Read","Technology","Animal","Measure Length","Writing","Stationary","Cut  food", "Building Material","Search Engine","Paper organizer","Binding papers together","Temperature","Storage","Attach things together","Approval","Disapproval"]
secret_word = s_list[answer]
hint = f'Hint:{hint_list[answer]}'
incorrect = s_list[answer]
incorrect1 = s_list[answer]

master = Tk()
master.title("2 Players Guessing Game!")
master.geometry('700x900+90+90')

def check_winner():
    winner = 1 if p1 == 3 else 2 if p2 == 3 else None
    if winner:
        print(f'Player {winner} won!')
        master.destroy() # end the program

def random1():
   global p1
   p1 += 1
   label4["text"] = "Correct! Score:",p1 ,p2
   check_winner()

def random2():
   global p2
   p2 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2
   check_winner()

def random3():
   global p2
   p2 += 1
   label4["text"] = "Correct! Score:",p1 ,p2
   check_winner()

def random4():
   global p1
   p1 += 1
   label4["text"] = "Incorrect! Score:",p1 ,p2
   check_winner()

label = Label(master, text="2 Player Guessing Game!", font  = "Arial 14")
label2 = Label(master, text="<- P1", font = "Arial 14")
label3 = Label(master, text="P2 ->", font = "Arial 14")
button = Button(master, text=secret_word, font = "Arial 14", command=random1)
button2 = Button(master, text=incorrect, font = "Arial 14", command=random2)
button3 = Button(master, text=incorrect1, font = "Arial 14", command=random2)
button4 = Button(master, text=secret_word, font = "Arial 14", command=random3)
button5 = Button(master, text=incorrect, font = "Arial 14", command=random4)
button6 = Button(master, text=incorrect1, font = "Arial 14", command=random4)
label4 = Label(master, text=hint, font = "Arial 14")

label.pack()
label2.pack()
label3.pack()
label4.pack()
button.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=RIGHT)
button5.pack(side=RIGHT)
button6.pack(side=RIGHT)

master.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34