Pygame图像定位

问题描述 投票:1回答:1

试图将这两张图片并排放在pygame的界面中,但是我收到了错误

TypeError:blit的目标位置无效

import pygame
from pygame.locals import *
import urllib.request
from urllib.request import urlopen

pygame.init() #initialize pygame

image1_url = "http://polygonblog.com.www32.zoner-asiakas.fi/wp-content/uploads/2010/05/underwater.jpg"
image1_str = urllib.request.urlretrieve(image1_url, "sea.png")

image2_url = "http://www.seascapemodeling.org/seascape_projects/Slicks.jpg"
image2_str = urllib.request.urlretrieve(image2_url, "surface.png")

white = (255,255,255)
screen = pygame.display.set_mode((600,440))
sea = pygame.image.load('sea.png').convert()
sea_rect = sea.get_rect()
sea_rect.x = 220
sea_rect.y = 600
sea_rect = sea_rect.left
surface = pygame.image.load('surface.png').convert()
surface_rect = surface.get_rect()
surface_rect.x = 220
surface_rect.y = 600
surface_rect = surface_rect.right
screen.fill(white)

while True:
    screen.blit(surface, surface_rect)
    screen.blit(sea,sea_rect)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            raise SystemExit
python python-3.x pygame
1个回答
0
投票

destpygame.Surface.blit()参数(第二个参数)必须是位置(x,y)或矩形(x,y,w,h)。

任务完成后

sea_rect = sea_rect.left

surface_rect = surface_rect.right

sea_rectsurface_rect只是单个积分值。注意.left返回矩形的左x坐标,.right返回右x坐标。 .topleft.topright将返回矩形的顶部(x,y)角坐标。见pygame.Rect

略过这2行

sea_rect = sea_rect.left surface_rect = surface_rect.righ

而是通过这个位置

screen.blit(surface, surface_rect.topright)
screen.blit(sea, sea_rect.topleft

或矩形直接到.blit()

screen.blit(surface, surface_rect)
screen.blit(sea, sea_rect)
© www.soinside.com 2019 - 2024. All rights reserved.