我正在尝试进行一场拉锯战,但我遇到了一个问题。我不知道如何在不导入pygame的情况下更改x值。我需要的是每当按下'z'时,x向左移动5,当按下'm'时,它向右移动5.
import simplegui
import random
import sys
x = 500
y = 200
num = 0
def draw_handler(canvas):
global x
global y
global num
color = "RGB(15, 69, 320)"
x = x + num
if (x >= 750):
print ("Red Wins")
quit()
if (x <= 250):
print ("Blue Wins")
quit()
canvas.draw_polygon([(0,0),(500,0),(500,500),(0,500)], 2, "Blue", "Blue")
canvas.draw_polygon([(500,0),(500,500),(1000,0),(500,1000)], 2, "Red", "Red")
canvas.draw_polygon([(0,0),(250,200),(0,400)], 2, "Black", "Red")
canvas.draw_polygon([(1000,0),(750,200),(1000,400)], 2, "Black", "Blue")
#Finish Line - canvas.draw_polygon([(199,0),(201,0),(199,1000),(201,1000)], 2, "Black", "Black")
canvas.draw_polygon([(0,199),(0,201),(1000,199),(1000,201)], 2, "White", "Black")
canvas.draw_line((500,0),(500,500), 5, ("Black"))
canvas.draw_line((0,0),(0,1000), 5, ("Black"))
canvas.draw_line((0,0),(1000,0), 5, ("Black"))
canvas.draw_line((0,400),(1000,400), 5, ("Black"))
canvas.draw_line((1000,0),(1000,400), 5, ("Black"))
canvas.draw_circle((x, y), 30, 20, "Black", "White")
canvas.draw_circle((x, y), 80, 1, "Black")
canvas.draw_circle((x, y), 90, 1, "Black")
canvas.draw_circle((x, y), 100, 1, "Black")
canvas.draw_circle((x, y), 120, 1, "Black")
for i in range (1,1):
print""
frame = simplegui.create_frame("Go", 1000, 400)
frame.set_draw_handler(draw_handler)
frame.set_canvas_background("Red")
frame.start()
代码更新here。
答案 0 :(得分:0)
您可以使用while循环来继续请求输入,然后在循环中处理该输入并在每次迭代时重绘画布。我知道这可能是一种痛苦,因为他们也必须按下输入,但这可能是你必须要做的。希望这有帮助!