Skip to content

Commit 92b51d6

Browse files
author
renzon
committed
Engine de console funcionando no Linux
1 parent 4aace91 commit 92b51d6

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed

executor_de_testes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
import unittest
5+
import sys
6+
import os
7+
8+
ROOT_PATH = os.path.dirname(__file__)
9+
10+
if __name__ == '__main__':
11+
tests = unittest.TestLoader().discover(ROOT_PATH, "*.py")
12+
result = unittest.TextTestRunner().run(tests)
13+
if not result.wasSuccessful():
14+
sys.exit(1)

placa_grafica.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, unicode_literals
3+
import os
4+
import time
5+
FIM='''|------------------------------------------------------------------------------|
6+
| |
7+
| |
8+
| PPPP Y Y TTTTT H H OOO NN N |
9+
| P P Y Y T H H O O N N N |
10+
| PPPP Y T HHHHH O O N N N |
11+
| P Y T H H O O N NN |
12+
| P Y T H H OOO N N |
13+
| |
14+
| |
15+
| |
16+
| BBBB I RRRR DDDD SSSS |
17+
| B B I R R D D S |
18+
| BBBB I RRRR D D SSS |
19+
| B B I R R D D S |
20+
| BBBB I R R DDDD SSSS |
21+
| |
22+
| |
23+
| |
24+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
25+
'''
26+
27+
LARGURA = 80
28+
ALTURA = 20
29+
30+
31+
def apagar_tela():
32+
os.system('clear')
33+
34+
35+
try:
36+
apagar_tela()
37+
except:
38+
apagar_tela = lambda: os.system('cls')
39+
40+
41+
def animar(fase, passo=0.1, delta_t=0.1):
42+
tempo = 0
43+
while not fase.acabou(tempo):
44+
time.sleep(passo)
45+
apagar_tela()
46+
pontos_cartesianos = fase.calcular_pontos(tempo)
47+
print(desenhar(*pontos_cartesianos))
48+
tempo += delta_t
49+
print(FIM)
50+
print(fase.status())
51+
52+
53+
def normalizar_coordenadas(x, y):
54+
return x, ALTURA - y - 1
55+
56+
57+
def esta_dentro_da_tela(x, y):
58+
return 0 < x < (LARGURA - 1) and 0 < y < (ALTURA - 1)
59+
60+
61+
def escolher_caracter_limitrofe(x, y):
62+
if x == 0 or x == LARGURA - 1:
63+
return '|'
64+
if y == 0:
65+
return '-'
66+
return 'T'
67+
68+
69+
def escolher_caracter(x, y, *pontos_cartesianos):
70+
for ponto in pontos_cartesianos:
71+
x_normalizdo, y_normalizado = normalizar_coordenadas(ponto.x, ponto.y)
72+
if x == x_normalizdo and y == y_normalizado:
73+
return ponto.caracter
74+
return ' '
75+
76+
77+
def desenhar(*pontos_cartesianos):
78+
frame = ''
79+
for y in range(ALTURA):
80+
for x in range(LARGURA):
81+
if esta_dentro_da_tela(x, y):
82+
frame += escolher_caracter(x, y, *pontos_cartesianos)
83+
else:
84+
frame += escolher_caracter_limitrofe(x, y)
85+
frame += os.linesep
86+
return frame
87+
88+
89+
def main():
90+
global Ponto, Fase
91+
92+
class Ponto():
93+
def __init__(self, caracter):
94+
self.caracter = caracter
95+
self(0)
96+
97+
def __call__(self, tempo):
98+
self.y = round(ALTURA * tempo / 5) if tempo < 5 else round(ALTURA * (10 - tempo) / 5)
99+
self.x = round(LARGURA * tempo / 10)
100+
return self
101+
102+
class Fase():
103+
def __init__(self):
104+
self.p = Ponto('>')
105+
106+
def calcular_pontos(self, tempo):
107+
return [self.p(tempo)]
108+
109+
def acabou(self, tempo):
110+
return tempo > 10
111+
112+
def status(self):
113+
return 'Você ganhou'
114+
115+
animar(Fase())
116+
117+
118+
if __name__ == '__main__':
119+
main()
120+

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, unicode_literals
3+

