Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active March 14, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horstjens/3743d4c940087652fefe3b0646cce213 to your computer and use it in GitHub Desktop.
Save horstjens/3743d4c940087652fefe3b0646cce213 to your computer and use it in GitHub Desktop.
cannon game using turtle graphic
import turtle
import math
import random
screen = turtle.getscreen()
screen.setup(width=1.0, height=0.9, startx=0, starty=0)
#width, height = screen.window_width(), screen.window_height()
width = random.randint(20000,50000)
height = width
screen.setworldcoordinates(0,0,width, height)
turtle.speed(0)
kanone1 = turtle.Turtle()
kanone2 = turtle.Turtle()
ball1 = turtle.Turtle()
ball2 = turtle.Turtle()
ball1.speed(0)
ball2.speed(0)
x1 = 20
x2 = width - 20
for (ball, kanone) in ((ball1, kanone1),(ball2,kanone2)):
kanone.penup()
ball.penup()
kanone.shape("turtle")
ball.shape("circle")
kanone.shapesize(2,2,1)
if kanone == kanone2:
kanone.goto(x2,20)
kanone.color("red","red")
ball.color("red","red")
kanone.left(180)
else:
kanone.goto(x1,20)
kanone.color("blue","blue")
ball.color("blue","blue")
ball.goto(kanone.pos())
ball.pendown()
winkel1, speed1 = 30,300
winkel2, speed2 = 40,400
player1 = turtle.textinput("Player1","Wie heißt Du, Spieler1?")
player2 = turtle.textinput("Player2","Wie heißt Du, Spieler2?")
text1 = text2 = f"Distanz zum Ziel: {kanone2.xcor()-kanone1.xcor()} m\n"
#text2 = ""
gameover = False
g = -9.81
crit = 1
dt = 0.25
runde = 0
while True:
runde += 1
winkel1 = turtle.numinput(f"{player1} Schuss {runde} Winkel",
f"{text1}{player1} Winkel in ° eingeben (0-90) >>>",
winkel1,0,90)
speed1 = turtle.numinput(f"{player1} Schuss {runde} Speed",
f"{text1}{player1} speed in [m/s] eingeben (1-99999) >>>",
speed1,1,99999)
winkel2 = turtle.numinput(f"{player2} Schuss {runde} Winkel",
f"{text2}{player2} Winkel in ° eingeben (0-90) >>>",
winkel2,0,90)
speed2 = turtle.numinput(f"{player2} Schuss {runde} Speed",
f"{text2}{player2} speed in [m/s] eingeben (1-99999) >>>",
speed2,1,99999)
kanone1.setheading(winkel1)
kanone2.setheading(180-winkel2)
ball1.goto(kanone1.pos())
ball2.goto(kanone2.pos())
ball1.clear()
ball2.clear()
vx1 = math.cos(math.radians(winkel1)) * speed1
vy1 = math.sin(math.radians(winkel1)) * speed1
vx2 = math.cos(math.radians(180-winkel2)) * speed2
vy2 = math.sin(math.radians(180-winkel2)) * speed2
t=0
while ball1.ycor() >= 20 or ball2.ycor() >= 20:
t += dt
x1 = kanone1.xcor() + vx1 * t
y1 = kanone1.ycor() + vy1 * t + g * 0.5 * t * t
x2 = kanone2.xcor() + vx2 * t
y2 = kanone2.ycor() + vy2 * t + g * 0.5 * t * t
if ball1.ycor() >= 20:
ball1.goto(x1,y1)
if ball2.ycor() >= 20:
ball2.goto(x2,y2)
# --- auswertung ----
win1 = False
win2 = False
distance1 = ball1.xcor() - kanone2.xcor()
distance2 = kanone1.xcor() - ball2.xcor()
if abs(distance1) < crit:
win1 = True
if abs(distance2) < crit:
win2 = True
if win1 or win2:
break
text1 += "Schuss Nummer {} ({}°, {}m/s) war {} Meter {}\n".format(
runde, winkel1, speed1, abs(distance1),
'zu weit' if distance1 > 0 else 'zu kurz')
text2 += "Schuss Nummer {} ({}°, {}m/s) war {} Meter {}\n".format(
runde, winkel2, speed2, abs(distance2),
'zu weit' if distance2 > 0 else 'zu kurz')
# winner
if win1 and win2:
text = "unentschieden"
elif win1:
text = f"{player1} gewinnt"
elif win2:
text = f"{player2} gewinnt"
turtle.penup()
turtle.goto(width/2,height/2)
turtle.pendown()
turtle.write(text,align="center", font=("System", 50))
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment