0

I'm new to pygame and trying to understand the get_rect() but still gets error pls help like just trying to get my "stationary" person not getting in to that "soil"

import pygame
import os

pygame.init()
screen = pygame.display.set_mode((680,340))
pygame.display.set_caption("Collector")
icon = pygame.image.load('knight.png')
pygame.display.set_icon(icon)
background = pygame.image.load('background.png')

i want to first start with this stationary picture hen move on to that movement as well

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))

it might be something wrong here

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

it might be something wrong here

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()

1 Answers1

0

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0). Note, a Surface object has no position. The Surface is placed at a position on the display with the blit function.
The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments):

soil_rect = soilImg.get_rect(topleft = (soilX, soilY))
stationary_rect = stationary.get_rect(topleft = (x, y))

You can remove the variables x, y, soilX and soilY, and use soil_rect and stationary_rect instead.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • shouldn't i change on something more? or is this the only problem? thank u for ur answer btw – Mr. Genetric Apr 11 '21 at 12:13
  • @Mr.Genetric I doesn't seem that `stationary_rect` and `soil_rect` are used at all. If you want to use them, you have to update the rectangles in the application loop- – Rabbid76 Apr 11 '21 at 12:23