这是第2次学习《Pygame游戏编程入门》这本书了,学习过程中的心得体会在这里记录一下。
第1章 挑战
- 要点:
- python中如何使用圆周率常量?答:
import math
,math.pi
- python中如何使浮点数字保留2位小数?答:
round( number, num_of_digits )
思路:首先创建 Point
类,表示一个点,包含两个参数 x
和 y
,表示该点的坐标。然后创建 Ellipse
类,表示一个椭圆,包含椭圆中心坐标 x
和 y
,长半轴 a
和短半轴 b
。最后,在实例化两个类之后,利用 to_string()
方法打印点对象和椭圆对象的信息。
1.py
代码如下:
import math
class Point():
def __init__( self, x, y ):
self.x = x
self.y = y
def to_string( self ):
return '坐标:({}, {})'.format( self.x, self.y )
class Ellipse( Point ):
def __init__( self, x, y, a, b ):
super().__init__( x, y )
self.a = a
self.b = b
def to_string( self ):
return '{} [ a = {}, b = {}, 周长 = {},面积 = {} ]'.format( super().to_string(), self.a, self.b, self.get_circumference(), self.get_area() )
def get_circumference( self ):
return round( 2 * math.pi * self.b + 4 * ( self.a - self.b ), 4 )
def get_area( self ):
return round( math.pi * self.a * self.b, 4 )
p = Point( 10, 10 )
e = Ellipse( 100, 100, 20, 5 )
print( p.to_string() )
print( e.to_string() )
运行结果:
python 1.py
坐标:(10, 10)
坐标:(100, 100) [ a = 20, b = 5, 周长 = 91.4159,面积 = 314.1593 ]
思路:沿用上一题 Point
类,表示一个点,包含两个参数 x
和 y
,表示该点的坐标。然后创建 Size
类,表示尺寸,包含宽 w
和高 h
。然后创建 Rectangle
类,表示一个矩形,包含矩形中心点的坐标(继承自 Point
),和矩形的尺寸(继承自 Size
)。最后,实例化3个类,利用 to_string()
方法打印各个对象的信息。
2.py
代码如下:
class Point():
def __init__( self, x, y ):
self.x = x
self.y = y
def to_string( self ):
return '坐标:( {}, {} )'.format( self.x, self.y )
class Size():
def __init__( self, w, h ):
self.w = w
self.h = h
def to_string( self ):
return '[ 宽 = {}, 高 = {} ]'.format( self.w, self.h )
class Rectangle( Point, Size ):
def __init__( self, x, y, w, h ):
Point.__init__( self, x, y )
Size.__init__( self, w, h )
def to_string(self):
return Point.to_string( self ) + ', ' + Size.to_string( self )
def get_circumference(self):
return ( self.w + self.h ) * 2
def get_area(self):
return self.w * self.h
p = Point( 1, 1 )
s = Size( 5, 2 )
r = Rectangle( 2, 2, 10, 3 )
print( p.to_string() )
print( s.to_string() )
print( r.to_string() )
运行结果:
python 2.py
坐标:( 1, 1 )
[ 宽 = 5, 高 = 2 ]
坐标:( 2, 2 ), [ 宽 = 10, 高 = 3 ]
思路:沿用上一题的 Point
类。然后创建 Circle
类,表示一个圆,参数包含圆心坐标(继承自 Point
)和半径 r
。然后,在该类中添加两个方法,分别计算圆周长和面积,并在打印时调用。最后,实例化2个类,利用 to_string()
方法打印各个对象的信息。
3.py
代码如下:
import math
class Point():
def __init__( self, x, y ):
self.x = x
self.y = y
def to_string( self ):
return '坐标:( {}, {} )'.format( self.x, self.y )
class Circle( Point ):
def __init__( self, x, y, r ):
super().__init__( x, y )
self.r = r
def to_string( self ):
return '{} [ 半径 = {},周长 = {},面积 = {} ]'.format( super().to_string(), self.r, self.get_circumference(), self.get_area() )
def get_circumference( self ):
return round( 2 * math.pi * self.r, 4 )
def get_area( self ):
return round( math.pi * self.r * self.r, 4 )
p = Point( 10, 10 )
c = Circle( 100, 100, 20 )
print( p.to_string() )
print( c.to_string() )
运行结果:
python 3.py
坐标:( 10, 10 )
坐标:( 100, 100 ) [ 半径 = 20,周长 = 125.6637,面积 = 1256.6371 ]
Original: https://blog.csdn.net/quanet_033/article/details/126889458
Author: 下唐人
Title: 《Pygame游戏编程入门》学习——第1章 使用类的Python
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783697/
转载文章受原作者版权保护。转载请注明原作者出处!