Crie um jogo simples para dois jogadores usando o Turtle em Python
Pré-requisitos: Programação de tartaruga em Python
TurtleMove jogo é basicamente um jogo baseado na sorte. Neste jogo, dois jogadores (Vermelho e Azul), usando sua própria tartaruga (objeto), jogam o jogo.
Como jogar
O jogo é jogado na grade predefinida com alguns limites.
- Ambos os jogadores movem a tartaruga por uma unidade de distância.
- Agora, os dois jogadores lançam a moeda:
- se HEAD, então vire à direita
- caso contrário, vire à esquerda
- 3) Agora repita 1 e 2, até que ambas as tartarugas estejam no limite
Implementação em Turtle Python
- Primeiro, um objeto de tela de tartaruga é criado para o limite da grade.
- Agora duas tartarugas (vermelha e azul) são criadas, uma para cada jogador.
- Ambas as tartarugas são movidas por uma unidade de distância usando o método turtle_obj.forward (50) .
- A volta é decidida, usando random.randrange (0, 2), ou seja, 0 para a esquerda e 1 para a direita.
- Após cada movimento, a posição de cada tartaruga é verificada, se alguma tartaruga cruzar a fronteira, então essa tartaruga perde o jogo.
Abaixo está a implementação
import
random
import
turtle
def
isInScreen(win, turt):
leftBound
=
-
win.window_width()
/
2
rightBound
=
win.window_width()
/
2
topBound
=
win.window_height()
/
2
bottomBound
=
-
win.window_height()
/
2
turtleX
=
turt.xcor()
turtleY
=
turt.ycor()
stillIn
=
True
if
turtleX > rightBound
or
turtleX < leftBound:
stillIn
=
False
if
turtleY > topBound
or
turtleY < bottomBound:
stillIn
=
False
return
stillIn
def
sameposition(Red, Blue):
if
Red.pos()
=
=
Blue.pos():
return
False
else
:
return
True
def
main():
wn
=
turtle.Screen()
Red
=
turtle.Turtle()
Red.pencolor(
"red"
)
Red.pensize(
5
)
Red.shape(
'turtle'
)
pos
=
Red.pos()
Blue
=
turtle.Turtle()
Blue.pencolor(
"blue"
)
Blue.pensize(
5
)
Blue.shape(
'turtle'
)
Blue.hideturtle()
Blue.penup()
Blue.goto(pos[
0
]
+
50
, pos[
1
])
Blue.showturtle()
Blue.pendown()
mT
=
True
jT
=
True
while
mT
and
jT
and
sameposition(Red, Blue):
coinRed
=
random.randrange(
0
,
2
)
angleRed
=
90
if
coinRed
=
=
0
:
Red.left(angleRed)
else
:
Red.right(angleRed)
coinBlue
=
random.randrange(
0
,
2
)
angleBlue
=
90
if
coinBlue
=
=
0
:
Blue.left(angleBlue)
else
:
Blue.right(angleBlue)
Red.forward(
50
)
Blue.forward(
50
)
mT
=
isInScreen(wn, Blue)
jT
=
isInScreen(wn, Red)
Red.pencolor(
"black"
)
Blue.pencolor(
"black"
)
if
jT
=
=
True
and
mT
=
=
False
:
Red.write(
"Red Won"
,
True
, align
=
"center"
,
font
=
(
"arial"
,
15
,
"bold"
))
elif
mT
=
=
True
and
jT
=
=
False
:
Blue.write(
"Blue Won"
,
True
, align
=
"center"
,
font
=
(
"arial"
,
15
,
"bold"
))
else
:
Red.write(
"Draw"
,
True
, align
=
"center"
,
font
=
(
"arial"
,
15
,
"bold"
))
Blue.write(
"Draw"
,
True
, align
=
"center"
,
font
=
(
"arial"
,
15
,
"bold"
))
wn.exitonclick()
main()
Resultado:
As postagens do blog Acervo Lima te ajudaram? Nos ajude a manter o blog no ar!
Faça uma doação para manter o blog funcionando.
70% das doações são no valor de R$ 5,00...
Diógenes Lima da Silva