pygame使用

基本功能

  • 初始化:pygame.init()
  • 设置窗口:screen = pygame.display.set_mode((400,400))
  • 设置标题:pygame.display.set_caption(‘hello world’)
  • 关闭固定代码
while True:
	for event in pygame.event.get():
		if event.type==pygame.QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.flip()
  • 设置字体:
    f=pygame.font.Font(‘C:/Windows/Fonts/simsunb.ttf’,50)

  • 生成文本信息,第一个参数文本内容;第二个参数,字体是否平滑;
    第三个参数,RGB模式的字体颜色;第四个参数,RGB模式字体背景颜色;
    text = f.render(“test”,True,(255,0,0),(0,0,0))

  • 获得显示对象的rect区域坐标
    textRect =text.get_rect()

  • 设置显示对象居中
    textRect.center = (200,200)

  • 将准备好的文本信息,绘制到主屏幕 Screen 上。
    screen.blit(text,textRect)

  • 获得鼠标位置
    (x,y)=pygame.mouse.get_pos()

  • 获得rect坐标
    object.left,object.right,object.top,object.bottom

  • rect移动
    object.move(x, y);
    x>0 右移;x<0左移
    y>0 下移;y<0 上移

  • 鼠标点击
    for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:

  • 加载位图
    space = pygame.image.load(“space.png”).convert_alpha()
    screen.blit(space, (0,0))

  • 修改左上角图标
    p2 = pygame.image.load(r"D:\code\表白\p2.png").convert_alpha()
    pygame.display.set_icon(p2)

  • 图片打包进exe

def get_resource_path(relative_path):
    if getattr(sys,'frozen',False):
        base_path=sys._MEIPASS
    else:
        base_path=os.path.abspath('.')
    return os.path.join(base_path,relative_path)

p1_path=get_resource_path(os.path.join('resources','p1.png'))
p2_path=get_resource_path(os.path.join('resources','p2.png'))

1.cmd 执行pyi-makespec -F name.py 生成spec文件
2.修改spec文件,data=[]改为[(‘resources’,‘resources’)]
3.cmd执行pyinstaller -F name.spec

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值