1 说明: ===== 1.1 pygame显示图片的方法,熟悉pygame的各种参数,动画设置,由浅入深,小白秒懂,值得收藏。 1.2 图片来源:今日头条正版免费图库,向女神高圆圆致敬,仅供学习。 1.3 环境:python3.8+pygame 1.9.6+微软编辑器vscode+深度操作系统deepin-linux。 gyy1.jpeg gyy2.jpeg gyy3.jpeg 2 基本图片显示: ============ 2.1 代码: #第1步:模块导入import pygame,sys#可调用参数,比如:RESIZABLE,窗口大小可调节from pygame.locals import * #第2步:初始化pygame.init()#窗口标题名:支持中文pygame.display.set_caption('show pic')#读取或加载图片img = pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg') #获取位图的宽和高width,height = img.get_size()#定义屏幕和设置窗口大小,这样的宽和高的设置符合图片大小screen = pygame.display.set_mode([width,height],RESIZABLE, 32)#背景颜色填充screen.fill([255,255,255])#显示图片和指定坐标点0,0左上角开始screen.blit(img,[0,0])#更新整个待显示的 Surface 对象到屏幕上#pygame.display.flip()#更新部分内容显示到屏幕上,如果没有参数,则与flip功能相同(上一条)pygame.display.update()#当使用OpenGL的时候,不能使用update()来更新窗口,需要使用flip() 来更新#第3步:退出设置running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit() 2.2 图:简单,图省略。 3 图片缩小显示: ============ 3.1 代码:
3.2 操作效果图: 3.3 附赠知识点:保真的图片大小缩放修改,代码。 # -*- coding: utf-8 -*-#不失真的图片修改:放大和缩小from PIL import Imagedef ResizeImage(filein, fileout, width, height, type): ''' filein: 输入图片 fileout: 输出图片 width: 输出图片宽度 height:输出图片高度 type:输出图片类型(png, gif, jpeg...) ''' img = Image.open(filein) out = img.resize((width, height),Image.ANTIALIAS) out.save(fileout, type)#需要修改的图片和路径filein = '/home/xgj/Desktop/pygame-pic/gyy2.jpeg'#生成图片和路径fileout = '/home/xgj/Desktop/pygame-pic/gyy22.png'#需要修改的宽和高width = 200height = 120type1 = 'png'if __name__ == '__main__': ResizeImage(filein, fileout, width, height, type1) ===高级一点=== 4 自动从下往上移动图片: =================== 4.1 代码:
4.2 背景图自己下载,来自今日头条免费正版图库,修改大小可用到上面保真图片修改代码。 4.3 效果图:由于gif太大,不能上传,故截屏。 5 按方向键调整图片移动方向: ====================== 5.1 代码: #第1步:导入模块import pygame,sys#第2步:加载图片和初始化#读取图片img=pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg')#获取图片的宽和高picwidth,picheight = img.get_size()#初始化pygame.init()#窗口标题screen=pygame.display.set_caption('按键盘方向键移动图片')#窗口大小winwidth=2000winheight=1200screen=pygame.display.set_mode([winwidth,winheight])#默认字体和大小设置my_font=pygame.font.SysFont(None,22)#屏幕白色255,255,255screen.fill([0,0,0]) #背景颜色设置黑色#第3步:定义相关函数#定义函数:加载图片def loadimg(xloc,yloc): locationxy=[xloc,yloc] screen.blit(img,locationxy) pygame.display.flip()#定义函数:加载文字def loadtext(xloc,yloc): textstr='location:'+str(xloc)+','+str(yloc) text_screen=my_font.render(textstr, True, (255, 0, 0)) #50和50是坐标,位于左上角 screen.blit(text_screen, (50,50))#第4步:定义主函数def main(): #文字加载位置 loadtext(310,0) #上下移动参数 looper=1200 #水平移动参数 shuip=2000 #循环 while True: #退出设置 for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() #功能键定义 elif event.type == pygame.KEYDOWN: #按键↑功能定义 if event.key==pygame.K_UP: looper=looper-50 #pic的高 if looper<-picheight: #高 looper=winheight #按键↓功能定义 if event.key==pygame.K_DOWN: looper=looper+50 #高 if looper>winheight: looper=-picheight #按键←功能定义 if event.key == pygame.K_LEFT: shuip=shuip-50 #pic的宽 if shuip<-picwidth: shuip=winwidth #按键→功能定义 if event.key == pygame.K_RIGHT: shuip=shuip+50 if shuip>winwidth: shuip=0 #再次屏幕填充黑色 screen.fill([0,0,0]) loadtext(shuip,looper) loadimg(shuip,looper)#第5步:if __name__=='__main__': main() 5.2 效果图: |
|
来自: 老友mk09qda3vs > 《尚待分类音乐歌曲》