Compare commits
3 Commits
99f5f4bbfa
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
dedf852090 | ||
|
5d4b3c55c6 | ||
|
0dd62af930 |
@@ -23,4 +23,4 @@ Other key bindings are:
|
||||
|
||||
## 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>`
|
||||
|
53
maxman.py
53
maxman.py
@@ -1,10 +1,10 @@
|
||||
#!/bin/env python3
|
||||
|
||||
import abc
|
||||
import argparse
|
||||
import dataclasses
|
||||
import math
|
||||
import random
|
||||
import sys
|
||||
from typing import List
|
||||
|
||||
import pygame
|
||||
@@ -115,11 +115,14 @@ class AI:
|
||||
return
|
||||
|
||||
target = self.target
|
||||
if random.random() * 100 < 10:
|
||||
if (target and target.size > self.man.size) or random.random() * 100 < 10:
|
||||
target = None
|
||||
|
||||
if target is None:
|
||||
if targets := [e for e in self.enemies or [] if e.size < self.man.size]:
|
||||
targets = [
|
||||
e for e in self.enemies or [] if e.alive and e.size < self.man.size
|
||||
]
|
||||
if targets:
|
||||
target = random.choice(targets)
|
||||
|
||||
if target is None:
|
||||
@@ -157,10 +160,9 @@ dt = 0
|
||||
|
||||
# set up board
|
||||
sw, sh = screen.get_width(), screen.get_height()
|
||||
ai = AI()
|
||||
maxman = Man(pygame.Vector2(sw / 4, sh / 4), name="Maxman")
|
||||
blackman = Man(
|
||||
pygame.Vector2(sw * 0.75, sh * 0.75),
|
||||
pygame.Vector2(sw * 0.75, sh * 0.25),
|
||||
name="Blackman",
|
||||
color="black",
|
||||
key_up=pygame.K_i,
|
||||
@@ -168,11 +170,41 @@ blackman = Man(
|
||||
key_left=pygame.K_j,
|
||||
key_right=pygame.K_l,
|
||||
)
|
||||
players = [maxman, blackman]
|
||||
chrisman = Man(
|
||||
pygame.Vector2(sw * 0.25, sh * 0.75),
|
||||
name="Chrisman",
|
||||
color="red",
|
||||
key_up=pygame.K_UP,
|
||||
key_down=pygame.K_DOWN,
|
||||
key_left=pygame.K_LEFT,
|
||||
key_right=pygame.K_RIGHT,
|
||||
)
|
||||
manman = Man(
|
||||
pygame.Vector2(sw * 0.75, sh * 0.75),
|
||||
name="Manman",
|
||||
color="green",
|
||||
key_up=pygame.K_KP_8,
|
||||
key_down=pygame.K_KP_2,
|
||||
key_left=pygame.K_KP_4,
|
||||
key_right=pygame.K_KP_6,
|
||||
)
|
||||
players = [maxman, blackman, chrisman, manman]
|
||||
food = Food.new()
|
||||
|
||||
if any([arg in ["--ai", "-a"] for arg in sys.argv]):
|
||||
ai.hook(blackman)
|
||||
parser = argparse.ArgumentParser()
|
||||
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:
|
||||
# check recent events
|
||||
@@ -206,6 +238,7 @@ while running:
|
||||
for p in players:
|
||||
for op in players:
|
||||
if p != op and p.can_eat(op):
|
||||
op.alive = False
|
||||
players.remove(op)
|
||||
|
||||
if p.can_eat(food):
|
||||
@@ -222,7 +255,9 @@ while running:
|
||||
)
|
||||
|
||||
else:
|
||||
ai.decide()
|
||||
for ai in ais:
|
||||
ai.decide()
|
||||
|
||||
for p in players:
|
||||
p.handle_input(keys, dt)
|
||||
|
||||
|
Reference in New Issue
Block a user