使用python 3.4在pygame中获取表面中心 [英] Getting the center of surfaces in pygame with python 3.4

查看:95
本文介绍了使用python 3.4在pygame中获取表面中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用pygame获取曲面中心时遇到麻烦。尝试将其放置在其他曲面上时,它默认使用曲面的左上角。

I'm having troubles getting the center of surfaces with pygame. It defaults to the upper left corner of surfaces when trying to place them on other surfaces.

为演示我的意思,我写了一个简短的程序。

To demonstrate what I mean I wrote a short program.

import pygame

WHITE = (255, 255, 255)

pygame.init()
#creating a test screen
screen = pygame.display.set_mode((500, 500), pygame.RESIZABLE)
#creating the canvas
game_canvas = screen.copy()
game_canvas.fill(WHITE)
#drawing the canvas onto screen with coords 50, 50 (tho its using the upper left of game_canvas)
screen.blit(pygame.transform.scale(game_canvas, (200, 200)), (50, 50))
pygame.display.flip()


#you can ignore this part.. just making the program not freeze on you if you try to run it
import sys

clock = pygame.time.Clock()
while True:
    delta_time = clock.tick(60) / 1000
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

如果运行此程序,它将在桌面上绘制200 x 200白色game_canvas屏幕(显示)在坐标50、50处。但是,不是使用game_canvas在坐标50、50处的中心。左上角是50、50。

If you run this program it will draw a 200 by 200 white game_canvas on the screen (display) at the coords 50, 50. But instead of using the center of the game_canvas at the coords 50, 50.. the upper left is at 50, 50.

那么我如何使用game_canvas的中心将其放置在坐标50、50或任何其他给定的坐标上?

So how do I use the center of the game_canvas to have it place at the coords 50, 50 or any other given coords?

推荐答案

它将始终在左上角涂抹Surface。解决此问题的一种方法是计算必须将曲面放置在何处才能使其围绕某个位置居中。

It will always blit the Surface at the topleft corner. A way around this is to calculate where you have to place the Surface in order for it to be centered around a position.

x, y = 50, 50
screen.blit(surface, (x - surface.get_width() // 2, y - surface.get_height() // 2 ))

这会将其中心定位在(x,y)坐标上。

This will position it's center at the (x, y) coordinates.

这篇关于使用python 3.4在pygame中获取表面中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