0

我是一个相当新的程序员,我正在学习如何使用 codeskulptor 的 simplegui!我做了一个猜谜游戏,我做了一个有 2 个按钮的画布。1 个按钮设置他们可以猜测的数字范围,从 0 到 100,另一个从 0 到 1000。我想添加第三个按钮,上面写着“新游戏!” 当他们按下它时,一切都会重置,他们可以再次播放,而无需退出框架。您可以在http://www.codeskulptor.org/#user27_FQ1uDycAiykat13_0.py看到我的代码感谢您的帮助!

#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"
4

2 回答 2

0

我也是python的新手:)

我通常定义一个代表游戏本身的函数:

例如

def game
  rand num
  rand num2

  def range1
  def range 2
  def guesses

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

我希望我能帮助你:)

ps 显然我写的代码没有任何意义;)

于 2013-12-17T22:33:05.230 回答
0

每次按下其他按钮时都会开始新游戏,range100并且range1000。该get_input功能仅供您猜测,而不是您想要的新游戏。您希望此New game!按钮与按钮有何不同?如果你想从最后一个设置开始,那么可能是另一个函数,就像range100函数一样,另一个保存最后一个游戏信息的全局变量,并设置最后一个游戏信息,range100以及range1000何时设置这个当前游戏全局变量?

当然,这只是众多方法中的一种。看起来您正在学习 Coursera 课程“Python 交互式编程简介”?如果是这种情况,您应该参考论坛而不是这里寻求帮助。

于 2013-12-17T23:17:57.300 回答