The python game code is about 200 lines, the python programming game code

Hello everyone, this article will explain about 200 lines of python small game code. The small game program code python is something that many people want to understand. To figure out the python programming small game code, you need to understand the following things first.

1. Python simple game code How to use Python to make a simple game

1. Python guessing game code:

2. import random #Import random module

3,

4, num = 1

5, yin_num = 0

6, shu_num = 0

7, while num 2:

12, print('Cannot produce a value greater than 2 ')

13. else:

14. data = ['rock', 'scissors', 'cloth']

15. com = random.randint(0, 2)

16. print(Your output is {}, and the computer output is {}.format(data[user], data[com]))

17, if user == com:

18, print('tie')

19, continue

20, elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):

21, print('You won')

22, yin_num += 1

23, else:

24, print('You lost')

25. shu_num += 1

26. num += 1

27. Python digital bomb game code:

28. import random

29. import time

30,

31, bomb = random.randint(1, 99)

32, print(bomb)

33, start = 0

34, end = 99

35, while 1 == 1:

36,

37, people = int(input('please Enter the number between {} and {}: '.format(start, end)))

38, if people > bomb:

39, print('bigger')

40, end = people

41, elif people < bomb:

42 , print('small')

43, start = people

44, else:

45, print('BOOM!!!')

46, break

47, print('waiting for the computer, enter the number between {} and {}: '.format(start, end))

48, time.sleep(1)

49, com = random.randint(start + 1, end - 1)

50, print('computer input: {}'.format(com))

51. if com > bomb:

52. print('big')

53、 end = com

54、 elif com < bomb:

55. print('smaller')

56. start = com

57. else:

58. print('BOOM!!!')

59. break

2. 6 interesting Python codes

First, I selected 6 python codes that I think are worth pondering. I hope it will be helpful to you who are learning python.

1. The class has two methods, one is new, the other is init, what is the difference, which one will be executed first?

The result of the operation is as follows:

Let's look at another example

The result of the operation is as follows:

Here is the official explanation:  The function of init  is to initialize the class instance. The first parameter is self, which represents the object itself, and there may be no return value. new  returns an instance of a new class. The first parameter is cls, which represents the class itself and must have a return value. Obviously, the object can be produced by instantiating the class first. Obviously,  new  is executed first, and then  init  . In fact, as long as  new  returns an instance of the class itself, it will automatically call  init  for initialization. But there is an exception, if  new  returns an instance of another class, it will not call  the init  of the current class . Below we output the types of object a and object b respectively:

It can be seen that a is an object of the test class, and b is an object of object.

2. The object returned by the map function

The first parameter of the map() function is fun, the second parameter is generally a list, and the third parameter can be written as a list or not. The function is to call the function fun sequentially for each element of the list in the list.

Have you noticed that when the elements in b are output for the second time, they are found to be empty. The reason is that the map() function returns an iterator, and yield is used for the returned result. The purpose of this is to save memory. for example:

The execution result is:

If yield is not used here, when the elements in the list are very large, all of them will be loaded into memory, which is a waste of memory and reduces efficiency.

3. Is compile unnecessary in regular expressions?

For example, there is a demand now. For the text China, use regular expressions to match "China" in the label, and the class name of the class is uncertain. There are two methods, the code is as follows:

Why use compile to write two more lines of code here? The reason is that compile compiles the regular expression into an object, speeds it up, and reuses it.

4. [[1,2],[3,4],[5,6]] a line of code to expand the list, get [1,2,3,4,5,6]

5. One line of code inserts the string "->" into the middle of each character in "abcdefg"

It is also recommended to use more () to splice the file path of the operating system.

6. zip function

When the zip() function is operated, it takes one or more sequences (iterable objects) as parameters and returns a list of tuples. Simultaneously pair side-by-side elements of these sequences. The zip() parameter can accept any type of sequence, and it can also have more than two parameters; when the lengths of the incoming parameters are different, zip can automatically intercept based on the shortest sequence length to obtain a tuple.

 

おすすめ

転載: blog.csdn.net/chatgpt001/article/details/129272049