add circle sprites, refactor movement randomization
This commit is contained in:
parent
d169ed2fbd
commit
5ecef4efba
@ -114,10 +114,8 @@ def main():
|
|||||||
continue
|
continue
|
||||||
elif isinstance(spot, Block):
|
elif isinstance(spot, Block):
|
||||||
pygame.draw.rect(screen, spot.color, spot.sprite)
|
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):
|
elif isinstance(spot, Enemy):
|
||||||
screen.blit(frame_4, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
screen.blit(spot.sprite, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||||
elif isinstance(spot, Player):
|
elif isinstance(spot, Player):
|
||||||
screen.blit(frame_0, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
screen.blit(frame_0, ((tile*spot.renderPos[0])+2, (tile*spot.renderPos[1])+2))
|
||||||
scoreSurface = game_font.render(f'player {spot.id} kills: {spot.kills} lives: {spot.lives}', False, black)
|
scoreSurface = game_font.render(f'player {spot.id} kills: {spot.kills} lives: {spot.lives}', False, black)
|
||||||
|
49
enemies.py
49
enemies.py
@ -17,6 +17,25 @@ def findClosestPlayer(x, y):
|
|||||||
closestPlayerPosition = {"difference": difference, "distance": distance, "player": player}
|
closestPlayerPosition = {"difference": difference, "distance": distance, "player": player}
|
||||||
return closestPlayerPosition
|
return closestPlayerPosition
|
||||||
|
|
||||||
|
def randomizeMovement(weightsx, weightsy, diff, wrongChance):
|
||||||
|
weightsx = [-diff[0], abs(diff[1]), diff[0]]
|
||||||
|
if weightsx[0] < 0:
|
||||||
|
weightsx[0] = weightsx[2]*wrongChance
|
||||||
|
elif weightsx[2] < 0:
|
||||||
|
weightsx[2] = weightsx[0]*wrongChance
|
||||||
|
else:
|
||||||
|
weightsx[0] = max(weightsx[1]/(15.1-weightsx[1]*0.5), 1)
|
||||||
|
weightsx[2] = weightsx[0]
|
||||||
|
weightsy = [-diff[1], abs(diff[0]), diff[1]]
|
||||||
|
if weightsy[0] < 0:
|
||||||
|
weightsy[0] = weightsy[2]*wrongChance
|
||||||
|
elif weightsy[2] < 0:
|
||||||
|
weightsy[2] = weightsy[0]*wrongChance
|
||||||
|
else:
|
||||||
|
weightsy[0] = max(weightsy[1]/(15.1-weightsy[1]*0.5), 1)
|
||||||
|
weightsy[2] = weightsy[0]
|
||||||
|
return (weightsx, weightsy)
|
||||||
|
|
||||||
class Enemy:
|
class Enemy:
|
||||||
def __init__(self, id, x, y):
|
def __init__(self, id, x, y):
|
||||||
self.id = id
|
self.id = id
|
||||||
@ -27,6 +46,8 @@ class Enemy:
|
|||||||
# self.AITime = random.randint(350, 600)
|
# self.AITime = random.randint(350, 600)
|
||||||
self.AITime = random.randint(10, 50)
|
self.AITime = random.randint(10, 50)
|
||||||
self.deathColor = white
|
self.deathColor = white
|
||||||
|
self.sprite = pygame.Surface((32,32), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.sprite, white, (16,16), 10)
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
del enemies[self.id]
|
del enemies[self.id]
|
||||||
@ -61,22 +82,12 @@ class Enemy:
|
|||||||
if self.AITime < 1:
|
if self.AITime < 1:
|
||||||
try:
|
try:
|
||||||
diff = findClosestPlayer(self.x, self.y)["difference"]
|
diff = findClosestPlayer(self.x, self.y)["difference"]
|
||||||
weightsx = [-diff[0], abs(diff[1]), diff[0]]
|
weightsx = [1,1,1]
|
||||||
if weightsx[0] < 0:
|
weightsy = [1,1,1]
|
||||||
weightsx[0] = weightsx[2]/6
|
randomWeights = randomizeMovement(weightsx, weightsy, diff, 0.15)
|
||||||
elif weightsx[2] < 0:
|
print(randomWeights, diff)
|
||||||
weightsx[2] = weightsx[0]/6
|
weightsx = randomWeights[0]
|
||||||
else:
|
weightsy = randomWeights[1]
|
||||||
weightsx[0] = weightsx[1]/8
|
|
||||||
weightsx[2] = weightsx[1]/8
|
|
||||||
weightsy = [-diff[1], abs(diff[0]), diff[1]]
|
|
||||||
if weightsy[0] < 0:
|
|
||||||
weightsy[0] = weightsy[2]/6
|
|
||||||
elif weightsy[2] < 0:
|
|
||||||
weightsy[2] = weightsy[0]/6
|
|
||||||
else:
|
|
||||||
weightsy[0] = weightsy[1]/8
|
|
||||||
weightsy[2] = weightsy[1]/8
|
|
||||||
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
||||||
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
||||||
self.move(movementx, movementy)
|
self.move(movementx, movementy)
|
||||||
@ -90,6 +101,8 @@ class Smart(Enemy):
|
|||||||
self.deathColor = red
|
self.deathColor = red
|
||||||
self.trackedPlayer = False
|
self.trackedPlayer = False
|
||||||
self.trackingCounter = 0
|
self.trackingCounter = 0
|
||||||
|
self.sprite = pygame.Surface((32,32), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(self.sprite, red, (16,16), 12)
|
||||||
|
|
||||||
def runAI(self):
|
def runAI(self):
|
||||||
self.AITime -= 1
|
self.AITime -= 1
|
||||||
@ -107,6 +120,10 @@ class Smart(Enemy):
|
|||||||
fastDir = pathMap[self.x][self.y][2]
|
fastDir = pathMap[self.x][self.y][2]
|
||||||
weightsx[1 + fastDir[0]] = 30
|
weightsx[1 + fastDir[0]] = 30
|
||||||
weightsy[1 + fastDir[1]] = 30
|
weightsy[1 + fastDir[1]] = 30
|
||||||
|
else:
|
||||||
|
randomWeights = randomizeMovement(weightsx, weightsy, self.trackedPlayer["difference"], 0.32)
|
||||||
|
weightsx = randomWeights[0]
|
||||||
|
weightsy = randomWeights[1]
|
||||||
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
movementx = random.choices([-1, 0, 1], weightsx)[0]
|
||||||
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
||||||
self.move(movementx, movementy)
|
self.move(movementx, movementy)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user