辅导python GraphWin程序、讲解GraphWin python
- 首页 >> Python编程from graphics import *
import random
import time
def main():
win = GraphWin("Bug-template",600,600)
win.setCoords(0,0,6,6)
#Times left information
numTry = 0
#draw grid
for i in range(3):
index = i+1
p1 = Point(index,1)
p2 = Point(index,3)
Line(p1,p2).draw(win)
p3 = Point(1,index)
p4 = Point(3,index)
Line(p3,p4).draw(win)
# initialize position of bug and show it
initPnt = Point(1.5,1.5)
bug = Image(initPnt,"bug.gif")
bug.draw(win)
#repeat until 5 times
gC = win.getMouse()
while ((gC.x>=1)and (gC.x<=3) and (gC.y>=1)and (gC.y<=3)):
# move the bug
# convert user click and mark the grid
converted_x = int(gC.x)
converted_y = int(gC.y)
redGridPnt1 = Point(converted_x,converted_y)
redGridPnt2 = Point(converted_x+1,converted_y+1)
redGrid = Rectangle(redGridPnt1,redGridPnt2)
redGrid.setFill("lightblue")
redGrid.draw(win)
# check if bug is there, break if so
numTry = numTry+1
if numTry >= 5:
break
# timesText.setText("Moves: "+str(numTry))
gC = win.getMouse()
redGrid.undraw()
click = win.getMouse()
win.close()
main()