当前位置:网站首页>Writing word adventure games with Mulan programming language (first eight chapters)

Writing word adventure games with Mulan programming language (first eight chapters)

2020-12-06 06:15:28 Wu Ji 2020

Just got it 《Make Your Own Python Text Adventure: A Guide to Learning Programming》 A Book , We plan to rewrite its routine in Mulan language and change it into Chinese identifier . On the one hand, look at The reappearance of Mulan function Is it enough for beginners , By the way, to the previous summary Mulan and Python Grammar comparison of To supplement . The following code is in Mulan release version 0.0.15.0 Run through .

The book introduces user input in three four five six chapters 、 Conditional statements 、 function 、 list , Go straight to Chapter Seven : loop , Because routines contain the techniques of the previous chapters :

func  play  {
   stock  = [' dagger ',' Gold coin (5)',' Bread stick ']
  println(" I Am Alive !")
  loop {
     Input  =  Get orders ()
    if ['n', 'N'].__contains__( Input ) {
      println(" To the North !")
    } elif ['s', 'S'].__contains__( Input ) {
      println(" Southward !")
    } elif ['e', 'E'].__contains__( Input ) {
      println(" East !")
    } elif ['w', 'W'].__contains__( Input ) {
      println(" To the West !")
    } elif ['i', 'I'].__contains__( Input ) {
      println(" stock :")
      for  guy  in  stock  {
        println('* ' + str( guy ))
      }
    } else {
      println(" Don't understand, !")
    }
  }
}

func  Get orders  {
  return input(' action :')
}


 play ()

The bigger discomfort is not supporting Python Elements in list The judgment grammar of , Here we use __contains__ Instead of , It remains to be confirmed whether there is a simpler Mulan grammar .

It can be used .. and ..< Instead of Python Medium range:

for  Count  in 1..<11 {
     Factor  = []
    for  Divisor  in 1.. Count  {
         Factor .append( Divisor ) if  Count  %  Divisor  == 0
    }
    println("` Count ` The factor of :" + str( Factor ))
}

Chapter viii. : Types and individuals

type  people  {
    func $ people ( name ,  Age ,  Love to eat ) {
        $ name  =  name 
        $ Age  =  Age 
        $ Love to eat  =  Love to eat 
    }

    func $ Year of birth  {
        return 2015 - $ Age 
    }

    func $__str__ {
        return " full name :{}  Age :{}  Love to eat :{}".format(
            $ name , $ Age , $ Love to eat [0])
    }
}

 each one  = [ people ("Ed", 11, ["hotdogs", "jawbreakers"])
    ,  people ("Edd", 11, ["broccoli"])
    ,  people ("Eddy", 12, ["chunky puffs", "jawbreakers"])]

 Age and  = 0
 The year and  = 0
for  Sb.  in  each one  {
     Age and  =  Age and  +  Sb. . Age 
     The year and  =  The year and  +  Sb. . Year of birth ()
}

println(" Average age :" + str(( Age and  + 0.0) / len( each one )))
println(" Average year of birth :" + str(int( The year and  / len( each one ))))

println(" People in the census :")
for  Sb.  in  each one  {
    println( Sb. )
}

Chapter nine 、 abnormal , It needs Magnolia try...catch grammar , Going to the gitee The item reproduces the syntax Continue with this series .

版权声明
本文为[Wu Ji 2020]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012060612058449.html