This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
forked from brunoxd13/ag-tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjogo.py
120 lines (87 loc) · 3.46 KB
/
jogo.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import random, time, pygame, sys
from pygame.locals import *
import tetris as t
import algoritmoGenetico as ag
size = [640, 480]
screen = pygame.display.set_mode((size[0], size[1]))
#add individuo,
def jogar(individuo, multVel, scoreMax = 20000, jogoRapido = False):
t.FPS = int(multVel)
t.main()
board = t.getBlankBoard()
lastFallTime = time.time()
score = 0
level, fallFreq = t.calculateLevelAndFallFreq(score)
fallingPiece = t.getNewPiece()
nextPiece = t.getNewPiece()
individuo.calcularMelhorJogada(board, fallingPiece)
pecasJogadas = 0
linhasDestruidas = [0,0,0,0] #combos
vivo = True
ganhou = False
while vivo: #game loop
#process
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
if fallingPiece == None:
# nenhuma peca caindo, gera uma nova peca
fallingPiece = nextPiece
nextPiece = t.getNewPiece()
#decide a melhor jogada baseado no que acha (pesos)
individuo.calcularMelhorJogada(board, fallingPiece, jogoRapido)
pecasJogadas +=1
score += 1
lastFallTime = time.time() # reseta lastFallTime
if not t.isValidPosition(board, fallingPiece):
#nao existe possiçao que caiba na tela
vivo = False
if jogoRapido or time.time() - lastFallTime > fallFreq:
if not t.isValidPosition(board, fallingPiece, adjY=1):
# A peca caiu, adiciona ela para o tabuleiro
t.addToBoard(board, fallingPiece)
numLines = t.removeCompleteLines(board)
if(numLines == 1):
score += 40
linhasDestruidas[0] += 1
elif (numLines == 2):
score += 120
linhasDestruidas[1] += 1
elif (numLines == 3):
score += 300
linhasDestruidas[2] += 1
elif (numLines == 4):
score += 1200
linhasDestruidas[3] += 1
fallingPiece = None
else:
# A peca ainda esta caindo, move ela para baixo
fallingPiece['y'] += 1
lastFallTime = time.time()
if not jogoRapido:
desenharNaTela(board,score,level,nextPiece, fallingPiece)
#condiçao de parada
if score > scoreMax:
vivo = False
ganhou = True
# retorna [numero de pecas, linhas destruidas(combos de 1,2,3,4), score normal de tetris, ganhou]
gameState = [pecasJogadas, linhasDestruidas ,score, ganhou]
return(gameState)
def desenharNaTela(board,score,level,nextPiece,fallingPiece):
t.DISPLAYSURF.fill(t.BGCOLOR)
t.drawBoard(board)
t.drawStatus(score, level)
t.drawNextPiece(nextPiece)
if fallingPiece != None:
t.drawPiece(fallingPiece)
pygame.display.update()
t.FPSCLOCK.tick(t.FPS)
if __name__ == '__main__':
numPesos = 7
pesos0 = numPesos*[0]
for k2 in range (0,numPesos):
pesos0[k2] = 2*random.random()-1
pesos0= [-0.97, 5.47, -13.74, -0.73, 7.99, -0.86, -0.72]
indiv = ag.Individuo(pesos0)
print(jogar(indiv,300,scoreMax = 200000))