Python---Life reopening simulator (simplified version)

Column: python
Personal homepage: HaiFan.
Column introduction: This column mainly updates some basic knowledge of python, and also implements some small games, address book, class time management system and so on. Interested friends can pay attention to it.


mind Mapping

insert image description here

foreword

We have already learned the order, selection, loop statement and the use of break and continue in python. Next, we will explain a code case based on these statements.


1. Set the initial properties

I believe everyone has played a small game like a life restart simulator.
In the game, there are generally four attributes

  1. Yan value (face)
  2. constitution (strong)
  3. intelligence (iq)
  4. family background

Here you can agree on the scope of the attributes, and how much the sum of the four attributes cannot exceed.
We agree that the range of attributes is 1-10, and the sum should not exceed 20.

1. Game title

Here you can print the title of the game at the beginning

print("******************************************")
print("                                          ")
print("            人生重开模拟器                   ")
print("                                          ")
print("         花有重开日 人无在少年                ")
print("                                          ")
print("******************************************")

insert image description here

2. Initialization of attributes

These four attributes can be input by the player themselves, which uses the inputfunction mentioned earlier, and through this function, the initialization of the attributes is completed.

 print("请设置初始属性(可用属性值:20)")
 face = int(input("设定颜值(1-10)"))
 iq = int(input("设定智力(1-10)")) 
 strong = int(input("设定体质(1-10)"))   
 home = int(input("设定家境(1-10)"))

In this way, the input of attributes is completed, which is not perfect. We have agreed before that the attributes must not exceed the number, and the sum must not exceed the number. Therefore, we will use the if-elif-elsesentences we have learned to judge the sum of these four attributes. .

    if face < 1 or face > 10:
        print("颜值设置错误")
        continue
    elif iq < 1 or iq > 10:
        print("智力设置错误")
        continue
    elif strong < 1 or strong > 10:
        print("体质设置错误")
        continue
    elif home < 1 or home > 10:
        print("家境设置错误")
        continue
    elif face + iq + strong + home > 20:
        print("总属性超过20")
        continue
    print("初始属性设置完毕")
    print(f"颜值:{
      
      face},体质:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")

Whenever a certain attribute is inconsistent, print the reason on the screen and re-enter the attribute value. If it meets the agreement, print the four attribute values ​​on the screen.


When an error occurs in a certain judgment, the code will directly end the program without letting the player output in a loop. This is because a loop is missing, so we need to set a function that can initialize the property but does not meet the agreement, and let the player Ability to re-enter attribute values. It's time to use itwhile循环

while True:
    print("请设置初始属性(可用属性值:20)")
    face = int(input("设定颜值(1-10)"))
    iq = int(input("设定智力(1-10)"))
    strong = int(input("设定体质(1-10)"))
    home = int(input("设定家境(1-10)"))

    if face < 1 or face > 10:
        print("颜值设置错误")
        continue
    elif iq < 1 or iq > 10:
        print("智力设置错误")
        continue
    elif strong < 1 or strong > 10:
        print("体质设置错误")
        continue
    elif home < 1 or home > 10:
        print("家境设置错误")
        continue
    elif face + iq + strong + home > 20:
        print("总属性超过20")
        continue
    print("初始属性设置完毕")
    print(f"颜值:{
      
      face},体质:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")

When all the judgments meet the agreement, the loop can no longer continue. To end the program, this time breakcomes in handy. If the judgment is satisfied, jump out of the whileloop directly.

breakJust add it to the end of the above code 注意break的位置要缩进.

insert image description here

2. Set gender

Setting gender is an interesting topic. Gender must be random. To set random numbers, a function is used random.randint. This function is a built-in module in python. You must call this module before import random
using random.randint(1,6)it to generate a 1-6. random integer.

  1. If singular: boy
  2. If even number: girl

Boys and girls will encounter different events.

point = random.randint(1,6)
# print(f'随机数为:{point}')
if point % 2 == 1:
    gender = "boy"
    print("你是个男孩.")
else:
    gender = "girl"
    print("你是个女孩.")

insert image description here

3. Set the birth point

First of all, according to the family background, the birth point can be divided into 4 parts

  1. 10 is the first part, with the best family background and the highest bonus
  2. 7-9 the second part, also has some bonuses
  3. 4-6 third part with less bonus
  4. 1-4 The fourth part will deduct attributes

Of course, when you write by yourself, you can write a few more parts to make the game full of more possibilities.


Then, generate random numbers between 1-3, each number represents a situation.

