Home  >  Article  >  Backend Development  >  I can play this game for a whole day with a single line of Python code!

I can play this game for a whole day with a single line of Python code!

WBOY
WBOYforward
2023-04-27 22:22:051902browse

I can play this game for a whole day with a single line of Python code!Hello everyone, I am a rookie!

Python has long occupied the top three rankings of programming languages, and recently it has been ranked first in the weapons spectrum for several consecutive years. Python language is simple and powerful, and more and more friends are starting to learn Python. Even K12 students are starting to learn Python programming. When newbies get started, fun is actually the most important thing. So what kind of tricks can be played with a line of Python code, and what interesting games can be played with just one line of code. It is also an eye-opener for the editor, let’s take a look.

1. One line of code guessing number game

I believe many friends have played the number guessing game. This is an introductory programming example that is claimed to have been played by 10,000 people. I have seen many ways to write, but it can be done with one line of code. This is the first time I have seen it. It opened my eyes and I quickly took a look at the effect:

I can play this game for a whole day with a single line of Python code!

(Stare for 5 seconds)

The design of the program is super simple. Initialize a random integer from 0 to 99, and then make a judgment. If it is large, enter H, if it is small, enter L. If it exceeds 6 times, exit

2. A maze game with one line of code

Maze game, I don’t know if you have played it before. It was very popular in the past. There were maze designs of various sizes, especially the web version of the maze in the early years. It is very popular, but Python can do it with one line of code, which is really cool!

I can play this game for a whole day with a single line of Python code!

Let’s take a look at the effect:

I can play this game for a whole day with a single line of Python code!

(Start at it for 5 seconds)

The code is very It's very simple. It uses short single lines to create an infinite maze. Although it's not as good as a real maze, it still feels a bit like it. The algorithm is actually easy to understand. Use a while to loop infinitely. The highlight of the entire program design is that although the import statement moves within the loop, Python does not need to re-import it every time. The code picks a random character from one of the maze drawing characters and flush prints it.

3. Slot machines in the arcade

Do you still remember your youthful days? After school, you would go to the arcade to play masturbation games, Contra games, and slot machines. fruit machine. Put a few coins in, pull the lever or press the start button, and when several 777 or several identical fruits appear on the screen, you will be congratulated on winning and spit out a lot of coins. Doesn’t it bring back a lot of childhood memories for you? Now Python can help you do it, and it’s just one line of code!

I can play this game for a whole day with a single line of Python code!

Run and see the effect:

I can play this game for a whole day with a single line of Python code!

(Start for 5 seconds)

Design 1 String a string such as '7♪♫♣♠♦♥◄☼☽', and then use a random function to randomly select a character from it, and then let it perform a loop of more than 30,000 times. Such a large loop allows the string to form A rolling animation effect.

4. Mini Golf

In my mind, golf games are all 3D somatosensory games. For example, the popular ones are Microsoft’s XBox and Nintendo’s Wii, which include golf. A somatosensory game, the moment you swing, the ball draws a perfect arc in the air and goes into the hole! Such a picture can be done with Python, and it can be completed with one line of code.

I can play this game for a whole day with a single line of Python code!

Running effect:

I can play this game for a whole day with a single line of Python code!

(Start for 5 seconds)

The effect of the program is definitely It can’t be compared with somatosensory games, but it’s still quite enjoyable! When designing, you need to enter a tuple, such as (0.9,130). The first number represents the angle in radians, and the second number is the speed. What is more clever is to use eval to unpack, and then use a complex physical formula to calculate the flight trajectory of the ball. The idea is still very clever.

Note: The angle must basically be set before 0.7-0.9, and the speed must be above 100 to show a more perfect curve.

I can play this game for a whole day with a single line of Python code!

(An easter egg in Python)

Okay, these games with one line of Python code will make your imagination expand and you will be amazed! The above writing methods all use several advanced techniques in Python, such as anonymous functions, lists, and reflection! Anyway, my interest in learning Python has greatly increased. It turns out that Python can be played like this! You can adapt these codes, expand its functions by adding your own creativity and understanding, and discover the fun inside. I believe you can code like this for a day.

The above is the detailed content of I can play this game for a whole day with a single line of Python code!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete