2D development game assignment: graphic interface design -- guessing digital game

1. Game introduction

In the game, the program randomly generates numbers within 1024, and then lets the players guess. If the guessed numbers are too large or too small, it will prompt, and the program will also count the number of guesses. Using Tkinter to develop digital guessing game

2.python graphic interface design

Python provides multiple libraries of graphical development interfaces, and several common Python GUI libraries are as follows.

Tkinter: Tkinter module (TK interface) is the interface of Python's standard Tk GUI toolkit. Tkinter can be used on most UNIX platforms, as well as Windows and Macintosh systems. Subsequent versions of Tk 8.0 can implement local window style and run well on most platforms.

vxPython: wxPython is an open source software. It is a set of excellent GUI graphics library of Python language, which allows users to easily create a complete and functional GUI user interface.

Jython: Jython programs can integrate seamlessly with Java. In addition to some standard modules, Jython uses Java modules. Jython has almost all modules in standard Python that do not depend on C language. For example, Jython's user interface is rendered using Swing, AWT, or SWT. Jython can be compiled into Java bytecode dynamically or statically.

3. Some code Notes

1. Write the number guessing function

2. Number of guesses

3. Close the window

4. Code reference

import sys
import random
import re


number=random.randint(0,1024)
running =True
num=0
nmaxn=1024
nminn=0

def eBtnClose(event):
    root.destroy()
def eBtnGuess(event):
    global nmaxn
    global nminn
    global num
    global running
    if running:
        val_a = int(entry_a.get())
        if val_a == number:
            labelqval("Congratulations!")
            num += 1
            running = False
            numGuess()
        elif val_a < number:
            if val_a >nminn:
                nminn = val_a
                num += 1
                labelqval("Small, please input" + str(nminn) + "reach" + str(nmaxn) + "Any integer between:")
        else:
            if val_a < nmaxn:
                nmaxn = val_a
                num += 1
                labelqval("Large, please input" + str(nminn) + "reach" + str(nmaxn) + "Any integer between:")
    else:
        labelqval('You got it right.')

def numGuess():
    if num == 1:
        labelqval('One answer')
    elif num < 10:
        labelqval('The answer is correct within ten times. Number of attempts:' + str(num))
    else:
        labelqval('Number of attempts:' + str(num))

def labelqval(vText):
    label_val_q.config(label_val_q, text = vText)

root = tk.Tk(className = "Figure guessing game")
root.geometry("400x90+200+200")


line_a_tip=tk.Frame(root)
label_tip_max=tk.Label(line_a_tip,text=nmaxn)
label_tip_min=tk.Label(line_a_tip,text=nminn)
label_tip_max.pack(side="top",fill="x")
label_tip_min.pack(side="bottom",fill="x")
line_a_tip.pack(side="left",fill="y")
line_question = tk.Frame(root)
label_val_q = tk.Label(line_question, width = "80")
label_val_q.pack(side = "left")
line_question.pack(side = "top",fill = "x")

line_input = tk.Frame(root)
entry_a = tk.Entry(line_input, width = "40")
btnGuess = tk.Button(line_input, text = "guess")
entry_a.pack(side = "left")
entry_a.bind('<Return>', eBtnGuess)
btnGuess.bind('<Button-1>', eBtnGuess)
btnGuess.pack(side = "left")
line_input.pack(side = "top",fill = "x")

line_btn = tk.Frame(root)
btnClose = tk.Button(line_btn, text = "Close")
btnClose.bind('<Button-1>', eBtnClose)
btnClose.pack(side = "left")
line_btn.pack(side="top")

labelqval("Please enter any integer between 0 and 1024:")
entry_a.focus_set()

print(number)
root.mainloop()

5. Game display

Tags: Programming Python Java Unix Windows

Posted on Sat, 21 Mar 2020 10:26:57 -0400 by rimedey