好的,我的目标是创造一个游戏。
虽然用户得分小于10(最初设置为0),但我希望在圈子周围的内切方块中点击时,将0到5之间的随机数添加到得分中。
现在当我点击某些原因正在绘制新的圆圈时。如果在圆圈中单击它,我希望该圆圈未被绘制,并且a和新圆圈将在随机位置绘制。
from graphics import *
import random
def main():
win= GraphWin("lab12",500,500)
score=0
while score < 10:
x=random.randint(0,450)
y=random.randint(0,450)
centa=Point(x,y)
c=Circle(centa,50)
c.setFill(color_rgb(200,0,0))
c.draw(win)
mouseClick2=win.getMouse()
if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
score=score + random.randint(0,5)
c.undraw()
x=random.randint(0,450)
y=random.randint(0,450)
centa=Point(x,y)
c=Circle(centa,50)
c.setFill(color_rgb(200,0,0))
c.draw(win)
else:
score=score+0
print "you won"
print "your final score is, "
main()
答案 0 :(得分:0)
如何而不是尝试清除圆圈,而不是简单地翻译和移动坐标?
我使用以下
这样做了def draw_circle(win, c=None):
x=random.randint(0,450)
y=random.randint(0,450)
if c is None:
centa=Point(x,y)
c = Circle(centa,50)
c.setFill(color_rgb(200,0,0))
c.draw(win)
else:
p1 = c.p1
x_dif = (p1.x - x) * -1
y_dif = (p1.y - y) * -1
c.move(x_dif, y_dif)
return (c, x, y)
基本翻译算法是newX = (x1-x2) * -1
和newY = (y1-y2) * -1
。我们交换符号,因为负值表示新的x坐标大于旧坐标,而应该向右移动(不是左移,由移动指示)。
这会将您的主循环更改为以下内容:
def main():
win= GraphWin("lab12",500,500)
score=0
c,x,y = draw_circle(win)
while score < 10:
mouseClick2=win.getMouse()
if mouseClick2.y >= y-50 and mouseClick2.y <= y +50 and mouseClick2.x >= x-50 and mouseClick2.x <= x+50:
score=score + random.randint(0,5)
c,x,y = draw_circle(win, c)
print "you won"
print "your final score is, {0}".format(score)
请记住,在python中你可以 unpack
一个元组/数组/生成器到一组变量上,并且你的变量数量与所述元组/数组/生成器的大小相匹配。
e.g。
x,y,z = 5,10,20 # works
x,y = 5,10,20 # does not work
x,y,z = 5,10 # does not work
哦,我把你的分数加到了打印声明中!