import pygame class Wall: def __init__(self, x, y, width, height): self.x = x self.y = y self.height = height self.width = width self.halfH = height / 2 self.halfW = width / 2 def isPointOnWall(self, x, y): xIsInBetween = self.x - self.halfW <= x and x <= self.x + self.halfW yIsInBetween = self.y - self.halfH <= y and y <= self.y + self.halfH return xIsInBetween and yIsInBetween # Calculates the coordinates that the object should be at if it was in the # wall. def pushedOutCoordinates(self, fromX, fromY, currentX, currentY): if not self.isPointOnWall(currentX, currentY): return [currentX, currentY] # If we were to the left of the wall before... if fromX <= self.x - self.halfW: # Then stay to the left of the wall. return [self.x - self.halfW, currentY] # If we were to the right of the wall before... elif fromX >= self.x + self.halfW: # Then stay to the right of the wall. return [self.x + self.halfW, currentY] # If we were to the top of the wall before... elif fromY <= self.y - self.halfH: # Then stay on top of the wall. return [currentX, self.y - self.halfH] # If we were to the bottom of the wall before... elif fromY >= self.y + self.halfH: # Then stay at the bottom of the wall. return [currentX, self.y + self.halfH] return [currentX, currentY] def draw(self, screen): pygame.draw.rect( screen, (0, 0, 0), (self.x - self.halfW, self.y - self.halfH, self.width, self.height) )