Batalha_naval/src/main.py

119 lines
2.6 KiB
Python
Raw Normal View History

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