Pygame 图片位置 [英] Pygame Image position

查看:68
本文介绍了Pygame 图片位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 pygame 来看看我能想出什么并遇到一个问题,如果没有这个 pygame.sprite.Sprite 和 rect 的东西我就找不到答案,我的问题是我如何找到图像的位置,因为我需要它的位置来计算旋转角度.如果有帮助,这是我正在使用的代码:

I've recentley started using pygame to see what I could come up with and come across a problem which I can't find an answer to without this pygame.sprite.Sprite and rect things, my question is how do I find a position of an image since I need the position of it to calculate a rotation angle. If it helps, here's the code that i am using:

import sys, pygame, math, time;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
fire_beam = ('beam.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
f_beam = pygame.image.load(fire_beam).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
space_ship_rect = space_ship.get_rect() #added
while True:
    screen.blit(bk, (0, 0))
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN and event.button == 1:
            print("Left Button Pressed")
        elif event.type == MOUSEBUTTONDOWN and event.button == 3:
            print("Right Button Pressed")
        if event.type == MOUSEMOTION:
            x1, y1 = pygame.mouse.get_pos()
            x2, y2 = space_ship_rect.x, space_ship_rect.y #added
            dx, dy = x2 - x1, y2 - y1
            rads = math.atan2(dx, dy)
            degs = math.degrees(rads)
            print degs
            pygame.transform.rotate(space_ship, degs)
            pygame.display.update()
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    #screen.blit(space_ship, (375, 300)) #space_ship_rect did not work here.
    pygame.display.update()

推荐答案

您可以使用 .get_rect() for image 来获取图像矩形 (pygame.Rect())

You can use .get_rect() for image to get image rectangle (pygame.Rect())

space_ship = pygame.image.load(spaceship).convert_alpha()

space_ship_rect = space_ship.get_rect()

然后你可以得到 xywidthheight 甚至 centerxcenterycenter

and than you can get x, y, width, height and even centerx, centery, center etc.

print space_ship_rect.x, space_ship_rect.y, 
print space_ship_rect.centerx, space_ship_rect.centery, 
print space_ship_rect.center
print space_ship_rect.left, space_ship_rect.right
print space_ship_rect.top, space_ship_rect.bottom
print space_ship_rect.topleft, space_ship_rect.bottomright
print space_ship_rect.width, space_ship_rect.height

顺便说一句:.get_rect() 也适用于您的 screen 和其他 pygame.Surface().

by the way: .get_rect() works also with your screen and other pygame.Surface().

但是您不能为图像分配新值,因此您必须保留 space_ship_rect 并更改其中的值(因此仅使用一次 get_rect() 来获取图像大小)

But you can't assign new values to image so you have to keep space_ship_rect and change values in it (so use get_rect() only once to get image size)

space_ship_rect.x = 100
space_ship_rect.y = 200
 # or
space_ship_rect.centerx = 100
space_ship_rect.centery = 200

如果改变x, y 矩形重新计算centerx, centery 使用width> 和 height.如果更改centerxcentery 矩形重新计算xy

If you change x, y rectangle recalculate centerx, centery using width and height. If you change centerx, centery rectangle recalculate x and y

您可以使用 self.image 为位图创建类,使用 self.rect 为图像大小和位置创建类.

You could create class with self.image for bitmap and self.rect for image size and position.

ps.你可以使用 screen.blit(space_ship, space_ship_rect)

这篇关于Pygame 图片位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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