• 如何在 PyGame 中初始化所有导入的模块


    14天学习训练营导师课程:
    李宁《Python Pygame游戏开发入门与实战》
    李宁《计算机视觉OpenCV Python项目实战》1
    李宁《计算机视觉OpenCV Python项目实战》2
    李宁《计算机视觉OpenCV Python项目实战》3

    如何在 PyGame 中初始化所有导入的模块?

    PyGame 是专为游戏开发而设计的 Python 库。PyGame 建立在SDL库之上,因此它提供了用 Python 开发游戏的全部功能。Pygame 有很多模块来执行它的操作,在使用这些模块之前,必须先对它们进行初始化。所有模块都可以单独初始化或一次初始化一个。这篇文章描述了如何一次初始化所有导入的模块。

    使用的方法:

    • pygame.init() – 初始化所有模块。它不带任何参数并返回一个元组 (numpass,numfail),它指示成功初始化的模块数和失败的模块数。

    • pygame.get_init() – 此方法用于检查 pygame 模块是否已初始化。

    **示例 1:**此示例初始化所有 pygame 模块并打印成功初始化的模块数。

    # importing the library
    import pygame
     
    # initializing all the imported
    # pygame modules
    (numpass,numfail) = pygame.init()
     
    # printing the number of modules
    # initialized successfully
    print('Number of modules initialized successfully:',
          numpass)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    image-20221120225716960

    **示例 2:**此示例使用 pygame.get_init() 函数来检查 pygame 模块是否已初始化。

    # importing the library
    import pygame
    
    # initializing the modules
    pygame.init()
    
    # checking the initialization
    is_initialized = pygame.get_init()
    
    # printing the result
    print('Is pygame modules initialized:',
    	is_initialized)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    image-20221120225827065

    最后给大家附上

    在 pygame 窗口上显示文本有 7 个基本步骤:

    • 使用 pygame 的 display.set_mode() 方法创建一个显示表面对象。
    • 使用 pygame 的 font.Font() 方法创建一个 Font 对象。
    • 使用pygame字体对象的render()方法,创建一个Text表面对象iesurface对象,上面绘制了Text。
    • 使用pygame文本表面对象的get_rect()方法为文本表面对象创建一个矩形对象。
    • 通过设置pygame矩形对象的center属性的值来设置矩形对象的位置。
    • 使用 pygame 显示表面对象的 blit() 方法将文本表面对象复制到显示表面对象。
    • 使用 pygame 的 display.update() 方法在 pygame 窗口上显示显示表面对象。
    # import pygame module in this program
    import pygame
    
    # activate the pygame library
    # initiate pygame and give permission
    # to use pygame's functionality.
    pygame.init()
    
    # define the RGB value for white,
    # green, blue colour .
    white = (255, 255, 255)
    green = (0, 255, 0)
    blue = (0, 0, 128)
    
    # assigning values to X and Y variable
    X = 400
    Y = 400
    
    # create the display surface object
    # of specific dimension..e(X, Y).
    display_surface = pygame.display.set_mode((X, Y))
    
    # set the pygame window name
    pygame.display.set_caption('坚果show')
    
    # create a font object.
    # 1st parameter is the font file
    # which is present in pygame.
    # 2nd parameter is size of the font
    font = pygame.font.Font('freesansbold.ttf', 32)
    
    # create a text surface object,
    # on which text is drawn on it.
    text = font.render('坚果', True, green, blue)
    
    # create a rectangular object for the
    # text surface object
    textRect = text.get_rect()
    
    # set the center of the rectangular object.
    textRect.center = (X // 2, Y // 2)
    
    # infinite loop
    while True:
    
    	# completely fill the surface object
    	# with white color
    	display_surface.fill(white)
    
    	# copying the text surface object
    	# to the display surface object
    	# at the center coordinate.
    	display_surface.blit(text, textRect)
    
    	# iterate over the list of Event objects
    	# that was returned by pygame.event.get() method.
    	for event in pygame.event.get():
    
    		# if event object type is QUIT
    		# then quitting the pygame
    		# and program both.
    		if event.type == pygame.QUIT:
    
    			# deactivates the pygame library
    			pygame.quit()
    
    			# quit the program.
    			quit()
    
    		# Draws the surface object to the screen.
    		pygame.display.update()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    运行即可。

    image-20221120233357837

  • 相关阅读:
    Linux Socket网络编程UDP、TCP 阻塞与非阻塞 断线重连机制
    左神算法(一)下修改版
    Apache Dubbo 首个 Node.js 3.0-alpha 版本正式发布
    react 常用的hooks—useState、useEffect
    Qt编程-QTableView同时冻结行和列
    Omniverse Machinima
    一篇搞定Sentinel-搭建Spring Cloud Alibaba服务组件Sentinel实现服务资源控制
    【编译原理】词法分析
    QListWidget 类使用教程
    关于element-ui的样式覆盖问题
  • 原文地址:https://blog.csdn.net/qq_39132095/article/details/127957418