Can python develop games

Python can write games, but it is not suitable. Let's analyze the specific reasons below.

Can a car be built with a hammer? No one can say no? There have indeed been cars in history that were built with a hammer. But in general, is it more appropriate to use industrial robots?

There are two relatively large games that use Python, one is "EVE" and the other is "Civilization". But this is just an example and has no broad significance.
Generally speaking, there are two languages used to make games. One is C++. . One is C#. .

In theory, Python is not only not suitable for games, but as long as large programs are not suitable. Only suitable for writing relatively small things, such as a calculator, a crawler, etc.

There are two main aspects, one is slow speed, and the other is grammatical defects.

Maybe you must think that Python's syntax is clean and elegant, why is it flawed? But think about it carefully, why are other languages not so clean? Not so elegant? Obviously a=123 can be directly written, why must it be written as int a=123; Is it because the designers of other languages have obsessive-compulsive disorder? The reason is simple, there are gains and losses.
If the data type is only strings and numbers, omitting the process of declaring variables is certainly not a problem. But as long as the logic becomes complicated, the situation is completely different. . . In the game, you write it in C# or C++, it will probably be like this.

Skill a=xxxx;
Weapon b=xxxx;
Role c=xxxx;
Potion d=xxxx;
Music e=xxxx;

What about Python? Probably like this

a=xxxx
b=xxxx
c=xxxx
d=xxxx

If you have very little code, Python is obviously more convenient. But if you create hundreds of objects, the code exceeds 10,000 lines. . . When I wrote a few thousand lines, I encountered an object called x. Do you still know what it is? Is it a weapon? Or a bottle of potion? Still a picture? A piece of audio? A light? A house?
Don't think that 10,000 lines of code are a lot. . . . 10,000 lines can't even finish "Fight Landlord". .

The feeling of writing large programs in Python is that on your first day, you only wrote 50 lines of code and created 3 classes and 5 objects. You will feel so cool, this is definitely the best language in the world. . . The next day, when you created 2 more classes and 5 objects, you felt a little dizzy. On the third day, after creating two more classes, you will find that you have to read the comments very carefully, otherwise you won't write them. On the fourth day, you have been reading the notes all day. . . .

This is the inferior nature of dynamic languages. At the beginning, the amount of code was small, no shortcomings were seen, all kinds of trouble-free, all kinds of cool. The more code, the more confused the brain. Generally speaking over 500 lines, the efficiency will be overtaken by languages such as JAVA and C#. . 1000 lines, you must add various comments to understand. . 2000 lines, there are more comments than code. . 5000 lines, comments are completely useless, I can't understand my code at all, I need to prepare to discard the pit.

To sum up, it's not that python can't develop games, but it's not appropriate. Each language has its own advantages and disadvantages. Perhaps the disadvantage of python is the development of games.

Python game example supplement:

Dealing game

  1. game introduction

Four players play cards, and the computer randomly distributes 52 cards (not suitable for the big and small kings) to the four players, and displays the cards of each player on the screen.

  1. Object-oriented programming

  2. Programming steps

Design category, the licensing program has three categories: Card category, Hand category and Poke category.

Card class: Card class represents a card, among which, the FaceNum field refers to the card numbers 1-13, the Suit field refers to the suit color, "plum" for clubs, "square" for squares, and "red" for hearts. "Black" means spades.

Hand class: The Hand class represents a hand (a card held by a player), which can be considered as a card in a player's hand. The cards list variable stores the cards in the player's hand. You can add cards, clear the cards in your hand, and give a card to another player.

Poke class: Poke class represents a deck of cards. We can regard a deck of cards as a player with 52 cards, so we inherit the Hand class. Since the cards list variable needs to store 52 cards, and the cards are dealt and shuffled, the following methods are added.

Main program: The main program is relatively simple. Because there are four players, the players list is generated to store the initialized four players. Generate an object instance poke1 of a deck of cards, call the populate() method to generate a deck of 52 cards, call the huffle() method to shuffle the order, and call the deal(players, 13) method to give each player 13 The cards, and finally all the cards of the four players are shown.

classCard():""" A playing card. """
 RANKS=["A","2","3","4","5","6","7","8","9","10","J","Q","K"] #Card number 1-13
 SUITS=["plum","square","red","black"]
# Plums are clubs, squares are square diamonds, red is red hearts, and black is spades

 def __init__(self,rank,suit,face_up=True):
 self.rank=rank    #Refers to the card number 1-13
 self.suit=suit    #suit refers to suit
 self.is_face_up=face_up #Whether to display the front of the card, True is the front, False is the back of the card

 def __str__(self): #print()if self.is_face_up:
 rep=self.suit+self.rank #+" "+str(self.pic_order())else:
 rep="XX"return rep

 def flip(self):    #Flop method
 self.is_face_up=not self.is_face_up

 def pic_order(self):   #Sequence number
 if self.rank=="A":
 FaceNum=1
 elif self.rank=="J":
 FaceNum=11
 elif self.rank=="Q":
 FaceNum=12
 elif self.rank=="K":
 FaceNum=13else:
 FaceNum=int(self.rank)if self.suit=="plum":
 Suit=1
 elif self.suit=="square":
 Suit=2
 elif self.suit=="red":
 Suit=3else:
 Suit=4return(Suit-1)*13+FaceNum
classHand():""" A hand of playing cards. """
 def __init__(self):
 self.cards=[]
 def __str__(self):if self.cards:
 rep=""for card in self.cards:
 rep+=str(card)+"\t"else:
 rep="Unlicensed"return rep
 def clear(self):
 self.cards=[]
 def add(self,card):
 self.cards.append(card)
 def give(self,card,other_hand):
 self.cards.remove(card)
 other_hand.add(card)classPoke(Hand):""" A deck of playing cards. """
 def populate(self):   #Generate a deck of cards
 for suit in Card.SUITS:for rank in Card.RANKS:
 self.add(Card(rank,suit))
 def shuffle(self):   #Shuffle
 import random
 random.shuffle(self.cards) #Shuffle the order of the cards
 def deal(self,hands,per_hand=13):for rounds inrange(per_hand):for hand in hands:

 top_card=self.cards[0]
 self.cards.remove(top_card)
 hand.add(top_card)if __name__=="__main__":print("This is a module with classed for playing cards.")
 # Four players
 players=[Hand(),Hand(),Hand(),Hand()]
 poke1=Poke()
 poke1.populate()   #Generate a deck of cards
 poke1.shuffle()    #Shuffle
 poke1.deal(players,13)  #13 cards to each player
 # Show the cards of four players
 n=1for hand in players:print("Poker player",n,end=":")print(hand)
 n=n+1input("\nPress the enter key to exit.")

This is the end of this article about can games be developed with python. For more related content, can you write games with python, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

Can python develop games
What can Python do
Python implements minesweeper games
What can python crawlers crawl
OpenCV Python implements puzzle games
What databases can python use
What can python collections do
Python can also analyze official accounts
Can python become a mainstream language?
Can Python implement the structure of the stack?
Use Python to make airplane war games
Can I learn python without programming foundation
Can variable types be declared in python
What kind of work can python do