在这个程序中,我可以让绿色物体移动,但不能让另一个物体移动。我在函数 pygame() 下编写的“if”语句上方评论了“问题”,我认为存在问题。我应该做一个 elif 语句来让另一个对象移动吗?如果有效(r,c)后中断?等这是代码:
from graphics import *
from time import sleep
''''''
def main():
global win,msg
win = GraphWin("PYGAME", winW, winH)
msg = Text(Point(100, winH - off_field//2), "PYGAME")
msg.draw(win)
make_field()
button_Exit, button_Restart = create_buttons()
pygame()
win.getMouse()
win.close()
''''''
column, row = 12, 10
adjacent = ( ( -1, -1), (0, -1), (1, -1),
( -1, 0), (1, 0),
( -1, 1), (0, 1), (1, 1) )
square_size = 30 # Size of each square on the field
off_field = 100 # Display off the field
# field : main game variable; 'I' = ice, 'B': broken
field = [ [ 'I' for c in range (column)] for r in range (row) ]
# user: identifies whose turn it is to play (click)
user = 0 # How do I switch users?
# Initial users' positions: [r, c]; None later replaced by Circle
users = [ [ row//2, 0, None],
[ row//2, column-1, None] ]
winW, winH = column * square_size, row * square_size + off_field # 360, 400
win = None # main app window
msg = None # main information Text
button_Exit = None # exit button
button_Restart = None # restart button
# squares: grid of squares, initially white, later changed to blue:
squares = None
''''''
# pygame_reset(): resets positions and squares turn white
def pygame_reset():
global win, squares
for r in range(row):
for c in range(column):
squares[r][c].setFill('white')
users[0][:2] = [row//2, 0]
users[1][:2] = [row//2, 0]
# User 1 and 2 are repositioned
# make_field(): makes visual grid of squares
def make_field():
global win, squares
# Allocate memory space for Rectangle objects:
squares = [ [ None for c in range (column)] for r in range (row) ]
for r in range(row):
for c in range(column):
x, y = c * square_size, r * square_size
squares[r][c] = Rectangle(Point(x,y), Point(x + square_size, y + square_size) )
squares[r][c].draw(win)
users[0][2] = Circle(Point(square_size//2, square_size//2 + row //2 *square_size),
square_size//2-5)
users[0][2].setFill('green')
users[0][2].draw(win)
users[1][2] = Circle(Point(square_size*11.5, square_size//2 + row //2 *square_size),
square_size//2-5)
users[1][2].setFill('yellow')
users[1][2].draw(win)
# Reset user's positions and all squares to white:
pygame_reset()
# returns True if position (r, c) is adjacent to
# current user's position
# Recall: users = [ [ r, c, Circle],[ r, c, Circle] ]
def valid(r, c):
pr, pc = users[user][0:2]
for dc, dr in adjacent:
if pr + dr == r and pc + dc == c:
return True
return False
def button_template(win, x, y, w, h, txt):
r = Rectangle(Point(x, y), Point(x+w, y+h))
r.setFill("white")
r.draw(win)
t = Text(r.getCenter(), txt) # same as Point(x+w//2, y+h//2)
t.draw(win)
return [ r, t ]
def create_buttons():
global win, field, squares, msg
button_Exit = button_template(win, 260, winH - off_field//1.25, 60, 25, "Exit")
button_Restart = button_template(win, 260, winH - off_field//2.25, 60, 25, "Restart")
return (button_Exit, button_Restart)
def temporary_color(r, c):
global squares
color = squares[r][c].config['fill']
squares[r][c].setFill('red')
sleep(0.5)
squares[r][c].setFill(color)
def move_user(r, c):
global msg, user, users
pr, pc = users[user][0:2]
dx, dy = square_size * (c - pc), square_size * (r - pr)
users[user][2].move(dx, dy)
users[user][0:2] = [ r, c]
msg.setText("User moves in: " + str((r, c)))
def pygame():
global win, field, squares, msg
while True:
pt = win.getMouse()
if pt.x > 260 and pt.y > winH - off_field//1.25:
msg.setText("Exit")
break
if pt.x > 260 and pt.y > winH - off_field//2.25:
msg.setText("Restart")
break
if pt.y < row * square_size:
r, c = pt.y // square_size, pt.x // square_size
msg.setText(str((r, c)))
# PROBLEM - DO I USE 'break' SOMEWHERE HERE?
if valid(r, c):
move_user(r, c)
# Do I use elif? Break? What to do next?
else:
temporary_color(r, c)
msg.setText("Invalid move")
continue
if valid(r, c):
squares[r][c].setFill('orange')
field[r][c] = 'B'
else:
msg.setText("Not in field")
main()