python自学 您所在的位置:网站首页 pygame背景图 python自学

python自学

2024-03-08 18:56| 来源: 网络整理| 查看: 265

任务目标:将图片放在屏幕正中央的两种方法

方法一:数学方法

使用方法:使用 image.get_rect()方法获得图像的长(.right)和宽(.bottom),利用数学方法计算出中心点位置并赋值给图像放置位置。

实现代码:

import pygame,sys pygame.init() screen =pygame.display.set_mode([640,480]) pygame.display.set_caption("按键测试") bg_color= [255,255,255] screen.fill(bg_color) image = pygame.image.load("D:\pythonstudy\pygameStudy\skyrobot\images\ship.bmp") rect = image.get_rect() while True: #方法一:将图片放在屏幕正中央 rect_x = (screen.get_width()-rect.right)/2 rect_y = (screen.get_height()-rect.bottom)/2 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.blit(image, [rect_x,rect_y]) pygame.display.flip()

方法二:使用center属性

方法说明:获得屏幕的中心点坐标,直接把该坐标赋值给图像的中心点坐标

代码实现:

import pygame,sys pygame.init() screen =pygame.display.set_mode([6


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有