New to Python Please HELP

New to Python Please HELP

Postby ssmith21991 » Thu Apr 24, 2014 4:37 am

I have to make a simple shooting game for my programming class. Everything is fine except for my math, when I shoot the cannon it is not calculating where the target is correctly. It should count as a hit no matter what part of the cannon it hits. Please advise, your help is greatly appreciated.

Code: Select all
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()
Last edited by Mekire on Thu Apr 24, 2014 4:39 am, edited 1 time in total.
Reason: First post lock; code tags.
ssmith21991
 
Posts: 1
Joined: Thu Apr 24, 2014 4:30 am

Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 2 guests