Last active
November 10, 2018 20:48
-
-
Save PaulPolaschek/f44e34f373e975184ce4a6549a9af7df to your computer and use it in GitHub Desktop.
artillery game with python turtle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
turtle.setup(1400,800) | |
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 cannon(t,angle=45, length=100, radius=20): | |
t.begin_fill() | |
t.left(angle) | |
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 baloon(t,radius=10): | |
t.circle(radius) | |
t.left(90) | |
t.fd(radius) | |
#main | |
#cannon to down left | |
peter.penup() | |
peter.goto(-600,-385) | |
peter.pendown() | |
cannon(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 #round | |
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() | |
baloon(franz) | |
angle = turtle.numinput("player 1, round {}".format(runde), "angle") | |
speed = turtle.numinput("player 1, round {}".format(runde),"speed") | |
peter.clear() | |
cannon(peter, angle) | |
turtle.home() | |
turtle.clear() | |
turtle.left(angle) | |
turtle.fd(speed) | |
dx = turtle.xcor() | |
dy = turtle.ycor() | |
turtle.home() | |
turtle.clear() | |
resultlist = [] | |
while peter.ycor() >= -385: | |
peter.goto(peter.xcor()+dx, peter.ycor()+dy) | |
dy += gravity | |
d = peter.distance(franz) | |
resultlist.append(d) | |
if peter.distance(franz) < 30: | |
peter.write("hit") | |
play = False | |
break | |
susi.penup() | |
susi.goto(200, runde*10) | |
susi.pendown() | |
susi.write("a:{} s:{} best distance {}".format(angle, speed, round(min(resultlist),3))) | |
turtle.exitonclick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment