Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGUI] GUIS
#1
Ive created this code for a dice game... it is a two player game with an authentication before it, its for a GCSE project.
I want all of the outputs to appear in a seperate tabb i also want to add in a GUI button to roll the dice for each player each round. Can anyone point me in the right directiom. Thankyou in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
print("================================")
print("      Enya's Dice game          ")
print("================================")
import time
time.sleep(3)
 
 
player1= input("player 1 what is your name?") #asks the first player for their name
print("Hello" , player1)#outputs on the screen hello and then the players name
print("")
print("$$$================================$$$")
print("      password authentication         ")
print("$$$================================$$$")
print("")
password1= input("please enter the top secrete password: ")#asks you to enter password
        
if password1 == "dice":
    print("authentication complete:)")#this is the code for the complete password
elif password1 == "quit": #if the password is quit on your first attempt then it will say good bye
        print("goodbye :(")
        quit()
 
while password1 != "dice":
    password1= input("please try again: ")#asks you to enter password  
    if password1 == "quit": #after your first atempt you can quit then it will say good bye
        print("goodbye :(")
        quit()
    elif password1 == "dice": #code for after your first atempt and the password is correct.
        print("authentication complete")
#the following is the authentication process for the second player
player2= input("player 2 what is your name?") #asks the first player for their name
print("Hello" , player2)#outputs on the screen hello and then the players name
print("")
print("$$$================================$$$")
print("      password authentication         ")
print("$$$================================$$$")
print("")
password2= input("please enter the top secrete password: ")#asks you to enter password
         
     
if password2 == "dice":
    print("authentication complete:)") #this is the code for the complete password
elif password2 == "quit": #if the password is quit on your first attempt then it will say good bye
        print("goodbye :(")
        quit()
 
while password2 != "dice":
    password2= input("please try again: ")#asks you to enter password  
    if password2 == "quit": #after your first atempt you can quit then it will say good bye
        print("goodbye :(")
        quit()
    elif password2 == "dice": #code for after your first atempt and the password is correct.
        print("authentication complete")
#the following will output the rules
print(player1, "vs", player2, "you will now take part in 5 rounds of dice game")
print("these are the rules: ")
print("")
print("- 2 six sided dice will be rolled at RANDOM")
print("")
print("- If the total is an even number, an additional 10 points are added to their score.")
print("")
print("- If the total is an odd number, 5 points are subtracted from their score.")
print("")
print("- The person with the highest score at the end of 5 rounds wins")
print("")
print("good luck!")
print("")
time.sleep(20) # this gives a time dalay for the next outputs of 10 seconds
 
print("")
print("")
 
 
print("Round 1")
print(player1)
time.sleep(3)
print("Dice rolling...")
time.sleep(3)
print("")
from random import *
 
P1R1N1= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 1 variable
P1R1N2= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 2 variable
 
if P1R1N1 == P1R1N2: #if the dice have both the same number then both dice will role 2 new numbers
    P1R1N3= randint(1,6)
    P1R1N4= randint(1,6)
    totalR1p1= (P1R1N1 + P1R1N2 + P1R1N3 + P1R1N4) #these 2 new numbers will then be aded to form a total
    print("you rolled a double, you scored a ", P1R1N1, "and a ", P1R1N2, "so we gave you two more rolls you scored a : ", P1R1N3, "and a: ", P1R1N4, "These have been added to your original score your total is: ", totalR1p1)
else:
    totalR1p1= (P1R1N1 + P1R1N2) #if the dice did not role two of the same numbers then the total of the dice are just added up and the total is outputed # it is then stored in the variable totalR1p1 which is the total for the players 1 round this will be later used to add up all of the players totals
    print("you rolled a ", P1R1N1, "and a ",P1R1N2, "your total is ", totalR1p1)
 
print("")
print("")
 
time.sleep(5)
print(player2)
print("")
time.sleep(3)
print("Dice rolling...")
time.sleep(3)
print("")
#this repeates the process for player 2
from random import *
 
P2R1N1= randint(1,6)
P2R1N2= randint(1,6)
 
if P2R1N1 == P2R1N2:
    P2R1N3= randint(1,6)
    P2R1N4= randint(1,6)
    totalR1p2= (P2R1N1 + P2R1N2 + P2R1N3 + P2R1N4)
    print("you rolled a double, you scored a ", P2R1N1, "and a ", P2R1N2, "so we gave you two more rolls you scored a : ", P2R1N3, "and a: ", P2R1N4, "These have been added to your original score your total is: ", totalR1p2)
