learn python the hard way 习题45-制作一个游戏

小白自学python。

from sys import exit
from random import randint

class Scene(object):
    def enter(self):
        print "it‘s no use"
        exit(1)

class Engine(object):
    def __init__(self,scene_map):
        self.scene_map = scene_map

    def play(self):
        current_scene = self.scene_map.opening_scene()

        while True:
            print "------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

class choosebike(Scene):

    model = {
        ‘bike1‘: ‘you got a merida bike‘,
        ‘bike2‘: ‘you got a giant bike‘,
        ‘bike3‘: ‘congratuations,you got a specialized bike!‘
        }
    def enter(self):
        print """there have three bike
                they are bike1, bike2, bike3.
                if you can choose your bike,
                which will you choose ?
            """
        bike = raw_input(">")

        if bike == ‘bike1‘ or bike  == ‘bike2‘ or bike == ‘bike3‘:
            print choosebike.model[bike]
            print "now, to complete your race with your bike!"
            print "Good luck!"

            return ‘lock‘
        else:
            print"oh no"
            print "please input agian"
            return ‘choosebike‘

class lock(Scene):

    def enter(self):
        print "oh my god! the bike have locked"
        print "ah! it have a note"
        print "it say ‘you should guess the number correctly and you can go‘"
        print "and you should take notice to your time "
        print "you just have 20 times"
        print "please input the number which is a double-digit you guess!"
        number = "%d%d"  %(randint(0,9),randint(0,9))
        guess = raw_input("-->")
        guesses = 0

        return ‘speedorno‘
        while guess != number and guesses < 20:
            guesses += 1
            print "input again"
            guess = raw_input("-->")

        if guess == number:
            print "ahhh you are so samartly"
            print "now ,continue your race"

            return ‘speedorno‘

        else:
            print "\a\a\a\a"
            print "so sorry, your times is uesd out"
            print "you have no time to compelete the race"
            exit(1)

class speedorno(Scene):
    results = [
            ‘you are so cool, you got the no.1!! congratuations!!‘,
            ‘oh it\‘s a nice race , you got the no.2, congratuations‘,
            ‘i belive you will be the no.1 next time! ‘,
            ‘e...it\‘s really a little pity, you are the no.4, but you are very good,you will better next time!‘]
    def enter(self):
        print "you will compelete race immediately,"
        print "and now you want speed, but"
        print "if you speed,your leg may hurted and can‘t go on"
        print "if you not speed, you not speed ,you may win"
        print "but it‘s probability is smaller then speed"
        print "will you speed?"
        spdorno = raw_input(">")

        print "you choose %r " % spdorno

        print speedorno.results[randint(1,4)]
        return ‘finished‘
class Map(object):
    scenes = {
        ‘choosebike‘: choosebike(),
        ‘lock‘: lock(),
        ‘speedorno‘: speedorno()
        }
    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):                               # the function of next_scene
        return Map.scenes.get(scene_name)                    # system function .get -dictionary 

    def opening_scene(self):                     # the function of opening_scene  and use function next scene
        return self.next_scene(self.start_scene)

start = Map(‘choosebike‘)
game = Engine(start)
game.play()

有些地方引用了前面的习题44,虽然只是一个简单的文字游戏,什么意义也没有。

时间: 2024-10-08 19:34:52

learn python the hard way 习题45-制作一个游戏的相关文章

使用CocosSharp制作一个游戏 - CocosSharp中文教程

注:本教程翻译自官方<Walkthrough - Building a game with CocosSharp>,官方教程有很多地方说的不够详细,或者代码不全,导致无法继续,本人在看了GoneBananas项目代码后,对本教程进行了部分修改,但当前只涉及Android方面,iOS因没有环境验证代码,暂未修改. 本人博客地址:http://fengyu.name 原文链接:http://fengyu.name/?cat=game&id=295 相关资源: 离线PDF文档:Downloa

learn python the hard way 习题18~25总结

定义函数和调用函数的语法 定义函数 形式: def functionName(p1,p2): statement other statement 需要注意: 紧跟者函数定义的代码是否使用了4个空格的缩进?不能多,也不能少 函数结束的位置是否取消了缩进? 调用函数 形式:functionname(a1,a2) UTF-8的相关知识 UTF-8 是 Unicode Transformation Format 8 Bits 的简称,是一种编码规定,其目的在于减少编码时候的文本对内存的浪费.这是一种压缩

练习制作一个游戏官网主页(标题导航栏)

html <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <link rel="stylesheet" href="../CSS/神女控headfoot.css"/> <title>神女控官网</title></head><body><div

制作一个游戏编辑器玩玩(4)

加上了地形工具修改面板 加上天空盒后的效果. 加上了鼠标拾取功能,可以正确地选择地形块了. 高度编辑辅助体绘制完成. 终于实现了对地形高度的编辑. 现在还差法线还没有重新计算. 现在的代码是做功能测试的代码, 明天开始要对代码进行一下结构调整.把命令模式加上,要实现返回,重做功能.还有画刷种类等功能等. 原文地址:https://www.cnblogs.com/dodobox/p/12569282.html

笨办法学 Python (Learn Python The Hard Way)

最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 3: 数字和数学计算 习题 4: 变量(variable)和命名 习题 5: 更多的变量和打印 习题 6: 字符串(string)和文本 习题 7: 更多打印 习题 8: 打印,打印 习题 9: 打印,打印,打印 习题 10: 那是什么? 习题 11: 提问 习题 12: 提示别人

Python老王视频习题答案

基础篇2:一切变量都是数据对象的引用sys.getrefcount('test') 查看引用计数变量命名不能以数字开头编码:ascii.unicode.utf-81.阅读str对象的help文档,并解决如下的问题.1.1.有如下字符串. python是动态语言 要求如下[请分别写出脚本]: a=' python是动态语言 '(1.)去掉该字符串下前面所有的空格. print a.lstrip() (2.)去掉该字符串下后面所有的空格. print a.rstrip() (3.)去掉该字符串2边的

Learn Python the Hard Way--Exercise 46

0. 缘起 <Learn Python the Hard Way>Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use th

Learn Python From &#39;Head First Python&#39; [3](2) : Pickle

1.the use of 'with open... as ...' 2.the use of pickle(dump and load) for Step1: the 'with open ... as...' is the short format of 'try...except...finally' for Step2: you can store a list with pickle.dump() and get the content again with pickle.load()

Learn Python From &#39;Head First Python&#39; [2] : Sharing

1.publish 2.update print_lol PS: for Step 1. write the code in to a py file. put the py file into a folder named nester. new a py file named 'setup' and the content as below: setup( name = 'nester', version = '1.0.0', py_modules = ['nester'], author