point = random.randint(1,3)
if home == 10:
    #第一部分
    print("你出生在帝都,父母是世界首富.")
    home += 1
    face += 1
    iq += 1
elif home >= 7 and home <=9:
    #第二部分
    if point == 1:
        print("你出生在一线城市,父母是医生.")
        face += 2
    elif point == 2:
        print("你出生在一线城市,父母是资深程序员.")
        iq += 2
    else:
        print("你出生在一线城市,父母是健身教练.")
        strong += 3
        face += 1
        iq -= 1
elif home >= 4 and home <= 6:
    #第三部分
    if point == 1:
        print("你出生在三线城市,父母是企业管理.")
        iq += 1
    elif point == 2:
        print("你出生在三线城市,父母是小学老师.")
        iq += 1
    else:
        print("你出生在三线城市,父母是自己开店做生意.")
        home += 1

else:
    #第四部分
   if point == 1:
       print("你出生在农村,父母是农民.")
       strong += 2
       face -= 1
   elif point == 2:
       print("你出生在山里,父母是猎人.")
       strong += 2
       home -= 1
       face -= 1
   else:
       print("你出生在小镇上,父母是镇长.")
       face += 1
       home += 1

print(f"颜值:{
      
      face},体量:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")

4. Automatically generate life experiences for each age

According to age, life can be divided into four cut-offs

  1. childhood
  2. youth
  3. the prime of life
  4. elderly

Different things happen with each truncation, and there may be some opportunities as well.
For example: childhood truncation, intelligence, appearance, physique, etc. will change, and events can be unfolded here.
Here only write childhood experience:
because it is every year, it can be used for循环展开to splice each year's experience with strings, loop to the end, and output strings. For the occurrence of events, random numbers can be used, according to Events can be generated by numbers, and events can also be triggered according to gender, appearance, physique, etc. Different events have different effects. If you get sick and die, you can directly sys.exit(0)exit the program by using time.sleep(1)it. It can pause the program for one second for easy observation.

for age in range(1,11):
    #把一整年的打印都整理到一个字符串中,在这一年的结尾统一打印
    info = f'你今年{
      
      age}岁'
    #生成一个一到三的随机整数
    point = random.randint(1,3)
    #接下来编写各种事件的代码
    #性别触发的事件
    if gender == 'girl' and home <= 3 and point == 1:
        info += '你的家里人重男轻女思想非常严重,把你丢弃了.'
        print("游戏结束.")
        sys.exit(0)
    #体质触发事件
    elif strong < 6 and point < 3:
        info += '你生了一场病.'
        if home >= 5:
            info += '在父母的照顾下,你康复了.'
            strong += 1
            home -= 1
        else:
            info += '父母没时间管你,你的情况更糟糕了.'
            strong -= 1
    # 颜值触发事件
    elif face <= 4 and age >= 7:
        info += '你长的太丑了,别的小朋友不和你一起玩.'
        if iq > 5:
            info += '你决定用学习填充自己.'
            iq += 1
        else:
            if gender == 'boy':
                info += '你和别的小朋友打架.'
                strong += 1
                iq -= 1
            else:
                info += '你经常被别的小朋友欺负.'
                strong -= 1
    #智商触发的事件
    elif iq < 5:
        info += '你看起来傻乎乎的.'
        if home >= 8 and age >= 6:
            info += '你的父母把你送到更好的学校学习.'
            iq += 1
        elif 4 <= home <= 7:
            if gender == 'boy':
                info += '你的父母鼓励你多运动,争取成为运动健将.'
                strong += 1
            else:
                info += '你的古父母鼓励你多打扮自己.'
                face += 1
        else:
            info += '你的父母经常为此吵架.'
            if point == 1:
                strong -= 1
            elif point == 2:
                iq -= 1
            else:
                pass
    #健康成长
    else:
        info += '你健康成长.'
        if point == 1:
            info += '你看起来更结实了.'
            strong += 1
        elif point == 2:
            info += '你看起来更好看了.'
            face += 1
        else:
            pass
    #打印这一年发生的事情
    print(info)
    print(f"颜值:{
      
      face},体质:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")
    print("---------------------------------------------------------------")
    time.sleep(1)

insert image description here

insert image description here

Summary (with code)

Here, I don't write all four truncations, but only one infancy stage. After reading this article, I believe that everyone has the ability to realize the follow-up content by themselves and make the game more complete.


Note: Don’t forget to call import sys
import time when using sys.exit and time.sleep