else:
    totalR1p2= (P2R1N1 + P2R1N2)
    print("you rolled a ", P2R1N1, "and a ",P2R1N2, "your total is ", totalR1p2)
print("")   
time.sleep(3)
if totalR1p1 > totalR1p2:
    print(player1, "wins round 1")
else:
    print(player2, "wins round 1")
     
print("")
print("")
     
     
print("Round 2")
print("")
print(player1)
print("")
time.sleep(3)
print("Dice rolling...")
time.sleep(3)
print("")
from random import *
 
P1R2N1= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 1 variable
P1R2N2= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 2 variable
 
if P1R2N1 == P1R1N2: #if the dice have both the same number then both dice will role 2 new numbers
    P1R2N3= randint(1,6)
    P1R2N4= randint(1,6)
    totalR2p1= (P1R2N1 + P1R2N2 + P1R2N3 + P1R2N4) #these 2 new numbers will then be aded to form a total
    print("you rolled a double, you scored a ", P1R2N1, "and a ", P1R2N2, "so we gave you two more rolls you scored a : ", P1R2N3, "and a: ", P1R2N4, "These have been added to your original score your total is: ", totalR2p1)
else:
    totalR2p1= (P1R2N1 + P1R2N2) #if the dice did not role two of the same numbers then the total of the dice are just added up and the total is outputed # it is then stored in the variable totalR1p1 which is the total for the players 1 round this will be later used to add up all of the players totals
    print("you rolled a ", P1R2N1, "and a ",P1R2N2, "your total is ", totalR2p1)
 
print("")
print("")
 
time.sleep(5)
print(player2)
print("")
time.sleep(3)
print("Dice rolling...")
print("")
time.sleep(3)
#this repeates the process for player 2
from random import *
 
P2R2N1= randint(1,6)
P2R2N2= randint(1,6)
 
if P2R2N1 == P2R1N2:
    P2R2N3= randint(1,6)
    P2R2N4= randint(1,6)
    totalR2p2= (P2R2N1 + P2R2N2 + P2R2N3 + P2R2N4)
    print("you rolled a double, you scored a ", P2R2N1, "and a ", P2R2N2, "so we gave you two more rolls you scored a : ", P2R2N3, "and a: ", P2R2N4, "These have been added to your original score your total is: ", totalR2p2)
else:
    totalR2p2= (P2R2N1 + P2R2N2)
    print("you rolled a ", P2R2N1, "and a ",P2R2N2, "your total is ", totalR2p2)
print("")
time.sleep(3)
if totalR2p1 > totalR2p2:
    print(player1, "wins round 2")
else:
    print(player2, "wins round 2")
print("")
 
totalP1= (totalR1p1 + totalR2p1)
print(player1, "your total score at the end of round 2 is ", totalP1)
totalP2= (totalR1p2 + totalR2p2)
print(player2, "your total score at the end of round 2 is ", totalP2)
 
print("")
print("")
 
 
print("Round 3")
print("")
print(player1)
time.sleep(3)
print("")
print("Dice rolling...")
time.sleep(3)
print("")
from random import *
 
P1R3N1= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 1 variable
P1R3N2= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 2 variable
 
if P1R3N1 == P1R3N2: #if the dice have both the same number then both dice will role 2 new numbers
    P1R3N3= randint(1,6)
    P1R3N4= randint(1,6)
    totalR3p1= (P1R3N1 + P1R3N2 + P1R3N3 + P1R3N4) #these 2 new numbers will then be aded to form a total
    print("you rolled a double, you scored a ", P1R3N1, "and a ", P1R3N2, "so we gave you two more rolls you scored a : ", P1R3N3, "and a: ", P1R3N4, "These have been added to your original score your total is: ", totalR3p1)
else:
    totalR3p1= (P1R3N1 + P1R3N2) #if the dice did not role two of the same numbers then the total of the dice are just added up and the total is outputed # it is then stored in the variable totalR1p1 which is the total for the players 1 round this will be later used to add up all of the players totals
    print("you rolled a ", P1R3N1, "and a ",P1R3N2, "your total is ", totalR3p1)
 
print("")
print("")
 
time.sleep(5)
print(player2)
time.sleep(3)
print("")
print("Dice rolling...")
time.sleep(3)
#this repeates the process for player 2
from random import *
print("")
 
