Add support for multiple AI-controlled players
This commit is contained in:
@@ -23,4 +23,4 @@ Other key bindings are:
|
|||||||
|
|
||||||
## Playing against AI
|
## Playing against AI
|
||||||
|
|
||||||
If you want to play against the AI instead of other players, supply the corresponding CLI flag: `pipenv run ./maxman.py --ai`
|
If you want to play against the AI instead of other players, supply the corresponding CLI flag: `pipenv run ./maxman.py --ai <number of AI players>`
|
||||||
|
23
maxman.py
23
maxman.py
@@ -1,10 +1,10 @@
|
|||||||
#!/bin/env python3
|
#!/bin/env python3
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
|
import argparse
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
import sys
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
@@ -157,7 +157,6 @@ dt = 0
|
|||||||
|
|
||||||
# set up board
|
# set up board
|
||||||
sw, sh = screen.get_width(), screen.get_height()
|
sw, sh = screen.get_width(), screen.get_height()
|
||||||
ai = AI()
|
|
||||||
maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman")
|
maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman")
|
||||||
blackman = Man(
|
blackman = Man(
|
||||||
pygame.Vector2(sw * 0.75, sh * 0.25),
|
pygame.Vector2(sw * 0.75, sh * 0.25),
|
||||||
@@ -189,8 +188,20 @@ manman = Man(
|
|||||||
players = [maxman, blackman, chrisman, manman]
|
players = [maxman, blackman, chrisman, manman]
|
||||||
food = Food.new()
|
food = Food.new()
|
||||||
|
|
||||||
if any([arg in ["--ai", "-a"] for arg in sys.argv]):
|
parser = argparse.ArgumentParser()
|
||||||
ai.hook(blackman)
|
parser.add_argument(
|
||||||
|
"--ai",
|
||||||
|
type=int,
|
||||||
|
choices=[0, 1, 2, 3, 4],
|
||||||
|
default=1,
|
||||||
|
help="number of AI players",
|
||||||
|
)
|
||||||
|
args = parser.parse_args()
|
||||||
|
ais = []
|
||||||
|
for i in range(args.ai):
|
||||||
|
ai = AI()
|
||||||
|
ai.hook(players[-i - 1])
|
||||||
|
ais.append(ai)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
# check recent events
|
# check recent events
|
||||||
@@ -240,7 +251,9 @@ while running:
|
|||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
ai.decide()
|
for ai in ais:
|
||||||
|
ai.decide()
|
||||||
|
|
||||||
for p in players:
|
for p in players:
|
||||||
p.handle_input(keys, dt)
|
p.handle_input(keys, dt)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user