Pygame:如何正确使用 get_rect() [英] Pygame: How to correctly use get_rect()

查看:155
本文介绍了Pygame:如何正确使用 get_rect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 get_rect() 的工作原理.在这个简单的例子中,我有两张图片,想要获取第二张图片的位置并将第一张图片移动到第二张图片.

I'm trying to understand how get_rect() works. In this simple example, I have two images and want to obtain the location of the second and move the first image to the second image.

我在网上查看了各种示例,但无法使其正常工作.我做错了什么?

I have looked at a variety of examples online and cannot get this to work. What am I doing wrong?

import pygame, sys
from pygame.locals import *
import time

pygame.init()
FPS = 10 # frames per second setting
fpsClock = pygame.time.Clock()

# Set up the window
DISPLAYSURF = pygame.display.set_mode((600, 400), 0, 32)
pygame.display.set_caption('Test program for get_rect()')

WHITE = (255, 255, 255)

# Load two images
baseImg = pygame.image.load('image1.jpg')
spaceshipImg = pygame.image.load('image2.jpg')

DISPLAYSURF.fill(WHITE)

# Place one image at the bottom of the screen
DISPLAYSURF.blit(baseImg, (300, 300))
pygame.display.update()

# Place the second image at the top of the screen
DISPLAYSURF.blit(spaceshipImg, (300, 0))
pygame.display.update()

# Wait for one second
time.sleep(1)

# Obtain the rectangle for each image
baseRect = baseImg.get_rect()
spaceshipRect = spaceshipImg.get_rect()

# This is where I believe I'm going wrong
# I understand this to obtain the x,y of the spaceship image
# Set the xy coordinates for the top image to the xy of the bottom image
spaceshipRect.x = baseRect.x
spaceshipRect.y = baseRect.y

# Move the top image to new xy position
# However this doesn't work
DISPLAYSURF.blit(spaceshipImg, (spaceshipRect.x, spaceshipRect.y))

pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

推荐答案

首先,images/pygame.Surfaces 没有位置,因此您必须将 blit 位置存储在 rect 中.当您调用 pygame.Surface 的 get_rect 方法时,Pygame 会创建一个具有图像大小和 x、y 坐标 (0, 0) 的新矩形.要在实例化期间为 rect 提供其他坐标,您可以将参数传递给 get_rect,主要使用 centertopleft.要稍后移动矩形,您可以更改矩形的以下任何属性:

First, images/pygame.Surfaces don't have a position, so you have to store the blit position in the rect. When you call the get_rect method of a pygame.Surface, Pygame creates a new rect with the size of the image and the x, y coordinates (0, 0). To give the rect other coords during the instantiation you can pass an argument to get_rect, mostly center or topleft is used. To move the rect later, you can change any of these attributes of the rect:

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

这是一个例子(按 a 或 d 改变矩形的位置,从而改变图像的 blit 位置):

Here's an example (press a or d to change the position of the rect and thereby the blit pos of the image):

import sys
import pygame as pg

BG_COLOR = pg.Color(80, 60, 70)
PLAYER_COLOR = pg.Color(90, 140, 190)

def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()

    player_img = pg.Surface((40, 60))
    player_img.fill(PLAYER_COLOR)
    # Create a rect with the size of the image/pygame.Surface
    # and immediately set it's topleft coords to (100, 300).
    player_rect = player_img.get_rect(topleft=(100, 300))

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_d:
                    # Set the center to these new coords.
                    player_rect.center = (400, 200)
                if event.key == pg.K_a:
                    # Set the x coord to 300.
                    player_rect.x = 300

        screen.fill(BG_COLOR)
        screen.blit(player_img, player_rect)
        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

这篇关于Pygame:如何正确使用 get_rect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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