Pygame 如何将rect属性设置为sprite

Pygame 如何将rect属性设置为sprite,pygame,Pygame,当我运行此代码时,它不会在屏幕上显示精灵。只是一个空白的屏幕,我已经尝试了我能想到的一切来让这个工作。一些帮助将不胜感激 我已经想尽了一切办法让它发挥作用。我要做的是用rect属性创建精灵 import pygame pygame.display.init() pygame.display.set_mode((0, 0), pygame.FULLSCREEN) x = 300 y = 500 x1 = 100 y1 = 200 image1 = pyga

当我运行此代码时,它不会在屏幕上显示精灵。只是一个空白的屏幕,我已经尝试了我能想到的一切来让这个工作。一些帮助将不胜感激

我已经想尽了一切办法让它发挥作用。我要做的是用rect属性创建精灵

import pygame
pygame.display.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

    x = 300
    y = 500
    x1 = 100
    y1 = 200
    image1 = pygame.sprite.Sprite()
    image1.image = pygame.image.load("picy.png").convert_alpha()
    image2 = pygame.sprite.Sprite()
    image2.image = pygame.image.load("picy1.png").convert_alpha()
    image1_rect = image1.image.get_rect(topleft=(x,y))
    image2_rect = image2.image.get_rect(topleft=(x1,y1))
    screen.blit(image2_rect,(x1,y1))
    screen.blit(image1_rect,(x,y))
    pygame.display.update()
我希望它能将我的两个精灵放在屏幕上,当他们触摸时,他们会记录一个点击。

来自:

blit()
将一个图像绘制到另一个图像上
blit(源、目标、区域=无,特殊标志=0)->Rect
在此曲面上绘制源曲面。可以使用dest参数定位绘图。Dest可以是表示源的左上角的一对坐标。矩形也可以作为目标传递,矩形的左上角将用作blit的位置。目标矩形的大小不影响blit

blit
方法将
Surface
作为第一个参数,而不是
Rect
表面是您的图像。
尝试:

screen.blit(image2.image, dest=image2_rect)
screen.blit(image1.image, dest=image1_rect)
顺便说一下,您可能还希望创建
Sprite
实例的矩形属性,而不是单独的实例:

image1.rect = image1.image.get_rect(topleft=(x,y))
image2.rect = image2.image.get_rect(topleft=(x1,y1))