Python 之 pygame 学习(全屏,可变尺寸,图像转换)

您所在的位置:网站首页 电脑放图片怎么全屏 Python 之 pygame 学习(全屏,可变尺寸,图像转换)

Python 之 pygame 学习(全屏,可变尺寸,图像转换)

2024-07-08 03:08:10| 来源: 网络整理| 查看: 265

文章目录 一、全屏显示,可变尺寸,图像转换二、示例

一、全屏显示,可变尺寸,图像转换 初始化窗口或屏幕以进行显示 pygame.display.set_mode() 初始化窗口或屏幕以进行显示 set_mode(size =(0,0),flags = 0,depth = 0) size:参数是一对表示宽度和高度的数字 depth:参数表示用于颜色的位数(通常最好不要传递深度参数。它将默认为系统的最佳和最快颜色深度) flags:参数控制您想要的显示类型。有几种可供选择,您甚至可以使用按位或运算符(管道“|”字符)组合多种类型。如果传递0或没有flags参数,它将默认为软件驱动的窗口。

flag 参数: 在这里插入图片描述 2. pygame.transform 用于图像转换 在这里插入图片描述如:

pygame.transform.flip(Surface,x,y) # 这可以垂直,水平或两者翻转Surface pygame.transform.rotate(曲面,角度) #翻转曲面,逆时针旋转 # 将表面平滑地缩放到任意大小 pygame.transform.smoothscale(Surface,(width,height)) 二、示例

注意: 每次重新获得图像的位置矩形 dog_rect = dog.get_rect() left 和 top 均重置为0,即回到左上角原点

因此别忘更新 left 和 top相关值

import pygame import sys from pygame.locals import * pygame.init() size = width,height = 600,400 bg = (255,255,255) speed = [5,0] fullscreen = False # 默认不全屏 # 创建指定大小的窗口 Surface,窗口可改变 screen = pygame.display.set_mode(size,RESIZABLE) pygame.display.set_caption("初次见面,请大家多多关照") # 设置放大缩小的比率 ratio = 1.0 dog = pygame.image.load("dog.png") dog_t = dog # 方便观察只操作 dog_t # roate 逆时针旋转角度 dog_right = pygame.transform.rotate(dog,90) dog_top = pygame.transform.rotate(dog,180) dog_left = pygame.transform.rotate(dog,270) dog_bottom = dog dog = dog_top # 初始状态从上边界开始 # 获得图像的位置矩形 dog_rect = dog.get_rect() position = dog_t_rect = dog_rect # 设置为死循环,确保窗口一直显示 while True: # 遍历所有的事件 for event in pygame.event.get(): # 如果单击关闭窗口,则退出 if event.type == pygame.QUIT: sys.exit() # 根据键盘操作来控制小狗的移动 if event.type == KEYDOWN: # 全屏(F11) if event.key == K_F11: fullscreen = not fullscreen if fullscreen: #list_modes()可以自动获取电脑屏幕所支持的显示大小,默认由大到小排列成一个列表 # 所以取第一个即是全屏 size = width, height = pygame.display.list_modes()[0] screen = pygame.display.set_mode(size,FULLSCREEN | HWSURFACE) else: size = width,height = 600,400 screen = pygame.display.set_mode(size) # 放大缩小,空格键恢复原始尺寸 if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE: # 最大只能放大一倍,最小缩小 50% if event.key == K_EQUALS and ratio 0.5: ratio -= 0.1 if event.key == K_SPACE: ratio = 1.0 dog = pygame.transform.smoothscale(dog_t,\ (int(dog_t_rect.width*ratio),\ int(dog_t_rect.height*ratio))) # 用户手动调整窗口尺寸 if event.type == VIDEORESIZE: size = event.size width, height = size screen = pygame.display.set_mode(size, RESIZABLE) # 移动图像 #position.move(x,y) 返回由给定偏移量移动的新矩形。x和y参数可以是任何整数值,正数或负数。 position = position.move(speed) if position.right >width: dog = dog_right position = dog_rect = dog.get_rect() # 这里重新获取了 dog,即 left 和 top 又初始了0 print(position.left,position.right,position.top,position.bottom) # 因此这里需要重新更新下 left 保证沿右边界运行 position.left = width - dog_rect.width speed = [0,2] if position.bottom >height: dog = dog_bottom position = dog_rect = dog.get_rect() position.left = width - dog_rect.width position.top = height - dog_rect.height speed = [-2,0] if position.left


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