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