-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphical_two_ai.py
95 lines (77 loc) · 3.47 KB
/
graphical_two_ai.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
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
from tetris.tetris import Tetris, Tile
import pygame, sys, random, time
from drawing.color import *
from drawing.drawing import *
FPS = 60
WINDOWWIDTH = 960
WINDOWHEIGHT = 600
MAXCOLORS = 360
def main():
global FPSCLOCK, BASICFONT, BIGFONT, DISPLAYSURF, drawing
pygame.init()
FPSCLOCK = pygame.time.Clock()
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
BIGFONT = pygame.font.Font('freesansbold.ttf', 72)
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
drawing = Drawing(pygame, DISPLAYSURF, WINDOWWIDTH, WINDOWHEIGHT)
# play music
if len(sys.argv) < 2 or not sys.argv[1] == "--silent":
musicFile = 'resources/music.mp3'
pygame.mixer.init()
pygame.mixer.music.load(musicFile)
pygame.mixer.music.play(-1)
pygame.display.set_caption("Enomino")
playMenu(drawing, "Enomino")
while True:
response = playGame()
playMenu(drawing, response)
from ai.ai import AI
from ai.algorithms.mlAi import choosePosition as func1
from ai.algorithms.holyNeighborAi import choosePosition as func2
def playGame():
game = Tetris(numColors=MAXCOLORS, seed=int(time.time()))
ai = AI(func1)
game2 = Tetris(numColors=MAXCOLORS, seed=int(time.time()))
ai2 = AI(func2)
render(game, game2)
pressedKeys = [-1, -1, -1, -1] # up, down, left, right
while not game.lost and not game2.lost: # game loop ends when game is lost
for event in pygame.event.get(): # event handling loop
if event.type == pygame.QUIT:
terminate() # exit game
# increment time (move blocks down)
ai.aiSequence(game)
ai2.aiSequence(game2)
render(game, game2)
FPSCLOCK.tick(FPS)
return "Player 2 (Will) won" if game.lost else "Player 1 (Bill) won"
def playMenu(drawing, text):
drawing.fill()
drawing.drawMenu(text, BIGFONT, BASICFONT)
ready = False
while not ready: # start screen loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate() # exit game
if event.type == pygame.KEYDOWN:
ready = True
pygame.display.update()
FPSCLOCK.tick()
def render(game, aiGame):
drawing.fill()
drawing.drawText(text="AI #1 (Bill):", font=BASICFONT, location=(50, 10), center=False)
drawing.drawText(text="AI #2 (Will):", font=BASICFONT, location=(WINDOWWIDTH/2 + 50, 10), center=False)
drawing.drawBoard(board=game.getBoard(), numColors=MAXCOLORS, location=(50, 50), bordersize=5, boxsize=25)
drawing.drawBoard(board=aiGame.getBoard(), numColors=MAXCOLORS, location=(WINDOWWIDTH/2 + 50, 50), bordersize=5, boxsize=25)
drawing.drawNextPiece(piece=game.PIECES[game.next - 1], numColors=MAXCOLORS, font=BASICFONT, color=game.nextColor, location=(WINDOWWIDTH/2 - 150, 150), bordersize=5, boxsize=25)
drawing.drawNextPiece(piece=aiGame.PIECES[aiGame.next - 1], numColors=MAXCOLORS, font=BASICFONT, color=aiGame.nextColor, location=(WINDOWWIDTH - 150, 150), bordersize=5, boxsize=25)
drawing.drawStatus(score=game.numTurns, level=game.numLines, piece=game.next, font=BASICFONT, location=(WINDOWWIDTH/2 - 150, 50))
drawing.drawStatus(score=aiGame.numTurns, level=aiGame.numLines, piece=aiGame.next, font=BASICFONT, location=(WINDOWWIDTH - 150, 50))
pygame.display.update()
def terminate():
pygame.quit()
sys.exit()
if __name__ == "__main__":
print("Use --silent for mute")
main()