Browse Source

Wrote function to check if a mouse cooardinate is an acceptable

building location
master
Elis Axelsson 3 years ago
parent
commit
d3fa42e10e
1 changed files with 32 additions and 0 deletions
  1. +32
    -0
      classes/map.py

+ 32
- 0
classes/map.py View File

@ -33,6 +33,38 @@ class Map(object):
columnNumber = 0
rowNumber += 1
def canBuildOnCooardinate(self, cooardinates, towers):
# Extract Rects from towers
towerRects = list(map(lambda x: x.rect, towers))
# Set up a rect for the mouse
mouseRect = pygame.Rect(cooardinates, (1, 1))
# Do collision detection of the mouse and all the towers
towerKey = mouseRect.collidelist(towerRects)
# If we got a tower-key, mouse collided with a tower which makes the
# spot occupied so we can't build there.
if towerKey != -1:
return False
# Calculate which grid location the user clicked in
column = int(cooardinates[0] / self.settings.gridSize)
row = int(cooardinates[1] / self.settings.gridSize)
# Look up the data of that grid location
try:
mapData = self.settings.map[row][column]
except IndexError:
# If out of range, just deny
return False
# If mapData is == 1, allow building
if mapData == 1:
return (row, column)
return False
def draw(self, screen):
screen.blit(
self.surface,


Loading…
Cancel
Save