1. Preliminary preparations and precautions The road to learning Python game programming (continuous update)

Note: Opening such a topic is just to better learn how to develop a game with Python, record your own learning process, summarize your learning gains, it is equivalent to taking notes!

One, install Python

This step is very simple . Download the version that suits you directly on the official website: https://www.python.org/ . Python 3.6 or higher is recommended. Of course, downloading this is just to configure the Python environment. Its built-in IDLE text editor is not fully functional, and the experience is extremely poor. It is not recommended to do projects on it, so we have to download Pycharm, which is a Very easy to use software, suitable for Python project development, you can learn about it yourself.

Two, install Pygame

Since we are doing game development, one of the libraries we must use is Pygame. This Python does not come with it. We need to download and install it. How to install it? Open the command line prompt, we install the package we need through this command pip install 包名或模块名, but at this time we find that the download speed is very slow, because the Python package is the default foreign resource, if we download it without using special Means, it is naturally very slow. What should I do? We need to use the domestic mirror source, I generally use the Tsinghua mirror source: https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ , using this, we can directly enter this command:, pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-packagethis time The download is very fast.

Three, programming ideas

It is very important to pay attention to programming specifications, especially in Python, which is more strict.When we declare a variable, the name of the variable should correspond to the meaning of the variable as much as possible. This is a very good habit and should be followed.
Also, since Python is an object-oriented language, and the popular programming method is OOP, it is best to make projects in modules, that is, everything is an object. (Readers can go to understand the difference between these two programming methods, here is a link for reference: https://zhuanlan.zhihu.com/p/75265007 )

Four, Python basics

Before learning this, you must first be familiar with the basic syntax of Python, you don’t need to learn it deeplyFor Python, there are many directions, and there are many that you can't touch. We have to learn the basic chapters. The use of the Pygame library will be mentioned later (the blogger is also learning.).

Five, Python entry programming challenges

The premise of the topic The
given Point class is as follows:

class Point():
    x=0.0
    y=0.0

    #构造函数
    def __init__(self,x,y):
        self.x=x
        self.y=y
        print("Point constructor")

    #输出方法
    def ToString(self):
        return "{X:"+str(self.x)+",Y:"+str(self.y)+"}"

The Circle class is as follows:

class Circle(Point):
    radius=0.0
    def __init__(self,x,y,radius):
        super().__init__(x,y)
        self.radius=radius
        print("Circle constructor")
    def ToString(self):
        return super().ToString()+",{radius="+str(self.radius)+"}"

The Size class is as follows:

class Size():
    width=0.0
    height=0.9

    def __init__(self,width,height):
        self.width=width
        self.height=height
        print("Size constructor")

    def ToString(self):
        return "{widht="+str(self.width)+",height="+str(self.height)+"}"

The Rectangle class is as follows:

class Rectangle(Point,Size):
    def __init__(self,x,y,width,height):
        Point.__init__(self,x,y)
        Size.__init__(self,widht,height)
        print("Rectangle constructor")
    def ToString(self):
        return Point.ToString()+","+Size.ToString()

1. Open the GemotryDemo.py program, and create a new class integrated from Point, named Elipse. It has a horizontal radius and a vertical radius instead of just one radius like Circle.

2. Add a method named CalcArea() to the Rectangle class, which returns the area of ​​the Rectangle. The calculation formula is: Area=Width*Height. Test the method to make sure it works.

3. Add a new method named CalcCircum() to the Circle class, which returns the circumference of the circle. The calculation formula is Circumference=2*Pi*Radius(Pi=3.14159). Test the method to make sure it works.

1. Reference answer

class Point():
    x=0.0
    y=0.0

    #构造函数
    def __init__(self,x,y):
        self.x=x
        self.y=y
        print("Point constructor")

    #输出方法
    def ToString(self):
        return "{X:"+str(self.x)+",Y:"+str(self.y)+"}"
class Elipse(Point):
    x_radius=0.0
    y_radius=0.0

    #构造函数,这里我们要调用基类的构造函数。
    def __init__(self,x,y,x_radius,y_radius):
        super().__init__(x,y)
        self.x_radius=x_radius
        self.y_radius=y_radius
        print("Elipse constructor")
    def ToString(self):
        return super().ToString()+",{x_radius="+str(self.x_radius)+"}"+",{y_radius="+str(self.y_radius) + "}"
p = Point(10,20)
print(p.ToString())

e=Elipse(100,100,50,40)
print(e.ToString())

2. Reference answer

class Rectangle(Point,Size):
    def __init__(self,x,y,width,height):
        Point.__init__(self,x,y)
        Size.__init__(self,width,height)
        print("Rectangle constructor")
    def ToString(self):
        return Point.ToString(self)+","+Size.ToString(self)
    def CalcArea(self):
        return self.width*self.height
r=Rectangle(200,250,40,50)
print(r.ToString())
print(r.CalcArea())

Output
Insert picture description here

3. Reference answer

class Circle(Point):
    radius=0.0
    def __init__(self,x,y,radius):
        super().__init__(x,y)
        self.radius=radius
        print("Circle constructor")
    def ToString(self):
        return super().ToString()+",{radius="+str(self.radius)+"}"
    def CalcCircum(self):
        Pi=3.14159
        return 2*Pi*self.radius
c=Circle(100,100,50)
print(c.ToString())
print(c.CalcCircum())

Output
Insert picture description here

Guess you like

Origin blog.csdn.net/hzf0701/article/details/109071533