python pygame库入门

pygame提供的模块:

        pygame.display        访问显示设备

        pygame.event          管理事件

        pygame.draw           绘制形状、线和点

        pygame.surface       管理图像和屏幕

        pygame.rect             坐标处理

        pygame.font             文字处理

        pygame.music         音效处理

        pygame.sprite          动画精灵

        pygame.image         图片处理

        pygame.sprite          时间处理

导入 pygame 库:

        import pygame

        from pygame.locals import *        # pygame.locals 是管理 pygame 中的常量的模块,导入有很多常用的常量和函数给我们使用。

初始化 pygame库:

        pygame.init()        #获取并检测我们计算机的硬件设备,当无法检测到音频或视频的一些设备时,可能会导致 pygame 库中一些模块无法使用。 因此,使用前必须先初始化 pygame。

创建窗口:

        import pygame,sys #导入pygame库和system库

        from pygame.locals import * #导入常量

        pygame.init() #初始化pygame

        screen=pygame.display.set_mode([640,480]) #创建窗口并设置窗口大小使用pygame.display 模块。       

        while True: #让窗口不闪退

                for event in pygame.event.get(): #遍历所有事件的列表,用 pygame.event模块。

                        if event.type==QUIT: #如果事件类型是退出

                                sys.exit() #退出窗口

猜你喜欢

转载自blog.csdn.net/weixin_44457827/article/details/127163096