P2R3N1= randint(1,6)
P2R3N2= randint(1,6)
 
if P2R3N1 == P2R3N2:
    P2R3N3= randint(1,6)
    P2R3N4= randint(1,6)
    totalR3p2= (P2R3N1 + P2R3N2 + P2R3N3 + P2R3N4)
    print("you rolled a double, you scored a ", P2R3N1, "and a ", P2R3N2, "so we gave you two more rolls you scored a : ", P2R3N3, "and a: ", P2R3N4, "These have been added to your original score your total is: ", totalR3p2)
else:
    totalR3p2= (P2R3N1 + P2R3N2)
    print("you rolled a ", P2R3N1, "and a ",P2R3N2, "your total is ", totalR3p2)
print("")  
time.sleep(3)
if totalR3p1 > totalR3p2:
    print(player1, "wins round 3")
else:
    print(player2, "wins round 3")
print("")
totalP1= (totalR1p1 + totalR2p1 + totalR3p1)
print(player1, "your total score at the end of round 3 is ", totalP1)
print("")
totalP2= (totalR1p2 + totalR2p2 + totalR3p2)
print(player2, "your total score at the end of round 3 is ", totalP2)
     
print("")
print("")
     
     
print("Round 4")
print("")
print(player1)
print("")
time.sleep(3)
print("Dice rolling...")
time.sleep(3)
print("")
from random import *
 
P1R4N1= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 1 variable
P1R4N2= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 2 variable
 
if P1R4N1 == P1R4N2: #if the dice have both the same number then both dice will role 2 new numbers
    P1R4N3= randint(1,6)
    P1R4N4= randint(1,6)
    totalR4p1= (P1R4N1 + P1R4N2 + P1R4N3 + P1R4N4) #these 2 new numbers will then be aded to form a total
    print("you rolled a double, you scored a ", P1R4N1, "and a ", P1R4N2, "so we gave you two more rolls you scored a : ", P1R4N3, "and a: ", P1R4N4, "These have been added to your original score your total is: ", totalR4p1)
else:
    totalR4p1= (P1R4N1 + P1R4N2) #if the dice did not role two of the same numbers then the total of the dice are just added up and the total is outputed # it is then stored in the variable totalR1p1 which is the total for the players 1 round this will be later used to add up all of the players totals
    print("you rolled a ", P1R4N1, "and a ",P1R4N2, "your total is ", totalR4p1)
 
print("")
print("")
 
time.sleep(5)
print(player2)
print("")
time.sleep(3)
print("Dice rolling...")
print("")
time.sleep(3)
#this repeates the process for player 2
from random import *
 
P2R4N1= randint(1,6)
P2R4N2= randint(1,6)
 
if P2R4N1 == P2R4N2:
    P2R4N3= randint(1,6)
    P2R4N4= randint(1,6)
    totalR4p2= (P2R4N1 + P2R4N2 + P2R4N3 + P2R4N4)
    print("you rolled a double, you scored a ", P2R4N1, "and a ", P2R4N2, "so we gave you two more rolls you scored a : ", P2R4N3, "and a: ", P2R4N4, "These have been added to your original score your total is: ", totalR4p2)
else:
    totalR4p2= (P2R4N1 + P2R4N2)
    print("you rolled a ", P2R4N1, "and a ",P2R4N2, "your total is ", totalR4p2)
print("")
time.sleep(3)
if totalR4p1 > totalR4p2:
    print(player1, "wins round 4")
else:
    print(player2, "wins round 4")
print("")
 
totalP1= (totalR1p1 + totalR2p1 + totalR3p1 + totalR4p1)
print(player1, "your total score at the end of round 4 is ", totalP1)
print("")
totalP2= (totalR1p2 + totalR2p2 + totalR3p2 + totalR4p2)
print(player2, "your total score at the end of round 4 is ", totalP2)
 
 
print("Round 5")
print("")
print(player1)
time.sleep(3)
print("")
print("Dice rolling...")
time.sleep(3)
print("")
from random import *
 
P1R5N1= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 1 variable
P1R5N2= randint(1,6) #this generates a random number for the first dice from 1 to 6 and stores it in playe 1 round 1 number 2 variable
 
