Skip to content

Instantly share code, notes, and snippets.

@horstjens
Forked from PaulPolaschek/cannon1.py
Created October 10, 2018 17:28
Show Gist options
  • Save horstjens/30d69ce2547aa8e85981bda4e4dba011 to your computer and use it in GitHub Desktop.
Save horstjens/30d69ce2547aa8e85981bda4e4dba011 to your computer and use it in GitHub Desktop.
artillery game with python turtle
import turtle
import random
peter=turtle.Turtle()
peter.pencolor("blue")
peter.pensize(3)
peter.fillcolor("red")
franz=turtle.Turtle()
franz.pencolor("yellow")
franz.pensize(3)
franz.fillcolor("brown")
susi=turtle.Turtle()
susi.pencolor("purple")
susi.pensize(3)
susi.fillcolor("green")
def kanone(t,winkel=45, laenge=100, radius=20):
t.begin_fill()
t.left(winkel)
t.circle(-20)
t.fd(-40)
t.fd(100)
t.left(90)
t.fd(10)
t.left(90)
t.fd(100)
t.left(90)
t.fd(10)
t.end_fill()
t.left(90)
t.fd(100)
t.left(90)
t.fd(5)
t.right(90)
def ballon(t,radius=10):
t.circle(radius)
t.left(90)
t.fd(radius)
#main
#kanone nach links unten
peter.penup()
peter.goto(-600,-385)
peter.pendown()
kanone(peter)
#x = random.randint(0,300)
#y = random.randint(0,300)
#franz.penup()
#franz.goto(x,y)
#franz.pendown()
#ballon(franz)
#kanone(peter)
#peter.penup()
#peter.goto(200,200)
#peter.pendown()
#ballon(peter)
play = True
runde = 0
gravity = -9.81
while play:
runde +=1
peter.penup()
peter.goto(-600,-385)
peter.setheading(0)
peter.pendown()
franz.clear()
x = random.randint(0,300)
y = random.randint(0,300)
franz.penup()
franz.goto(x,y)
franz.pendown()
ballon(franz)
winkel = turtle.numinput("player 1, runde {}".format(runde), "winkel")
speed = turtle.numinput("player 1, runde {}".format(runde),"speed")
peter.clear()
kanone(peter, winkel)
turtle.home()
turtle.clear()
turtle.left(winkel)
turtle.fd(speed)
dx = turtle.xcor()
dy = turtle.ycor()
turtle.home()
turtle.clear()
ergebnisliste = []
while peter.ycor() >= -385:
peter.goto(peter.xcor()+dx, peter.ycor()+dy)
dy += gravity
d = peter.distance(franz)
ergebnisliste.append(d)
if peter.distance(franz) < 30:
peter.write("treffer")
play = False
break
susi.penup()
susi.goto(200, runde*10)
susi.pendown()
susi.write("w:{} s:{} best distance {}".format(winkel, speed, round(min(ergebnisliste),3)))
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment