我有下面的代码,这应该是一个圆圈游戏,如果用户点击圈内2次,游戏将结束。或在圆圈外(总共点击7次,游戏也将结束),圆圈将改变其位置。这是我需要一些指导和解释如何解决
当我在圆圈外单击时,我需要它出现在我单击的位置上,而不是像现在这样只是窗口中的一个随机位置。 请解释一下我是如何做到这一点的:)
import random
from graphics import *
def draw_circle(win, c = None):
x = random.randint(0,600)
y = random.randint(0,600)
if c is None:
centa = Point(x,y)
c = Circle(centa,50)
c.setFill(color_rgb(200,0,0))
c.draw(win)
else:
center_x = c.p1.x + 50
center_y = c.p1.y + 50
x_dif = (center_x - x) * -1
y_dif = (center_y - y) * -1
c.move(x_dif, y_dif)
return (c, x, y)
def circleGame():
win= GraphWin("Click circles", 600, 600)
totalClicks = 0
var,x,y = draw_circle(win)
clickInside = 0
while totalClicks < 10 and clickInside < 3:
totalClicks += 1
mouseClick2 = win.getMouse()
if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and \
mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
clickInside +=1
totalClicks += random.randint(0,5)
var,x,y = draw_circle(win, var)
circleGame()
提前多谢了
在
draw_circle
函数中,您将x
和y
设置为随机值,然后执行x_dif =(center_x - x) * -1
和y_dif = (center_y - y) * -1
,因此现在您将基于随机值x
和y
来确定位置。然后返回这些随机值。而且,x - center_x
比(center_x - x) * -1
简单另一个问题是
if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
。这不仅比你需要的复杂,而且不是一个圆的测试;这是对正方形的测试。你应该有(mouseClick2.y-y)**2+(mouseClick2.x-x)**2 <= 2500
你的
draw_circle
函数在做两件独立的事情,所以我不确定把它作为一个单独的函数是否有意义。它没有正确更新圆的x和y坐标,因为你总是返回随机坐标,即使你应该返回移动圆的坐标。在下面的代码中,我将该函数的两部分分解为主circle_game
函数我已经把你在正方形内点击的检查换成了在圆形内点击的检查:
(mouse.x - circle_x ) ** 2 + (mouse.y - circle_y) ** 2 <= CIRCLE_RADIUS_SQUARED
我也不知道
totalClicks += random.randint(0,5)
想要达到什么目的相关问题 更多 >
编程相关推荐