I am trying to have a ball hit a circle. If I miss I need to be able to try again without having the circle move to a different location (its location is random).
When I did this with one While Loop, I was able to hit the circle and stop the ball. However, if I missed and tried to shoot the circle again, its location changed; which is why I needed the second While Loop.
After adding the second While Loop, the ball just goes through the circle now and doesn't stop. I've tried == and > for my if len (x) >= 2and both do not work. I changed the if len (x) >= 1and that does give me the text You Won but that's false since every shot is now considered a hit.
When I print (len(x)) I can see the 2 for overlap but my if len (x) will not take the value anymore. Why would the second While Loop affect my if len (x) statement?
I'm using python 3.5.2
from tkinter import *
import time
import math
import random
master = Tk()
w = Canvas(master, width=800, height=800, bg="white")
w.pack ()
##____Floor________
floor=w.create_rectangle(0, 690, 800, 800, fill="brown")
##____________
msgbox2 = 'y'
while (True):
if msgbox2 == 'n':
w.delete (ALL)
w.update ()
break
##____Target________
r=random.randrange(0, 255)
r=random.randrange(0, 255)
g=random.randrange(0, 255)
b=random.randrange(0, 255)
tr_col="#%02x%02x%02x" %(r, g, b)
tar_x = 680 - 25
tar_y = random.randrange(70, 660) - 50
target = w.create_oval(tar_x, tar_y, tar_x+75, tar_y+75, fill=tr_col)
##_________________
while (True):
##____Ball_______
ball_x = 230 - 25
ball_y = 630 - 25
ball = w.create_oval(ball_x, ball_y, ball_x+25, ball_y+25, fill="blue" )
##_____Weapon_________
img=PhotoImage(file="cannon_02.png") ## the image doesn't seem to effect it
w.create_image(90, 600, anchor=NW, image=img)
##____Shoot___
msgbox1 = simpledialog.askstring ("Can you hit it?", \
' Enter a degree' \
' or n to exit game.', \
initialvalue='Enter a degree or n to exit')
msgbox1 = 90 - int(msgbox1)
for i in range (90):
w.move(ball, math.sin(math.radians(msgbox1))*10,\
-math.cos(math.radians(msgbox1))*10)
time.sleep (0.03)
w.update()
w.update ()
x = w.find_overlapping(tar_x, tar_y, tar_x+75, tar_y+75)
print (len(x))
print (x)
##____Hit_Target____
if len (x) >= 2:
text = w.create_text (300,400, text = 'You Won!!!', font = 'arial')
msgbox2 = simpledialog.askstring ('Target Hit!!!', \
'y = Play again,' \
'n = Exit game.', \
initialvalue=' type y or n')
w.delete (target)
w.delete (hittxt)
w.update
if msgbox2 == 'n':
break
##________________
if msgbox2 == 'n':
w.delete (ALL)
break
Aucun commentaire:
Enregistrer un commentaire