One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the first player answers either “Higher”, “Lower” or “Correct!” depending on whether the secret number is higher, lower or equal to the guess. In this project, you will build a simple interactive program in Python where the computer will take the role of the first player while you play as the second player.
You will interact with your program using an input field and several buttons. For this project, we will ignore the canvas and print the computer's responses in the console. Building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well. Focusing on getting the logic of the program correct before trying to make it display the information in some “nice” way on the canvas usually saves lots of time since debugging logic errors in graphical output can be tricky.
# template for "Guess the number" mini-project # input will come from buttons and an input field # all output for the game will be printed in the console import simplegui, random, math # initialize global variables used in your code frame_width = 100 frame_height = 200 button_width = 100 input_width = 100 num_range_low = 0 num_range_high = 100 player_num = 0 player_guess_times = 0 # helper function to start and restart the game def new_game(): global player_num, player_guess_times player_num = random.randrange(num_range_low, num_range_high) player_guess_times = int(math.ceil(math.log(num_range_high - num_range_low + 1, 2))) print '' print 'New game start, number range is [', num_range_low, ',', num_range_high, ').' print 'You have ', player_guess_times, ' chances to guess.' # define event handlers for control panel def range100(): # button that changes range to range [0,100) and restarts global num_range_high num_range_high = 100 new_game() def range1000(): # button that changes range to range [0,1000) and restarts global num_range_high num_range_high = 1000 new_game() def input_guess(guess): # main game logic goes here global player_guess_times player_guess_times -= 1 guess_num = int(guess) print 'You guessed: ', guess_num, '. ', print 'Your remained ', player_guess_times, 'guess chances.', if guess_num == player_num: print 'Correct!' new_game() elif guess_num > player_num: print 'Higher' else: print 'Lower' if player_guess_times <= 0: print 'You lose!' new_game() # create frame frame = simplegui.create_frame('Guess the number', frame_width, frame_height) # register event handlers for control elements frame.add_button('range100', range100, button_width) frame.add_button('range1000', range1000, button_width) frame.add_input('guess number', input_guess, input_width) # call new_game and start frame new_game() frame.start() # always remember to check your completed program against the grading rubric
演示地址: http://www.codeskulptor.org/#user29_oKHzERABsF_0.py
文/闫鑫原创