Batalha_naval/src/main.py

130 lines
2.7 KiB
Python

#setup
import turtle as tl
import random, sys
args = sys.argv
# name, -arguments
def has_argument(args: list[str], arg: str) -> bool:
if len(args) > 1:
if args[1].startswith("-"):
for chr in args[1]:
if chr == arg:
return True
return False
def num2char(num: int) -> chr:
return chr(num + 65)
def char2num(char: str) -> int:
return ord(char) - 65
posx = [0, 1, 2, 3, 4]
posy = [0, 1, 2, 3, 4]
postiro = []
for i in range(len(posx)):
for j in range(len(posy)):
postiro.append(f"{i-2} {j-2}")
tl.setup(650,650,None,None)
tl.title("Naval Battle 1943 Loading...")
tl.speed(0)
#funções
def linhax(x: int, y: int) -> None:
tl.up()
tl.goto(x,y)
tl.down()
tl.goto(x+600,y)
def linhay(x: int, y: int) -> None:
tl.up()
tl.goto(x,y)
tl.down()
tl.goto(x,y+600)
#radar screen
tl.bgcolor("lime green")
#moldura
linhax(-300,-300)
linhay(-300,-300)
linhay(300,-300)
linhax(-300,300)
#linhas X
for i in range(5):
linhax(-300, (i-2)*100)
tl.write(num2char(i), False, align="right")
#linhas Y
for i in range(5):
linhay((i-2)*100, -300)
tl.write(str(i), False, align="right")
#Tabuleiro pronto
tl.title("Naval Battle 1943")
tl.hideturtle()
boat = tl.Turtle()
boat.shapesize(3, 3)
boat.up()
boat.goto(0,0)
boat.left(90)
#Enemies
AMOUNT_OF_ENEMIES = 3
enemies = []
for i in range(AMOUNT_OF_ENEMIES):
enemy = tl.Turtle()
enemy.color("red")
enemy.up()
if not has_argument(args, "D"):
enemy.hideturtle()
else:
enemy.shapesize(3, 3)
pos = [random.choice(posy), random.choice(posy)]
enemy.goto((pos[0] - 2) * 100, (pos[1] - 2) * 100)
enemies.append([enemy, pos])
#partidas
win = False
while win == False:
tirox = tl.numinput("linha X do disparo","Em qual quadrante X devemos disparar a artilharia")
tiroy = tl.textinput("linha Y do disparo","Em qual quadrante Y devemos disparar a artilharia")
tiroy = char2num(tiroy.upper())
if tirox == None or tiroy == None:
tl.exitonclick()
boat.goto((tirox - 2) * 100, (tiroy - 2) * 100)
acerto = False
for enemy in enemies:
if [tirox,tiroy] == enemy[1]:
hit_enemy: list[tl.Turtle, list[int, int]] = enemy
acerto = True
break
if acerto:
print("Acerto!")
boat.write("U-boat Afundado\n Capitão!",False,align="center")
hit_enemy[0].hideturtle()
enemies.remove(hit_enemy)
else:
print("errou!")
boat.write("Erramos o alvo\n Capitão!",False,align="center")
acerto = False
if len(enemies) <= 0: # tamanho negativo???
win = True
print("Você ganhou!!")
tl.exitonclick()