Python I';我是pygame新手,希望得到get_rect()的帮助?

Python I';我是pygame新手,希望得到get_rect()的帮助?,python,pygame,Python,Pygame,我是pygame的新手,试图理解get_rect(),但仍然遇到错误,请帮助 就像试图让我的“静止”的人不进入“土壤” 我想先从这张静止的图片开始,然后再继续那个动作 stationary = pygame.image.load(os.path.join('standing.png')) # Another (faster) way to do it - using the sprites that face right. right = [None]*10 for picIndex in r

我是pygame的新手,试图理解get_rect(),但仍然遇到错误,请帮助 就像试图让我的“静止”的人不进入“土壤”

我想先从这张静止的图片开始,然后再继续那个动作

stationary = pygame.image.load(os.path.join('standing.png'))

# Another (faster) way to do it - using the sprites that face right.
right = [None]*10
for picIndex in range(1,10):
    right[picIndex-1] = pygame.image.load(os.path.join('pictures/R' + str(picIndex) + ".png"))
    picIndex+=1

left = [None]*10
for picIndex in range(1,10):
    left[picIndex-1] = pygame.image.load(os.path.join('pictures/L' + str(picIndex) + ".png"))
    picIndex+=1

soilImg = pygame.image.load('soil.png')
soilX = 150
soilY = 200

x = 200
y = 223
vel_x = 5
vel_y = 5
jump = False
move_left = False
move_right = False
stepIndex = 0

def soil(x, y):
    screen.blit(soilImg, (x, y))



# Draw the Game
def draw_game():
    global stepIndex
    screen.blit(background, (0,0))
    if stepIndex >= 36:
        stepIndex = 0
    if move_left:
        screen.blit(left[stepIndex//4], (x, y))
        stepIndex += 1
    elif move_right:
        screen.blit(right[stepIndex//4], (x,y))
        stepIndex += 1
    else:
        screen.blit(stationary, (x,y))
这里可能有问题

# Feels like something is wrong here
soil_rect = soilImg.get_rect()
stationary_rect = stationary.get_rect()
stationary_rect.x = soil_rect.x
stationary_rect.y = soil_rect.y


# Main Loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    draw_game()

    # Movement
    userInput = pygame.key.get_pressed()
    if userInput[pygame.K_LEFT]:
        x -= vel_x
        move_left = True
        move_right = False
    elif userInput[pygame.K_RIGHT]:
    x += vel_x
        move_left = False
        move_right = True
    else:
        move_left = False
        move_right = False
        stepIndex = 0
    if jump == False and userInput[pygame.K_SPACE]:
        jump = True
    if jump == True:
        y -= vel_y*2
        vel_y -= 1
        if vel_y < -10:
            jump = False
            vel_y = 10

    if userInput[pygame.K_ESCAPE]:
        run = False         
    
    if x < -15:
        x = -15
    elif x > 635:
        x = 635
    if y > 223:
        y = 223

    soil(soilX, soilY)
    pygame.time.delay(30)
    pygame.display.update()
这里可能有问题

# Feels like something is wrong here
soil_rect = soilImg.get_rect()
stationary_rect = stationary.get_rect()
stationary_rect.x = soil_rect.x
stationary_rect.y = soil_rect.y


# Main Loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    draw_game()

    # Movement
    userInput = pygame.key.get_pressed()
    if userInput[pygame.K_LEFT]:
        x -= vel_x
        move_left = True
        move_right = False
    elif userInput[pygame.K_RIGHT]:
    x += vel_x
        move_left = False
        move_right = True
    else:
        move_left = False
        move_right = False
        stepIndex = 0
    if jump == False and userInput[pygame.K_SPACE]:
        jump = True
    if jump == True:
        y -= vel_y*2
        vel_y -= 1
        if vel_y < -10:
            jump = False
            vel_y = 10

    if userInput[pygame.K_ESCAPE]:
        run = False         
    
    if x < -15:
        x = -15
    elif x > 635:
        x = 635
    if y > 223:
        y = 223

    soil(soilX, soilY)
    pygame.time.delay(30)
    pygame.display.update()
stational_rect.x=土壤_rect.x
静止_-rect.y=土壤_-rect.y
#主回路
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
平局
#运动
userInput=pygame.key.get_pressed()
如果用户输入[pygame.K_LEFT]:
x-=标高x
向左移动=真
向右移动=错误
elif userInput[pygame.K_RIGHT]:
x+=标高x
向左移动=错误
向右移动=真
其他:
向左移动=错误
向右移动=错误
步骤索引=0
如果jump==False且用户输入[pygame.K_SPACE]:
跳转=真
如果跳转==真:
y-=水平y*2
水平y-=1
如果y级<-10:
跳转=错误
水平y=10
如果用户输入[pygame.K_ESCAPE]:
运行=错误
如果x<-15:
x=-15
elif x>635:
x=635
如果y>223:
y=223
土壤(soilX,soilY)
pygame。时间。延迟(30)
pygame.display.update()
返回一个带有曲面对象大小的矩形,该矩形始终从(0,0)开始。 请注意,曲面对象没有位置。使用
blit
功能将表面放置在显示屏上的某个位置。
矩形的位置可以通过关键字参数指定。例如,可以使用关键字参数
center
指定矩形的中心。这些关键字参数在返回之前应用于的属性(有关关键字参数的完整列表,请参阅):

soil_rect=soilImg.get_rect(左上=(soilX,soilY))
静止=静止。获取(左上=(x,y))

您可以删除变量
x
y
soilX
soilY
,然后使用
soil\u rect
stational\u rect

我不应该再做些改变吗?还是这是唯一的问题?谢谢你的回答btw@Mr.Genetric我似乎根本没有使用
stational\u rect
soil\u rect
。如果要使用它们,必须更新应用程序循环中的矩形-