Add ai
This commit is contained in:
parent
43c5e2ae95
commit
d368885e94
@ -93,6 +93,9 @@ while True:
|
|||||||
|
|
||||||
for id, player in players.items():
|
for id, player in players.items():
|
||||||
player.useKeys(keys)
|
player.useKeys(keys)
|
||||||
|
|
||||||
|
for id, enemy in enemies.items():
|
||||||
|
enemy.runAI()
|
||||||
|
|
||||||
# Rendering
|
# Rendering
|
||||||
screen.fill(bg_color)
|
screen.fill(bg_color)
|
||||||
|
55
enemies.py
55
enemies.py
@ -1,15 +1,27 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import uuid
|
import uuid
|
||||||
|
import random
|
||||||
|
|
||||||
from globals import *
|
from globals import *
|
||||||
from player import *
|
from player import *
|
||||||
from blocks import *
|
from blocks import *
|
||||||
|
|
||||||
|
def findClosestPlayer(x, y):
|
||||||
|
closestPlayerPosition = {"difference": (1,1), "distance": 9999}
|
||||||
|
for id, player in players.items():
|
||||||
|
difference = (player.x - x, player.y - y)
|
||||||
|
distance = max(abs(difference[0]), abs(difference[1]))
|
||||||
|
if distance > closestPlayerPosition["distance"]:
|
||||||
|
continue
|
||||||
|
closestPlayerPosition = {"difference": difference, "distance": distance}
|
||||||
|
return closestPlayerPosition
|
||||||
|
|
||||||
class Enemy:
|
class Enemy:
|
||||||
def __init__(self, id, x, y):
|
def __init__(self, id, x, y):
|
||||||
self.id = id
|
self.id = id
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
self.AITime = random.randint(150, 200)
|
||||||
|
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
@ -24,4 +36,45 @@ class Enemy:
|
|||||||
raise Exception("Not crushing enemy!")
|
raise Exception("Not crushing enemy!")
|
||||||
|
|
||||||
def move(self, x, y):
|
def move(self, x, y):
|
||||||
pass
|
try:
|
||||||
|
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
|
||||||
|
raise Exception("Cannot move off edge!")
|
||||||
|
if gameMap[self.x + x][self.y + y]:
|
||||||
|
if isinstance(gameMap[self.x + x][self.y + y], Player):
|
||||||
|
gameMap[self.x + x][self.y + y].die()
|
||||||
|
else:
|
||||||
|
raise Exception("Path is blocked.")
|
||||||
|
gameMap[self.x][self.y] = 0
|
||||||
|
self.x += x
|
||||||
|
self.y += y
|
||||||
|
gameMap[self.x][self.y] = self
|
||||||
|
except Exception as error:
|
||||||
|
raise error
|
||||||
|
|
||||||
|
def runAI(self):
|
||||||
|
self.AITime -= 1
|
||||||
|
if self.AITime < 1:
|
||||||
|
try:
|
||||||
|
closestPlayer = findClosestPlayer(self.x, self.y)
|
||||||
|
weightsx = [-closestPlayer["difference"][0], abs(closestPlayer["difference"][1]), closestPlayer["difference"][0]]
|
||||||
|
if weightsx[0] < 0:
|
||||||
|
weightsx[0] = weightsx[2]/6
|
||||||
|
elif weightsx[2] < 0:
|
||||||
|
weightsx[2] = weightsx[0]/6
|
||||||
|
else:
|
||||||
|
weightsx[0] = weightsx[1]/8
|
||||||
|
weightsx[2] = weightsx[1]/8
|
||||||
|
weightsy = [-closestPlayer["difference"][1], abs(closestPlayer["difference"][0]), closestPlayer["difference"][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]
|
||||||
|
movementy = random.choices([-1, 0, 1], weightsy)[0]
|
||||||
|
self.move(movementx, movementy)
|
||||||
|
self.AITime = random.randint(150, 200)
|
||||||
|
except Exception as error:
|
||||||
|
pass
|
@ -18,7 +18,7 @@ class Player:
|
|||||||
"sw": lambda: self.move(-1,1),
|
"sw": lambda: self.move(-1,1),
|
||||||
"w": lambda: self.move(-1,0),
|
"w": lambda: self.move(-1,0),
|
||||||
"nw": lambda: self.move(-1,-1),
|
"nw": lambda: self.move(-1,-1),
|
||||||
"p": lambda: print(enemies),
|
"p": lambda: print("pull"),
|
||||||
}
|
}
|
||||||
self.commands = []
|
self.commands = []
|
||||||
for key in keys:
|
for key in keys:
|
||||||
@ -42,7 +42,7 @@ class Player:
|
|||||||
def move(self, x, y):
|
def move(self, x, y):
|
||||||
try:
|
try:
|
||||||
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
|
if self.x + x > tileCountx - 1 or self.y + y > tileCounty - 1 or self.x + x < 0 or self.y + y < 0:
|
||||||
raise Exception("Cannot push off edge!")
|
raise Exception("Cannot move off edge!")
|
||||||
if gameMap[self.x + x][self.y + y]:
|
if gameMap[self.x + x][self.y + y]:
|
||||||
gameMap[self.x + x][self.y + y].pushed(x, y, self, self)
|
gameMap[self.x + x][self.y + y].pushed(x, y, self, self)
|
||||||
gameMap[self.x][self.y] = 0
|
gameMap[self.x][self.y] = 0
|
||||||
@ -53,4 +53,7 @@ class Player:
|
|||||||
raise error
|
raise error
|
||||||
|
|
||||||
def pushed(self, x, y, caller, pusher):
|
def pushed(self, x, y, caller, pusher):
|
||||||
raise Exception("Cannot push other players.")
|
raise Exception("Cannot push other players.")
|
||||||
|
|
||||||
|
def die(self):
|
||||||
|
del players[self.id]
|
Loading…
Reference in New Issue
Block a user