-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (67 loc) · 2.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from demoAgent import demoPlayer
from randomAgent import randPlayer
from compAgent import *
random.seed(1)
case = 3
if case == 0: # Play with this case to get an idea of the environment
player1 = demoPlayer()
player2 = randPlayer(randAgentPath, BLUE)
elif case == 1: # Base Case
player1 = randPlayer(randAgentPath, BLUE)
player2 = randPlayer(randAgentPath1, YELLOW)
elif case == 2: # Test if your agent is rational
player1 = randPlayer(randAgentPath, BLUE)
player2 = PlayerB()
elif case == 3: # Test if your agents cooperate with each other
player1 = PlayerA()
player2 = PlayerB()
all_sprites.add(player1)
all_sprites.add(player2)
players.add(player1)
players.add(player2)
def gen_new_coin():
new_coin = coin_arr.pop(0)
coin = Coin(*new_coin)
if coin not in coins:
all_sprites.add(coin)
coins.add(coin)
# Game loop
clock = pygame.time.Clock()
while running:
dt = clock.tick(FPS)
global_time += dt
if global_time > 1000 * SEC:
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Game update
if len(coins) < N:
gen_new_coin()
all_sprites.update() ## update all objects in all_sprites Group
# When player 1 hits/collects a coin:
hits1 = pygame.sprite.spritecollide(player1, coins, True)
for hit in hits1:
player1.score += hit.value
# When player 2 hits/collects a coin:
hits2 = pygame.sprite.spritecollide(player2, coins, True)
for hit in hits2:
player2.score += hit.value
hits = pygame.sprite.groupcollide(walls, coins, False, True)
for hit in hits:
gen_new_coin()
# !! Note: collision between agents may result in negative utility, so your agents should cooperate well
if player1.rect.colliderect(player2) or player2.rect.colliderect(player1):
if (player1.rect.x != 0 and player1.rect.y != 0) and (
player2.rect.x != 0 and player2.rect.y != 0
):
player1.score -= 100
player2.score -= 100
# Game Render
screen.fill(WHITE)
all_sprites.draw(screen) ## draw all objects in all_sprites Group to the screen
pygame.display.update()
pygame.quit()
print("Score of Player 1:", player1.score)
print("Score of Player 2:", player2.score)
# print("Total Score:", player1.score + player2.score)