Please Help New to Python - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Please Help New to Python

ssmith21991ssmith21991 Daytona BeachPosts: 1Member

I have to make a simple shooting game for my programming class, and everything else seems to be fine except for my math. Please advise:

import turtle
import tkinter
import random
import math

turtle.screensize(400, 400)

y = random.randint(-206, 206)

turtle.hideturtle()
turtle.tracer(2)

def graph():
turtle.bgcolor("Sky Blue")

# Ground
turtle.setheading(0)
turtle.begin_fill()
turtle.color("Green")

turtle.width()
turtle.penup()
turtle.goto(-400, -250)
turtle.pendown()


for r in range(2):

    turtle.forward(800)
    turtle.right(90)
    turtle.forward(75)
    turtle.right(90)
turtle.end_fill()


#Sun
turtle.penup() 
turtle.goto(-245, 180)
turtle.pendown()


turtle.color("Yellow")
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
turtle.goto(-245, 230)
turtle.width(8)
for k in range(13):
    turtle.forward(75)
    turtle.back(75)
    turtle.right(5)
    turtle.right(360/13)

def c2():
turtle.color("Gray")
turtle.width()
turtle.penup()
turtle.goto(-340, -220)
turtle.pendown()
turtle.begin_fill()

turtle.setheading(0)

for q in range(2):
    turtle.forward(120)
    turtle.left(90)
    turtle.forward(25)
    turtle.left(90)
turtle.end_fill()


#Cannon wheel
turtle.setheading(0)
turtle.penup()
turtle.goto(-310, -250)
turtle.pendown()

turtle.color("Brown")
turtle.width(10)
turtle.circle(40)


turtle.penup()
turtle.width(5)
turtle.goto(-310, -210)
turtle.pendown()

for m in range(13):
    turtle.forward(35)
    turtle.back(35)
    turtle.right(5)
    turtle.right(360/13)
turtle.setheading(0)

def target():
turtle.penup()
turtle.goto(260, y)
turtle.pendown()

turtle.color("Red")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()

def c1():
#Cannon 1
turtle.tracer(2)
turtle.color("Gray")
turtle.width()
turtle.penup()
turtle.goto(-340, -220)
turtle.pendown()
turtle.begin_fill()

turtle.setheading(degree)

for i in range(2):
    turtle.forward(120)
    turtle.left(90)
    turtle.forward(25)
    turtle.left(90)
turtle.end_fill()


#Cannon wheel
turtle.setheading(0)
turtle.penup()
turtle.goto(-310, -250)
turtle.pendown()

turtle.color("Brown")
turtle.width(10)
turtle.circle(40)


turtle.penup()
turtle.width(5)
turtle.goto(-310, -210)
turtle.pendown()

for a in range(13):
    turtle.forward(35)
    turtle.back(35)
    turtle.right(5)
    turtle.right(360/13)

def ball():
turtle.hideturtle()
turtle.tracer(1)
turtle.shape("circle")
turtle.penup()
turtle.goto(-340, -206)
turtle.pendown()
turtle.showturtle()

turtle.color("black")
turtle.setheading(degree)
turtle.penup()
turtle.hideturtle()
turtle.forward(130)
turtle.pendown()
turtle.showturtle()
for u in range(16):
    turtle.speed("fastest")
    turtle.penup()
    turtle.forward(42)
    turtle.pendown()
    turtle.undo()
turtle.hideturtle()
turtle.setheading(0)

def c3():
turtle.tracer(2)
turtle.setheading(0)
turtle.penup()
turtle.goto(-360, -247)
turtle.pendown()
turtle.color("sky blue")
turtle.begin_fill()
for e in range(4):
turtle.forward(150)
turtle.left(90)
turtle.end_fill()

def hit():
for p in range(1):
turtle.tracer(2)

    turtle.penup()
    turtle.goto(260, y + 30)
    turtle.pendown()

    turtle.pencolor("sky blue")

    for l in range(40):
        k = random.randint(0,100)
        turtle.tracer(2)

        turtle.forward(k)
        turtle.back(k)

        turtle.right(10)
        turtle.right(360/40)

graph()
c2()

while (True):
a1 = 206 + (y - 65)
a2 = 206 + (y + 60)
b = 340 + 260

d1 = math.tan(a1/b)
d2 = math.tan(a2/b)


target()






degree = int(turtle.numinput("Enter a Degree", " Enter a Degree"))
q = math.radians(degree)


c3()
c1()
ball()







if(math.tan(q) > d1) and (math.tan(q) < d2):
    hit()
    answer = turtle.textinput("Nice Shot!", " Would you like to play again? y/n")
    if(answer == "n"):
        break
    else:
        turtle.penup()
        turtle.goto(245, y)
        turtle.pendown()
        turtle.color("sky blue")
        turtle.begin_fill()
        turtle.circle(100)
        turtle.end_fill()
        turtle.penup()
        turtle.setheading(0)
        y = random.randint(-206, 206)


else:
    answer2 = turtle.textinput("Miss! Try Again", "Continue? y/n")
    if(answer2 == "n"):
        break
    turtle.penup()

The cannon ball should count as a hit no matter where it hits the target. If anyone can help it would be greatly appreciated.
Thanks,
Sam

Sign In or Register to comment.