tests/testes_placa_grafica.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import, unicode_literals
3+
from unittest.case import TestCase
4+
import placa_grafica
5+
6+
7+
class TestesDoMotor(TestCase):
8+
def teste_inverter_coordenadas(self):
9+
self.assertTupleEqual((0, placa_grafica.ALTURA-1), placa_grafica.normalizar_coordenadas(0, 0))
10+
self.assertTupleEqual((3, placa_grafica.ALTURA - 2), placa_grafica.normalizar_coordenadas(3, 1))
11+
self.assertTupleEqual((10, 0), placa_grafica.normalizar_coordenadas(10, placa_grafica.ALTURA-1))
12+
13+
def teste_desenhar_frame_vazio(self):
14+
self.maxDiff=None
15+
class PontoCartesiano():
16+
def __init__(self, x, y, caracter):
17+
self.caracter = caracter
18+
self(x,y)
19+
20+
def __call__(self, x,y):
21+
self.y = y
22+
self.x = x
23+
24+
25+
self.assertEqual(FRAMES[0], placa_grafica.desenhar())
26+
pontoA = PontoCartesiano(1, 1, 'A')
27+
self.assertEqual(FRAMES[1], placa_grafica.desenhar(pontoA))
28+
pontoA.x=2
29+
self.assertEqual(FRAMES[2], placa_grafica.desenhar(pontoA))
30+
pontoB=PontoCartesiano(1,1,'B')
31+
pontoA.y=2
32+
self.assertEqual(FRAMES[3], placa_grafica.desenhar(pontoA,pontoB))
33+
pontoB(2,2)
34+
self.assertEqual(FRAMES[4], placa_grafica.desenhar(pontoA,pontoB))
35+
pontoB(placa_grafica.LARGURA-1,placa_grafica.ALTURA-1)
36+
self.assertEqual(FRAMES[4], placa_grafica.desenhar(pontoA,pontoB))
37+
38+
39+
FRAMES =['''|------------------------------------------------------------------------------|
40+
| |
41+
| |
42+
| |
43+
| |
44+
| |
45+
| |
46+
| |
47+
| |
48+
| |
49+
| |
50+
| |
51+
| |
52+
| |
53+
| |
54+
| |
55+
| |
56+
| |
57+
| |
58+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
59+
''',
60+
61+
'''|------------------------------------------------------------------------------|
62+
| |
63+
| |
64+
| |
65+
| |
66+
| |
67+
| |
68+
| |
69+
| |
70+
| |
71+
| |
72+
| |
73+
| |
74+
| |
75+
| |
76+
| |
77+
| |
78+
| |
79+
|A |
80+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
81+
'''
82+
,
83+
84+
'''|------------------------------------------------------------------------------|
85+
| |
86+
| |
87+
| |
88+
| |
89+
| |
90+
| |
91+
| |
92+
| |
93+
| |
94+
| |
95+
| |
96+
| |
97+
| |
98+
| |
99+
| |
100+
| |
101+
| |
102+
| A |
103+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
104+
'''
105+
,
106+
107+
'''|------------------------------------------------------------------------------|
108+
| |
109+
| |
110+
| |
111+
| |
112+
| |
113+
| |
114+
| |
115+
| |
116+
| |
117+
| |
118+
| |
119+
| |
120+
| |
121+
| |
122+
| |
123+
| |
124+
| A |
125+
|B |
126+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
127+
''',
128+
129+
'''|------------------------------------------------------------------------------|
130+
| |
131+
| |
132+
| |
133+
| |
134+
| |
135+
| |
136+
| |
137+
| |
138+
| |
139+
| |
140+
| |
141+
| |
142+
| |
143+
| |
144+
| |
145+
| |
146+
| A |
147+
| |
148+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
149+
''',
150+
151+
'''|------------------------------------------------------------------------------|
152+
| B|
153+
| |
154+
| |
155+
| |
156+
| |
157+
| |
158+
| |
159+
| |
160+
| |
161+
| |
162+
| |
163+
| |
164+
| |
165+
| |
166+
| |
167+
| |
168+
| |
169+
| |
170+
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
171+
''']
172+
173+
174+
175+
176+
177+
178+
179+
180+
181+
182+
183+
184+
185+
186+
187+
188+
189+
190+
191+

0 commit comments

Comments
 (0)