In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Python object-oriented to do a Mini Game", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Python object-oriented to make a Mini Game" bar!
We also achieve a Mini Game today. This Mini Game is very famous. I think everyone should have played it. It is tic tac toe, and we can find the game directly by opening chrome search.
Because we use Python to implement it, and we don't know how to make a UI interface, it won't look so good. It's not pretty enough, but the logic is the same. And compared with the Mini Game we did before, the game we are doing today has a very big feature is that it is very suitable for designing AI. We only need to use a very simple algorithm to make a good ai. Of course, let's take it step by step, starting with the simplest game function itself.
Subject
Today's topic is to use Python to write a tic tac toe Mini Game without UI interface.
This time, two sides will be involved in the game, so we need to have the relevant logic to judge the winner or loser of the game. In addition, since two players are involved, we need to design an AI so that we can play games with computers. The final effect should be something like this:
That is, at the beginning of the game, players are supported to choose the two parties involved in the game. Here we first put aside the design of the AI algorithm, we can first make a random choice of mentally retarded AI.
After the game starts, the two sides act alternately, and each execution will output the corresponding specific information on the screen, as well as the current situation of the chessboard.
Knowledge point
object-oriented
Tic tac's game is simple, but it involves a lot of content. You need a chessboard, you need a player, you need to add a player, you need to perform steps, and so on. If this logic is not encapsulated and all written as process-oriented, it will make the code very confusing. Obviously, we need to use object-oriented to abstract and encapsulate this logic in order to simplify coding and thinking.
Our current design is relatively simple, and we don't need to use high-end uses such as inheritance and abstract classes, just use the most basic object-oriented definition of classes. Defining a class in Python is very simple, using the keyword class.
For example:
Class Game: pass
Constructor function
Generally speaking, when we define a class, we need to design a constructor for it, which is the method we call when we create an instance of the class. It will do some initialization work for us. The constructor of the class in Python is _ _ init__, and we can implement it directly in the class.
Class Game: def _ init__ (self): self.board = Board () self.players = [] self.markers = ['oaks,' X'] self.numbers = [1,-1]
For example, in the above example, we have made some initialization settings for the class Game. For example, give it a board, players, and so on.
Class method
Since it is a class, there will naturally be class methods that belong to the class. The definition of a class method is the same as that of an ordinary function, except that it is written inside the class, and the first parameter is self by default. The self keyword is equivalent to this in Java and refers to an instance of a method called at run time.
For example, we implement a method to add players to the class Game:
Class Game: def _ init__ (self): self.board = Board () self.players = [] self.markers = ['oaks,' X'] self.numbers = [1,-1] def add_player (self Player): if player = ='h' or player = = 'human': self.players.append (HumanPlayer ()) elif player = =' r' or player = = 'random': self.players.append (RandomPlayer ())
Let's take a look at the logic inside the add_player method, where we call variables in the class instance through the self keyword. This is why we need to set a self parameter, when we call, we do not need to pay attention to the self parameter, it is automatically populated for us by Python.
Of course, when we define a class method, we can also define a method without a self parameter, but such a method is no longer an instance of the class, but belongs to the class itself. If we want to call it, we can only access it through the class name.
For example:
Class Test: def say (): print ("hello world")
In the class Test, we implement a say method without the self keyword, which is bound to make an error if we call it through an instance of Test. Because when we call a method through an instance, Python automatically passes in the instance as the first parameter for us. This leads to a mismatch between the accepted and transmitted parameters, which causes an error. If we want to call this say method, it should be like this:
Test.say ()
That is, this method no longer belongs to the instance created by the class, but to the class itself. It can be understood as a method modified by the static keyword in the Java class.
Method of the method
The definition of a method in Python is flexible. We can create a method for a class, or we can create another method inside a method. For example, the following example:
Def outer (arg1, arg2): def inner (arg1, arg2): return arg1 + arg2 return inner (arg1, arg2)
Because Python supports functional programming, methods inside methods can also implement functions such as closures, decorators, and so on. But we don't need that high-end usage here, we just need to know the basics. The most basic thing is to define a function inside the function, mainly in this inner function, you can use the variables defined in outer. For example:
Def outer (arg1): arg2 = 10 def inner (arg1): return arg1 + arg2 return inner (arg1)
There is nothing wrong with the above code, but there is one more thing to note. Although the parameters and variables defined in outer can be accessed in inner, it cannot be modified. If you want to modify it, you need to use the nonlocal keyword to declare that this is an outer variable.
For example:
Def outer (arg1): arg2 = 10 def inner (arg1): nonlocal arg2 arg2 + = 1 return arg1 + arg2 return inner (arg1)
By implementing the method within the method, the logic of the internal code of the function can be further simplified, and some complex function functions can be further split and simplified. Understanding this usage is also the basis for later learning about closures, functional programming and other advanced content.
Thank you for your reading, the above is the content of "how to use Python object-oriented to do a Mini Game". After the study of this article, I believe you have a deeper understanding of how to use Python object-oriented to do a Mini Game, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.