Fix block pull out of bounds check

This commit is contained in:
PAlexanderFranklin 2023-05-18 00:56:24 -07:00
parent 5786ac0d32
commit 0cff07658f
3 changed files with 9 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import pygame
import uuid
from globals import *
from utilities import *
from enemies import *
class Block:
@ -15,7 +16,7 @@ class Block:
self.sprite = pygame.Rect((x*tile) + 2, (y*tile) + 2, tile - 2, tile - 2)
def pushed(self, x, y, caller, pusher):
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
if checkOOBounds((self.x + x, self.y + y)):
raise Exception("Cannot push off edge!")
if gameMap[self.x + x][self.y + y]:
gameMap[self.x + x][self.y + y].pushed(x, y, self, pusher)
@ -29,6 +30,8 @@ class Block:
def pulled(self, x, y):
for i in range(-1,2):
for j in range(-1,2):
if checkOOBounds((self.x + i, self.y + j)):
continue
if hasattr(gameMap[self.x + i][self.y + j], "ENEMY"):
raise Exception("Cannot pull a block that is touching an enemy")
gameMap[self.x][self.y] = False

View File

@ -20,8 +20,7 @@ def buildPathMap(x, y):
for j in range(-1,2):
newX = tile[0] + i
newY = tile[1] + j
outOfBounds = newX > tileCountx - 1 or newY > tileCounty - 1 or newX < 0 or newY < 0
if (i != 0 or j != 0) and not outOfBounds and not pathMap[newX][newY]:
if (i != 0 or j != 0) and not checkOOBounds((newX,newY)) and not pathMap[newX][newY]:
pathMap[newX][newY] = (newX,newY,(-i,-j))
if not gameMap[newX][newY]:
newTiles.add(pathMap[newX][newY])

4
utilities.py Normal file
View File

@ -0,0 +1,4 @@
from globals import *
def checkOOBounds(pos):
return pos[0] > tileCountx - 1 or pos[1] > tileCounty - 1 or pos[0] < 0 or pos[1] < 0