初学python, 自己编的掷筛子小游戏

来源:互联网 发布:xp仿mac苹果系统 编辑:程序博客网 时间:2024/04/27 15:48

掷筛子游戏的基本规则是:开始都有100金币,掷出1,2,3的扣除相应的金币,掷出4,5,6加上相应的金币。如果前两次都掷出1的话游戏直接算输,前两次都掷出6的话游戏直接算胜利。除此之外,游戏金币扣光算输,游戏金币超过200算赢,一共最多十次投掷机会。

游戏运行结果如图:



下面是基本代码:

# -*- coding: utf-8 -*-import randomimport sysdef start():    print "Welcome to the exciting game of \'Dice or Dies\'!"    print "Here, nothings counts but luck."    print "Now, let's play!"    print "Everyone has %d golds at first." % 100def Dice_rolling():    point = random.randint(1, 6)    print "the point is: ", point    return pointdef Dice_one(golds):    print "So bad, your golds will be less 40."    golds -=40    print "Your golds now are: ", golds    return goldsdef Dice_two(golds):    print "Not so lucky, your golds will be less 30."    golds -= 30    print "Your golds now are: ", golds    return goldsdef Dice_three(golds):    print "Ok, your golds will be less 10."    golds -= 10    print "Your golds now are: ", golds    return goldsdef Dice_four(golds):    print "Good, your gold will be more 10."    golds += 10    print "Your golds now are: ", golds    return goldsdef Dice_five(golds):    print "Great, your gold will be more 30."    golds += 30    print "Your golds now are: ", golds    return goldsdef Dice_six(golds):    print "So lucky! your golds will be more 50."    golds += 50    print "Your golds now are: ", golds    return goldsdef Decide_rule(point, golds):    if point == 1:        golds = Dice_one(golds)    elif point == 2:        golds = Dice_two(golds)    elif point == 3:        golds = Dice_three(golds)    elif point == 4:        golds = Dice_four(golds)    elif point == 5:        golds = Dice_five(golds)    else:        golds = Dice_six(golds)    return goldsstart()golds = 100print "第1次投掷:".decode('utf-8').encode('gbk')number = Dice_rolling()if number == 1:    current_golds = Decide_rule(number, golds)    print "You will be out of the game, if your next point is still 1."    print "第2次投掷: ".decode('utf-8').encode('gbk')    number_sec = Dice_rolling()    if number_sec == 1:        print "Game over!"        exit(0)    else:        current_golds = Decide_rule(number_sec, current_golds)elif number == 6:    current_golds = Decide_rule(number, golds)    print "You will win, if your next point is still 6."    print "第2次投掷: ".decode('utf-8').encode('gbk')    number_sec = Dice_rolling()    if number_sec == 6:        print "You win!"        exit(0)    else:        current_golds = Decide_rule(number_sec, current_golds)else:    current_golds = Decide_rule(number, golds)    print "第2次投掷: ".decode('utf-8').encode('gbk')    number_sec = Dice_rolling()    current_golds = Decide_rule(number_sec, current_golds)count_rolling = 3while count_rolling < 11:    print "第 %d 次投掷: ".decode('utf-8').encode('gbk') % count_rolling    number = Dice_rolling()    current_golds = Decide_rule(number, current_golds)    if current_golds < 0:        print "Game over!"        exit(0)    elif current_golds > 200:        print "You win!"        print "Totally, you throwed %d times." % count_rolling        exit(0)    else:        count_rolling += 1


                                             
0 0