if P1R5N1 == P1R5N2: #if the dice have both the same number then both dice will role 2 new numbers
    P1R5N3= randint(1,6)
    P1R5N4= randint(1,6)
    totalR5p1= (P1R5N1 + P1R5N2 + P1R5N3 + P1R5N4) #these 2 new numbers will then be aded to form a total
    print("you rolled a double, you scored a ", P1R5N1, "and a ", P1R5N2, "so we gave you two more rolls you scored a  ", P1R5N3, "and a: ", P1R5N4, "These have been added to your original score your total is: ", totalR5p1)
else:
    totalR5p1= (P1R5N1 + P1R5N2) #if the dice did not role two of the same numbers then the total of the dice are just added up and the total is outputed # it is then stored in the variable totalR1p1 which is the total for the players 1 round this will be later used to add up all of the players totals
    print("you rolled a ", P1R5N1, "and a ",P1R5N2, "your total is ", totalR5p1)
 
print("")
print("")
 
time.sleep(5)
print(player2)
print("")
time.sleep(3)
print("Dice rolling...")
print("")
time.sleep(3)
#this repeates the process for player 2
from random import *
 
P2R5N1= randint(1,6)
P2R5N2= randint(1,6)
 
if P2R5N1 == P2R5N2:
    P2R5N3= randint(1,6)
    P2R5N4= randint(1,6)
    totalR5p2= (P2R5N1 + P2R5N2 + P2R5N3 + P2R5N4)
    print("you rolled a double, you scored a ", P2R5N1, "and a ", P2R5N2, "so we gave you two more rolls you scored a : ", P2R5N3, "and a: ", P2R5N4, "These have been added to your original score your total is ", totalR5p2)
else:
    totalR5p2= (P2R5N1 + P2R5N2)
    print("you rolled a ", P2R5N1, "and a ",P2R5N2, "your total is ", totalR5p2)
print("")
time.sleep(3)
if totalR5p1 > totalR5p2:
    print(player1, "wins round 5")
else:
    print(player2, "wins round 5")
print("")
 
totalP1= (totalR1p1 + totalR2p1 + totalR3p1 + totalR4p1 + totalR5p1)
print(player1, "your total score at the end of round 5 is ", totalP1)
print("")
totalP2= (totalR1p2 + totalR2p2 + totalR3p2 + totalR4p2 + totalR5p2)
print(player2, "your total score at the end of round 5 is ", totalP2)
 
if (totalP1 % 2) == 0:
    totalP1F = totalP1 + 10
    print(player1, "your final score was even so you earned an extra 10 points! your final total is ", totalP1F)
else:
    totalP1F = totalP1 - 5
    print(player1, "your final score was odd so you lost 5 points! your final score is ", totalP1F)
 
print("")
 
if (totalP2 % 2) == 0:
    totalP2F = totalP2 + 10
    print(player2, "your final score was even so you earned an extra 10 points! your final total is ", totalP2F)
else:
    totalP2F = totalP2 - 5
    print(player2, "your final score was odd so you lost 5 points! your final score is ", totalP2F)
 
print("")
 
if totalP1F > totalP2F:
    print(player1, "CONGRATULATIONS YOU WIN!")
else:
    print(player2, "CONGRATULATIONS YOU WIN!")
Reply
#2
What are you trying to accomplish? Do you want a text based console game or do you want a game with a graphical user interface? If you want a text based console game is really difficult to add a button to do the rolling. Maybe you could have hour dice game program launch another program to just do the dice rolling. But that would be a really odd way to program a game. If you want a graphical game with buttons and displays you will not be using print or input.

The layout of your program should change significantly either way. Your program does 2 things. Password validation and dice rolling. These are repeated multiple times. Doing anything multiple times means you should write 1 function to do it, and call the function multiple times. I took a stab at reorganizing your program to use a function for password validation and another for rolling the dice, and it shrunk the program size from 397 lines down to 60.

If I were you I would make this a graphical program. I would get image files for each die face. When you press the roll button the die would display as images instead of numbers. To the right of the dice you could have the results for each round. Underneath the dice the totals for the game. And of course the most important part of all, a Play Again button. This may sound complicated, but it would actually be far simpler than what you have done already.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkInter and Pillow don't display any images in GUIs - program gives errors instead SomeRandomGuy 9 17,824 Oct-29-2019, 02:57 PM
Last Post: SomeRandomGuy
  Worth learning QT to make GUIs for Python? Luke_Drillbrain 4 7,115 Apr-27-2017, 08:36 PM
Last Post: Weave

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020