0
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.

1 Answers1

0

Read *How do I detect collision in pygame?. I suggest to use pygame.Rect objects and colliderect for the collision detection. Create pygame.Rect objects for the player and the obstacle:

player_rect = pygame.Rect(200, 200, 60, 60)
obstacle_rect = pygame.Rect(100, 100, 100, 10)

Test for collision when the player has been moved and restrict the player's boundaries to the boundaries of the obstacle:

if keys[pygame.K_LEFT] and player_rect.left > 0:
    player_rect.x -= vel
    if player_rect.colliderect(obstacle_rect):
        player_rect.left = obstacle_rect.right

Complete example:

import pygame

pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
blue = (20,40,200)
gray = (100,100,100)
player_rect = pygame.Rect(200, 200, 60, 60)
obstacle_rect = pygame.Rect(100, 100, 100, 10)
vel = 5

def player():
    pygame.draw.rect(screen, blue, player_rect)
def obst():
    pygame.draw.rect(screen, gray, pygame.Rect(obstacle_rect))

run=True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT] and player_rect.left > 0:
        player_rect.x -= vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.left = obstacle_rect.right
    if keys[pygame.K_RIGHT] and player_rect.right < 800:
        player_rect.x += vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.right = obstacle_rect.left
    if keys[pygame.K_UP] and player_rect.top > 0:
        player_rect.y -= vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.top = obstacle_rect.bottom
    if keys[pygame.K_DOWN] and player_rect.bottom < 600:
        player_rect.y += vel
        if player_rect.colliderect(obstacle_rect):
            player_rect.bottom = obstacle_rect.top

    screen.fill((255,255,255))
    player()
    obst()
    pygame.display.update()
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174