lundi 27 juin 2016

Python game created with ovals, while loops, and for loops

Hey guys so I am working on a program that fires a bullet from a cannon after having the user input a degree in order to hit the randomly generated target at its position. The program works fine if you are able to hit the target on the first try every time but it seems if I miss that it creates a new target, like it is supposed to, but does not delete the old one. Any idea on how to correct that?

Also, I want it to display in text if you miss the target, inside the canvas. How can I do that? My code is below. Any help would be appreciated, thanks!

from tkinter import *
import time
import math
import random

master = Tk()

w = Canvas(master, width=800,height=800, bg="white")
w.pack()

#Cannon
Img1 = PhotoImage(file="goldcannon.gif")
Pic1 = w.create_image(85,600, image=Img1)

#Grass
w.create_rectangle(0,645, 800,800, fill="green")
w.update()

Choice = "Yes"
Win = False
while(True):
    if Choice == "No":
        TY = w.create_text(400,400, text="Thank you for playing!", font="arial")
        break
    x = []

    #Cannon Target
    Target_x = 700
    Target_y = random.randrange(100,560)
    Target = w.create_oval(Target_x,Target_y, Target_x+25,Target_y+25, fill="red")


    while(True):
        if Choice == "No":
            break


        #Cannon Degree
        degree = simpledialog.askstring("Cannon Degree", "What is your guess?", \
                                initialvalue="Enter a Degree")

        degree = 90 - int(degree)

        #Cannon Bullet
        bMove = w.create_oval(150,560, 150+10,560+10, fill="black")

        for i in range(90):
            w.move(bMove,math.sin(math.radians(degree))*10, \
                   -math.cos(math.radians(degree))*10)
            time.sleep(0.06)
            w.update()

            x = w.find_overlapping(Target_x-10,Target_y, Target_x+35,Target_y+35)

            if len(x)>1:
                w.delete(bMove)
                Hit = w.create_text(400,400, text="You hit the target!", font="arial")
                Choice = simpledialog.askstring("Hello", "Would you like to play again?", \
                                                initialvalue="Yes or No")
                w.delete(Hit)
                w.delete(Target)

                Win = True

                break

        if Win == True:
            break




Aucun commentaire:

Enregistrer un commentaire