Re-running Simplegui game

1.1k Views Asked by At

I'm a fairly new programmer, and i'm learning how to use simplegui from codeskulptor! I made a guessing game, and i made a canvas with 2 buttons. 1 button sets the range of numbers that they can guess from 0 to 100, and the other from 0 to 1000. I would like to add a third button that says "New Game!" and when they push it, everything resets and they can play it again without having to exit out of the frame. You can see my code at http://www.codeskulptor.org/#user27_FQ1uDycAiykat13_0.py Thanks for the help!

#imports 
import math
import simplegui
import random

#global variables
secret_num = random.randint(0, 100)
max_guesses = 7

#define event handlers for control panel

def range100():
    global secret_num, max_guesses
    secret_num = random.randint(0, 100)
    max_guesses = 7
    print "You've selected to guess a number between 0 and 100! Good luck!\n"
    #button that changes range to [0,100)

def range1000():
    global secret_num, max_guesses
    secret_num = random.randint(0, 1000)
    max_guesses = 10
    print "You've selected to guess a number between 0 and 1000! Good luck!\n"
#button that changes range to 0,1000

def get_input(guess):
    global count, max_guesses
    max_guesses -= 1
    num_guessed = int(guess)
    if num_guessed == secret_num:
        print guess + " IS CORRECT! YOU WIN!\n"
    elif max_guesses > 0:
        if num_guessed > secret_num:
            print "You guessed", num_guessed, "."
            print "You need to guess LOWER! You have", max_guesses, "guesses          remaining!\n"
        elif num_guessed < secret_num:
            print "You guessed", num_guessed, "."
            print "You need to guess HIGHER! You have", max_guesses, "guesses remaining!\n"
    else:
        print "YOU LOSE, LOSER! TRY AGAIN FOR BETTER LUCK!"
        print "The correct answer was", secret_num

#create a frame
f = simplegui.create_frame('Guessing Game!', 250, 250)
f.set_canvas_background('Cyan')

#create buttons
    f.add_button("Guess between (0,100)", range100)
    f.add_button("Guess between (0,1000)", range1000)
    f.add_button("New game!", get_input)
    f.add_input("Enter a guess!", get_input, 200)
    f.start()
    print "Welcome to Matt Schaefer's guessing game!"
    print "Please select the number range you wish to guess, and start your game!\n"
2

There are 2 best solutions below

0
On

I'm a novice with python too :)

I usually define a function that represents the game itself:

for example

def game
  rand num
  rand num2

  def range1
  def range 2
  def guesses

  button 1
  button2
  button3 (game) prints "begin game again"

I hope I helped you :)

p.s. obviously the code I wrote doesn't mean anything ;)

0
On

A new game starts every time you hit the other buttons, range100 and range1000. The get_input function is for your guess and isn't what you want for your new game. What would you like this New game! button to do differently than the buttons? If you want to start with last settings then maybe another function just like the range100 function, another global variable that holds the last game info, and set the last game info in range100 and range1000 when you set this current game global variables?

Of course this is only one of a ton of ways to do it. It looks like you are taking the Coursera course "An Introduction to Interactive Programming in Python"? You should refer to the forum for help rather than here if that is the case.