Add new enemy type that doesn't work

This commit is contained in:
PAlexanderFranklin 2023-05-18 01:57:15 -07:00
parent 5973675c51
commit 0add18a31d
4 changed files with 65 additions and 8 deletions

View File

@ -86,6 +86,7 @@ def main():
return image
frame_0 = get_image(sprite_sheet_image, 0, 0, 32, 32, (tile-2)/32, black)
frame_1 = get_image(sprite_sheet_image, 0, 32, 32, 32, (tile-2)/32, black)
frame_4 = get_image(sprite_sheet_image, 32, 0, 32, 32, (tile-2)/32, black)
game_font = pygame.font.Font("freesansbold.ttf",32)
@ -113,15 +114,18 @@ def main():
continue
elif isinstance(spot, Block):
pygame.draw.rect(screen, spot.color, spot.sprite)
elif isinstance(spot, Smart):
screen.blit(frame_1, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
elif isinstance(spot, Enemy):
screen.blit(frame_4, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
spot.renderPos = ((spot.x - spot.renderPos[0])/3 + spot.renderPos[0], (spot.y - spot.renderPos[1])/3 + spot.renderPos[1])
elif isinstance(spot, Player):
screen.blit(frame_0, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
spot.renderPos = ((spot.x - spot.renderPos[0])/3 + spot.renderPos[0], (spot.y - spot.renderPos[1])/3 + spot.renderPos[1])
scoreSurface = game_font.render(f'player {spot.id} kills: {spot.kills} lives: {spot.lives}', False, black)
screen.blit(scoreSurface, (500*int(spot.id) - 400, screen_height - bottomBarHeight + 20))
try:
spot.renderPos = ((spot.x - spot.renderPos[0])/3 + spot.renderPos[0], (spot.y - spot.renderPos[1])/3 + spot.renderPos[1])
except:
pass
pygame.display.flip()
clock.tick(60)

View File

@ -24,8 +24,9 @@ class Enemy:
self.x = x
self.y = y
self.renderPos = (x,y)
self.AITime = random.randint(350, 600)
# self.AITime = random.randint(350, 600)
self.AITime = random.randint(10, 50)
self.deathColor = white
def die(self):
del enemies[self.id]
@ -34,7 +35,7 @@ class Enemy:
if hasattr(pusher, "PLAYER") and hasattr(caller, "BLOCK"):
if checkOOBounds((self.x + x, self.y + y)) or hasattr(gameMap[self.x + x][self.y + y], "BLOCK"):
pusher.kills += 1
caller.color = white
caller.color = self.deathColor
self.die()
return
raise Exception("Not crushing enemy!")
@ -82,3 +83,34 @@ class Enemy:
self.AITime = random.randint(150, 200)
except Exception as error:
pass
class Smart(Enemy):
def __init__(self, id, x, y):
Enemy.__init__(self, id, x, y)
self.deathColor = red
self.trackedPlayer = False
self.trackingCounter = 0
def runAI(self):
self.AITime -= 1
if self.AITime < 1:
try:
if self.trackedPlayer and self.trackedPlayer.alive:
fastDir = self.trackedPlayer.pathMap[self.x][self.y][2]
# movementx = random.choices([-1, 0, 1], weightsx)[0]
# movementy = random.choices([-1, 0, 1], weightsy)[0]
movementx = fastDir[0]
movementy = fastDir[1]
# self.move(movementx, movementy)
self.trackingCounter -= 1
if self.trackingCounter < 1:
self.AITime = random.randint(500, 1000)
self.trackedPlayer = False
self.AITime = random.randint(10, 15)
else:
closestPlayer = findClosestPlayer(self.x, self.y)
self.trackedPlayer = closestPlayer["player"]
self.trackingCounter = 8
self.AITime = random.randint(10, 15)
except Exception as error:
print(error)

View File

@ -35,6 +35,19 @@ def generateMap():
except Exception as error:
continue
for i in range(1):
for j in range(50):
try:
spot = [random.randint(0, tileCountx - 1), random.randint(0, tileCounty - 1)]
if gameMap[spot[0]][spot[1]]:
raise Exception("spot taken!")
newEnemy = Smart(uuid.uuid4(), spot[0], spot[1])
gameMap[spot[0]][spot[1]] = newEnemy
enemies[newEnemy.id] = newEnemy
break
except Exception as error:
continue
for id, player in players.items():
maxTries = 5
for i in range(maxTries + 1):

View File

@ -14,7 +14,7 @@ def buildPathMap(x, y):
pathMap[x][y] = (x,y,(0,0))
currentTiles = [pathMap[x][y]]
newTiles = set()
while len(currentTiles) > 0:
while True:
for tile in currentTiles:
for i in range(-1,2):
for j in range(-1,2):
@ -24,6 +24,8 @@ def buildPathMap(x, y):
pathMap[newX][newY] = (newX,newY,(-i,-j))
if not gameMap[newX][newY]:
newTiles.add(pathMap[newX][newY])
if len(currentTiles) < 1:
break
currentTiles = list(newTiles)
newTiles = set()
return pathMap
@ -40,6 +42,7 @@ class Player:
self.PLAYER = True
self.kills = 0
self.lives = 3
self.alive = True
commands = {
"n": lambda: self.move(0,-1),
@ -118,6 +121,7 @@ class Player:
for i in range(maxTries + 1):
if self.lives < 1:
del players[self.id]
self.alive = False
break
if i == maxTries:
raise Exception("No spots found for player!")
@ -129,6 +133,10 @@ class Player:
self.x = spot[0]
self.y = spot[1]
self.lives -= 1
try:
self.buildOwnPathMap()
except Exception as error:
print(error)
break
except Exception as error:
continue