快捷搜索:  汽车  科技

pygame模块怎么运行(Pygame初步认识模块导入)

pygame模块怎么运行(Pygame初步认识模块导入)import pygame if __name__ == '__main__': # 1、初始化pygame pygame.init() # 2、创建游戏窗口 # set_mode(宽度,高度) screen = pygame.display.set_mode((600 400)) # 3、游戏循环 while True: # 检测事件 for event in pygame.event.get(): # 检测窗口上的关闭按钮是否被点击 if event.type == pygame.QUIT: # 退出游戏 print('关闭按钮被点击') exit() 注意事项:导入成功与否检测代码image.png这个过程可能会花费几分钟的时间,具体视网速决定。安装完成得到如下界面,之前还在上一页面安装成功会在底部有一个提示。image.png

pygame模块怎么运行(Pygame初步认识模块导入)(1)

pygame的导入

pygame模块怎么运行(Pygame初步认识模块导入)(2)

模块对应功能

初次使用pygame需要从网上下载pygame的包,下载过程如下所示:

File -- Settings -- Project:当前项目名称 -- Project interpreter --

根据此路径即可进入搜索页面,输入pygame然后点击安装

pygame模块怎么运行(Pygame初步认识模块导入)(3)

image.png

这个过程可能会花费几分钟的时间,具体视网速决定。安装完成得到如下界面,之前还在上一页面安装成功会在底部有一个提示。

pygame模块怎么运行(Pygame初步认识模块导入)(4)

image.png

导入成功与否检测代码

import pygame if __name__ == '__main__': # 1、初始化pygame pygame.init() # 2、创建游戏窗口 # set_mode(宽度,高度) screen = pygame.display.set_mode((600 400)) # 3、游戏循环 while True: # 检测事件 for event in pygame.event.get(): # 检测窗口上的关闭按钮是否被点击 if event.type == pygame.QUIT: # 退出游戏 print('关闭按钮被点击') exit()

注意事项:

1、测试的文件名不能与pygame重名,要不系统会默认导入自己你的测试文件而非从网上下载的pygame包

2、也可以在导入pygame包之后,直接使用

screen = pygame.display.set_mode((600 400))

测试安装是否成功。成功是一个一闪而过的对话框。

静态画布

画文字

具体步骤:

1.创建字体对象(找笔)

2.根据字体去创建显示对象(文字)(找内容)

3.将内容添加到窗口上(画到纸上)

4.将窗口上的内容展示出来(将画好文字的纸贴出来)

注:模块导入中的测试代码为pygame程序绘画运行框,故为基本代码,每个pygame都需要使用。系统自带字体不支持中文输出,会乱码,如下代码中已被注释的部分,直接使用其会中文乱码。所以需要一般都需要创建自定义字体。

具体代码演示:

import pygame if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((600 400)) # 设置窗口背景颜色 screen.fill((0 0 0)) # 1.创建字体对象(找笔) """ 创建系统字体 Sysfont(name size bold=False italic=False constructor=None) name --> 字体名 size --> 字体大小 bold --> 加粗 italic -> 倾斜 """ # font = pygame.font.SysFont('宋体' 44) """ 创建自定义字体 Font(字体文件路径,字体大小) """ font = pygame.font.Font('./font/aa.ttf' 22) # 2.根据字体去创建显示对象(文字)(找内容) """ render(self text antialias color background = None) text -> 要显示的文字内容(str) antialias -> 是否平滑 color -> 计算机三原色(红、绿、蓝) .RGB颜色,值的范围都是0-255 以元组表示((255 0 0) -> 红色 (0 0 0) -> 黑色 (255 255 255) -> 白色 (x x x)-> 灰色) 也可以用两位16进制表示() """ surface = font.render('你好 python!' True (255 0 0)) # 3.将内容添加到窗口上(画到纸上) """ blit(需要显示的对象,显示位置) 需要显示的对象 --> Surface类型的数据 显示位置 --> 坐标(x y) """ screen.blit(surface (100 100)) # 4.将窗口上的内容展示出来(将画好文字的纸贴出来) pygame.display.flip() # 游戏循环 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit()

画图片

具体步骤:

1、获取图片对象

2、将图片渲染到窗口上

注:在获取图片对象进行形变的时候,位移通过渲染坐标的设置实现,transform中并不包括平移的方法,只有缩放和位移。不过多使用演示代码中的第三种方法,也叫旋转缩放,因为它可以实现按比例缩放,这样可以保留图片的样子只改变大小了。

import pygame if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((600 400)) screen.fill((0 0 0)) # 1、获取图片对象 image = pygame.image.load('./image/the.jpg') """ a.获取图片大小 """ image_size = image.get_size() print(image_size) """ b.形变: transform:形变包含旋转、缩放和平移 scale(缩放对象,新的大小) --> 返回一个缩放后的新对象 """ new_image = pygame.transform.scale(image (50 50)) """ 旋转 rotate(旋转对象,旋转角度) """ image = pygame.transform.rotate(image 180) """ rotozoom(旋转对象,旋转角度 缩放比例): """ pygame.transform.rotozoom(image 0 0.4) # 2、将图片渲染到窗口上 screen.blit(image (50 50)) screen.blit(new_image (0 0)) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() display_shape

主要基于通过方法对于坐标的操作,相关方法可以pygame.draw.根据提示引入通过Ctrl 鼠标点击查看参数属性及功能

import pygame if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((900 500)) screen.fill((0 0 0)) # 1.画直线 """ line(Surface color start_pos end_pos width=1) Surface --> 画在哪个地方 color --> 线的颜色 start_pos --> 起点 end_pos --> 终点 width --> 线的宽度 """ pygame.draw.line(screen (255 0 0) (29 29) (58 49) 3) """ lines(Surface color closed pointlist width=1): """ pygame.draw.lines(screen (0 255 0) False [(0 0) (100 100) (500 344) (15 67)] 3) """ #画曲线 arc(Surface color Rect start_angle stop_angle width=1) Rect -> (x y width height)矩形 """ from math import pi pygame.draw.arc(screen (0 0 255) (200 200 200 200) pi / 2 pi) """ 画矩形 rect(Surface color Rect width=0) """ pygame.draw.rect(screen (111 111 111) (200 200 200 200) 3) """ 3.画圆 circle(Surface color pos radius width=0) """ import random pygame.draw.circle(screen \ (random.randint(0 255) random.randint(0 255) random.randint(0 255)) \ (300 300) 100 6) """ 4.画椭圆 ellipse(Surface color Rect width=0): """ pygame.draw.ellipse(screen (0 100 0) (100 300 200 80) 1) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit()

动起来的圆

import pygame if __name__ == '__main__': pygame.init() screen = pygame.display.set_mode((600 400)) screen.fill((255 255 255)) x = 0 y = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() x = 1 y = 1 screen.fill((255 255 255)) pygame.draw.circle(screen (255 0 0) (x y) 80) pygame.display.flip()zuzuzu

如果这篇文章对你有所帮助,请多多点赞收藏转发评论,让更多的人看到这篇文章。不要忘记点关注,后续我还会分享更多精彩有趣的文章。

最后小编有一套自学资料,需要的朋友可以回复【01】获取。

猜您喜欢: