import pygame
import keyboard
screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
blue = (20,40,200)
gray = (100,100,100)
x=200
y=200
w=60
h=60
p=0
OL=0
vel=0.1
#variables
def player():
pygame.draw.rect(screen,blue,pygame.Rect(p+x,p+y,p+w,p+h))
def obst():
pygame.draw.rect(screen,gray,pygame.Rect(OL+100,OL+100,OL+100,OL+10))
run=True
pygame.init()
while run:
screen.fill((255,255,255))
player()
obst()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x>0 and x>OL:
x-= vel
elif x==OL:
vel=0
if keys[pygame.K_RIGHT]and x<800-w and x>OL:
x+= vel
elif x==OL:
vel=0
if keys[pygame.K_UP]and y>0 and y>OL:
y-= vel
elif y==OL:
vel=0
if keys[pygame.K_DOWN]and y<600-h and y>OL:
y+= vel
elif y==OL:
vel=0
pygame.display.update()
pygame.quit()
I tried putting variables in the coordinates of my obstalce and it just didnt work, im lost. there isnt a whole lot that I could do with my little to none knowledge of python. I just want my square to stop when it hits the obstacle and it is very hard to get it to do that, I tried everything I know, please help. putting OL in the obstcle coordinates did not seem to help anything, and I just want to make it so that all the shapes I make are masked as "OL" so that I can have complex obstacles or moving ones that stop the player. but I would also like for the player to be able to move away from the obstacle as well.