"""
                人生重开模拟器
                花有重开日,人无在少年
"""
import random
import sys
import time

print("******************************************")
print("                                          ")
print("            人生重开模拟器                   ")
print("                                          ")
print("         花有重开日 人无在少年                ")
print("                                          ")
print("******************************************")


while True:
    print("请设置初始属性(可用属性值:20)")
    face = int(input("设定颜值(1-10)"))
    iq = int(input("设定智力(1-10)"))
    strong = int(input("设定体质(1-10)"))
    home = int(input("设定家境(1-10)"))

    if face < 1 or face > 10:
        print("颜值设置错误")
        continue
    elif iq < 1 or iq > 10:
        print("智力设置错误")
        continue
    elif strong < 1 or strong > 10:
        print("体质设置错误")
        continue
    elif home < 1 or home > 10:
        print("家境设置错误")
        continue
    elif face + iq + strong + home > 20:
        print("总属性超过20")
        continue
    print("初始属性设置完毕")
    print(f"颜值:{
      
      face},体质:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")
    break



point = random.randint(1,6)

if point % 2 == 1:
    gender = "boy"
    print("你是个男孩.")
else:
    gender = "girl"
    print("你是个女孩.")

point = random.randint(1,3)
if home == 10:
    
    print("你出生在帝都,父母是世界首富.")
    home += 1
    face += 1
    iq += 1
elif home >= 7 and home <=9:
    
    if point == 1:
        print("你出生在一线城市,父母是医生.")
        face += 2
    elif point == 2:
        print("你出生在一线城市,父母是资深程序员.")
        iq += 2
    else:
        print("你出生在一线城市,父母是健身教练.")
        strong += 3
        face += 1
        iq -= 1
elif home >= 4 and home <= 6:
   
    if point == 1:
        print("你出生在三线城市,父母是企业管理.")
        iq += 1
    elif point == 2:
        print("你出生在三线城市,父母是小学老师.")
        iq += 1
    else:
        print("你出生在三线城市,父母是自己开店做生意.")
        home += 1

else:
   
   if point == 1:
       print("你出生在农村,父母是农民.")
       strong += 2
       face -= 1
   elif point == 2:
       print("你出生在山里,父母是猎人.")
       strong += 2
       home -= 1
       face -= 1
   else:
       print("你出生在小镇上,父母是镇长.")
       face += 1
       home += 1

print(f"颜值:{
      
      face},体量:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")


for age in range(1,11):
    
    info = f'你今年{
      
      age}岁'
    
    point = random.randint(1,3)
    
    if gender == 'girl' and home <= 3 and point == 1:
        info += '你的家里人重男轻女思想非常严重,把你丢弃了.'
        print("游戏结束.")
        sys.exit(0)
    
    elif strong < 6 and point < 3:
        info += '你生了一场病.'
        if home >= 5:
            info += '在父母的照顾下,你康复了.'
            strong += 1
            home -= 1
        else:
            info += '父母没时间管你,你的情况更糟糕了.'
            strong -= 1
    
    elif face <= 4 and age >= 7:
        info += '你长的太丑了,别的小朋友不和你一起玩.'
        if iq > 5:
            info += '你决定用学习填充自己.'
            iq += 1
        else:
            if gender == 'boy':
                info += '你和别的小朋友打架.'
                strong += 1
                iq -= 1
            else:
                info += '你经常被别的小朋友欺负.'
                strong -= 1
   
    elif iq < 5:
        info += '你看起来傻乎乎的.'
        if home >= 8 and age >= 6:
            info += '你的父母把你送到更好的学校学习.'
            iq += 1
        elif 4 <= home <= 7:
            if gender == 'boy':
                info += '你的父母鼓励你多运动,争取成为运动健将.'
                strong += 1
            else:
                info += '你的古父母鼓励你多打扮自己.'
                face += 1
        else:
            info += '你的父母经常为此吵架.'
            if point == 1:
                strong -= 1
            elif point == 2:
                iq -= 1
            else:
                pass
    
    else:
        info += '你健康成长.'
        if point == 1:
            info += '你看起来更结实了.'
            strong += 1
        elif point == 2:
            info += '你看起来更好看了.'
            face += 1
        else:
            pass

    print(info)
    print(f"颜值:{
      
      face},体质:{
      
      strong},智力:{
      
      iq},家境:{
      
      home}")
    print("---------------------------------------------------------------")
    time.sleep(1)






Guess you like

Origin blog.csdn.net/weixin_73888239/article/details/128732979