-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invaders.py
More file actions
68 lines (57 loc) · 2.1 KB
/
Copy pathspace_invaders.py
File metadata and controls
68 lines (57 loc) · 2.1 KB
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
import os
import time
import keyboard
def tela(largura, altura, posicao_barra, posicao_c, posicao_tiro):
os.system('cls' if os.name == 'nt' else 'clear')
for y in range(altura):
for x in range(largura):
if y == altura - 1 and posicao_barra <= x < posicao_barra + 3:
print('=', end='')
elif y == posicao_c[1] and x == posicao_c[0]:
print('C', end='')
elif y == posicao_tiro[1] and x == posicao_tiro[0]:
print('|', end='')
else:
print(' ', end='')
print()
def jogar():
largura, altura = 30, 25
posicao_barra = largura // 2 - 1
posicao_c = [largura // 2, 2]
posicao_tiro = [0, 0]
tiro_ativo = False
direcao_c = 1
contador_movimento = 0
print("Bem-vindo ao jogo Space Invaders simplificado!")
print("Use as teclas 'a' e 's' para mover a nave e a barra de espaço para atirar.")
print("Pressione Enter para começar.")
input()
while True:
if keyboard.is_pressed('a') and posicao_barra > 0:
posicao_barra -= 1
if keyboard.is_pressed('s') and posicao_barra < largura - 3:
posicao_barra += 1
if not tiro_ativo and keyboard.is_pressed('space'):
tiro_ativo = True
posicao_tiro = [posicao_barra + 1, altura - 2]
if tiro_ativo:
posicao_tiro[1] -= 1
if posicao_tiro[1] == 0:
tiro_ativo = False
elif posicao_tiro == posicao_c:
print("Parabéns! Você destruiu a nave inimiga!")
break
posicao_c[0] += direcao_c
contador_movimento += 1
if contador_movimento == 5:
posicao_c[1] += 1
contador_movimento = 0
if posicao_c[0] == 0 or posicao_c[0] == largura - 1:
direcao_c = -direcao_c
if posicao_c[1] == altura - 1:
print("Você perdeu! A nave inimiga chegou à base.")
break
tela(largura, altura, posicao_barra, posicao_c, posicao_tiro)
time.sleep(0.1)
if __name__ == "__main__":
jogar()