Python-life reopening simulator

Blog homepage: @Jarvis of Wearing Stars and Wearing Moon
Welcome to follow:LikeFavoriteLeave a message
Series of columns: Python column
Please don’t believe that victory is as easy as a dandelion on the hillside, but please believe that there are always some good things in the world that are worth all our efforts, even if we are smashed to pieces!
Come on together, to pursue, to become a better self!

Article directory

  • foreword
  • 1. Set initial properties
  • 2. Set gender
  • 3. Set the birth point
  • 4. Generate life experiences for each age
  • Summarize

Tip: The following is the text of this article, and the following cases are for reference

Foreword

We have already learned the basic knowledge of Python grammar. Young people nowadays are generally under a lot of pressure. Why don’t we use the knowledge we have learned to write a simple life reopening simulator, and let us experience “Lu’s Spring and Autumn”, The games I write, I call the shots. Also, we should not only be the protagonist of the game, but also the protagonist of our own life!

1. Set initial attributes

1.1 Initial Interface Design

print(" + ------------------------------------------- -------------------------- + ")
print("||")
print("| Life restart simulator |")
print("||")
print("| This garbage life doesn't want to stay for a second! |")
print("|reopen now|")
print(" + ---------------------------------------------- ----------------------- + ")

1.2 Set initial properties
The sum of our rules, appearance, physique, intelligence, and family background does not exceed 400, and the value of each item is between 1-100. And the user’s input may cause errors. We use a while loop. If the user enters a wrong input, continue to input, and if the input is correct, break

 while True:
    print("Please set the initial attributes (total available points 200)")
    face = int(input("Set face value (1-100):"))
    strong = int(input("Set physical fitness (1-100):"))
    iq = int(input("Set intelligence (1-100):"))
    home = int(input("Set home environment (1-100):"))
    if face < 1 or face > 100:
        print("The face value setting is wrong!")
        continue
    if strong < 1 or strong > 100:
        print("Physical setting is wrong!")
        continue
    if iq < 1 or iq > 100:
        print("Intelligence setting is wrong!")
        continue
    if home < 1 or home > 100:
        print("Family setting is wrong!")
        continue
    if face + strong + iq + home > 400:
        print("The total number of points exceeds 400!")
        continue
    print(f"Appearance: {face}, constitution: {strong}, intelligence: {iq}, family background: {home}")
    break
        //If the above conditions are not triggered, it means that the user's input is legal
        break

2. Set gender

Principle: Generate a random integer of [1, 6] through random.randint(1, 6), similar to throwing dice. Python is designed according to the rand function of C/C++.

  • If singular, gender is set to boy
  • If it is an even number, the gender is set to girl.

Boys and girls will encounter different events
The random.randint here is a module of Python. In Python, if you want to import other modules, you need to import the name of the module, for example: import random.
Code example:

point = random.randint(1, 6) # roll dice
if point % 2 == 1:
    gender = 'boy'
    print("You are a boy")
else:
    gender = 'girl'
    print("You are a girl")

3. Set birth point

First of all, according to the family (home), divided into four stalls.

  • 10 is the first gear. The bonus is the highest
  • [7, 9] is the second gear. Also has some bonuses
  • [4, 6] is the third gear. The bonus is less
  • [1, 3] is the fourth gear. The attribute will be deducted.

Throw the dice again to generate a random number of [1, 3], which is used to represent each subdivision situation.

The code here is mainly composed of various if else

Code example:

point = random.randint(1, 3) # roll dice
# first file
if home == 10:
    print('You were born in the imperial capital, your parents are high-ranking officials and dignitaries')
    home += 1
    iq + = 1
    face += 1
# second file
elif 7 <= home <= 9:
    if point == 1:
        print('You were born in a big city, your parents are civil servants')
        face += 2
    elif point == 2:
        print('You were born in a big city, your parents are big business executives')
        home += 2
    else:
        print('You were born in a big city, your parents are university professors')
        iq + = 2
# third file
elif 4 <= home <= 6:
    if point == 1:
        print('You were born in a third-tier city, your parents are teachers')
        iq + = 1
    elif point == 2:
        print('You were born in a town, your parents are doctors')
        strong + = 1
    else:
        print("You were born in a small town, your parents are self-employed")
        home += 1
# fourth gear
else:
    if 1 <= point <= 2:
        print('You were born in the village, your parents are hardworking farmers')
        strong + = 1
        face -= 2
    elif 3 <= point <= 4:
        print('You were born in a remote area, your parents were vagrants')
        home -= 1
    else:
        print('You were born in a small town, your parents are at odds')
        strong -= 1

Run example:

4. Generate life experiences for each age

When implementing a game, in many cases, the logic involved in the game itself is not complicated, but some specific data and numerical balance in the game are very complicated!
The family background is divided into several levels, and there are several situations in each level that we need to consider, so it is still a bit complicated.

for age in range(1, 11):
    info = f'You are {age} years old this year,'
    point = random. randint(1, 3)
    # gender trigger event
    if gender == 'girl' and home <= 3 and point == 1:
        info + = 'Your family patriarchal thought is very serious, you have been abandoned!'
        print(info)
        print("Game over!")
        sys. exit(0)
    # Events triggered by constitution
    elif strong < 6 and point != 3:
        info + = 'You are sick,'
        if home >= 5:
            info + = 'Recovered health under the careful care of parents'
            strong + = 1
            home -= 1
        else:
            info + = 'Your parents have no energy to take care of you, your physical condition is getting worse'
            strong -= 1
    # The event triggered by the face value
    elif face < 4 and age >= 7:
        info + = 'Because you are too ugly, other children don't like you,'
        if iq > 5:
            info += 'You decided to fill yourself with learning'
            iq + = 1
        else:
            if gender == 'boy':
                info + = 'You often fight with other children'
                iq -= 1
                strong + = 1
            else:
                info + = 'You are often bullied by other children'
                strong -= 1
    # Events triggered by IQ
    elif iq < 5:
        info += 'You look silly,'
        if home >= 8 and age >= 6:
            info + = 'Your parents sent you to a better school'
        elif 4 <= home <= 7:
            if gender == 'boy':
                info + = 'Your parents encourage you to exercise more and strengthen your physical fitness'
                strong + = 1
            else:
                info + = 'Your parents encourage you to dress up more'
                face += 1
        else:
            info += 'Your parents often fight over this'
            if point == 1:
                strong -= 1
            elif point == 2:
                iq -= 1
    # grow healthy
    else:
        info + = 'You grow up healthily,'
        if point == 1:
        info += 'looks smarter'
            iq + = 1
        elif point == 2:
            info += 'looks better'
            face += 1
        else:
            info += 'looks stronger'
            strong + = 1
    print('-------------------------------------------')
    print(info)
    print(f'strong={strong}, face={face}, iq={iq}, home={home}')
    time. sleep(1)

The overall code running result:

Summary

This article introduces the design of a small game “Life Restart Simulator”, which only uses the simple logic of while loop and if-else, and does not involve the design of functions. It is a small game very suitable for beginners. , I hope everyone will like it and collect it!