Python中的Zelle图形:如何分别移动两个对象
在这个程序中,我可以移动绿色物体,但不能移动另一个。我在pygame()函数下编写的“if”语句上方对“PROBLEM”进行了注释,我认为该语句存在问题。我应该做一个elif语句来移动另一个对象吗?是否有效(r,c)后中断?等等。下面是代码:Python中的Zelle图形:如何分别移动两个对象,python,python-3.x,zelle-graphics,Python,Python 3.x,Zelle Graphics,在这个程序中,我可以移动绿色物体,但不能移动另一个。我在pygame()函数下编写的“if”语句上方对“PROBLEM”进行了注释,我认为该语句存在问题。我应该做一个elif语句来移动另一个对象吗?是否有效(r,c)后中断?等等。下面是代码: from graphics import * from time import sleep '''''' def main(): global win,msg win = GraphWin("PYGAME", winW, winH)
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()
从图形导入*
从时间上导入睡眠
''''''
def main():
全球胜利,味精
win=GraphWin(“PYGAME”,winW,winH)
msg=文本(点(100,winH-off_字段//2),“PYGAME”)
味精。抽签(赢)
make_字段()
按钮退出,按钮重新启动=创建按钮()
pygame()
win.getMouse()
赢
''''''
列,行=12,10
相邻=(-1,-1)、(0,-1)、(1,-1),
( -1, 0), (1, 0),
( -1, 1), (0, 1), (1, 1) )
方块大小=30#场上每个方块的大小
off_field=100#显示off field
#字段:主要游戏变量;'I’=冰,B:坏了
字段=[['I'表示范围内的c(列)]表示范围内的r(行)]
#用户:确定轮到谁玩(单击)
user=0#如何切换用户?
#初始用户位置:[r,c];没有一个后来被圆圈代替
users=[[row//2,0,None],
[第//2行,第1列,无]]
winW,winH=列*平方大小,行*平方大小+关闭字段#360,400
win=无#主应用程序窗口
msg=None#主信息文本
按钮退出=无#退出按钮
按钮重新启动=无#重新启动按钮
#正方形:正方形网格,最初为白色,后来改为蓝色:
正方形=无
''''''
#pygame_reset():重置位置并使方块变白
def pygame_reset():
全球胜利,方块
对于范围内的r(行):
对于范围内的c(列):
正方形[r][c]。设置填充(“白色”)
用户[0][:2]=[行//2,0]
用户[1][:2]=[行//2,0]
#用户1和2被重新定位
#make_field():生成正方形的可视栅格
def make_字段():
全球胜利,方块
#为矩形对象分配内存空间:
平方=[[范围内c无(列)]范围内r无(行)]
对于范围内的r(行):
对于范围内的c(列):
x、 y=c*方形尺寸,r*方形尺寸
正方形[r][c]=矩形(点(x,y),点(x+正方形大小,y+正方形大小))
方块[r][c]。平局(胜利)
用户[0][2]=圆(点(正方形大小//2,正方形大小//2+行//2*正方形大小),
方形(尺寸//2-5)
用户[0][2]。设置填充(“绿色”)
用户[0][2]。抽签(赢)
用户[1][2]=圆(点(正方形大小*11.5,正方形大小//2+行//2*正方形大小),
方形(尺寸//2-5)
用户[1][2]。设置填充(“黄色”)
用户[1][2]。抽签(赢)
#将用户位置和所有方块重置为白色:
pygame_reset()
#如果位置(r,c)邻近
#当前用户的位置
#召回:用户=[[r,c,Circle],[r,c,Circle]]
def有效(r,c):
pr,pc=用户[user][0:2]
对于dc,dr位于相邻的:
如果pr+dr==r和pc+dc==c:
返回真值
返回错误
def按钮_模板(win、x、y、w、h、txt):
r=矩形(点(x,y),点(x+w,y+h))
r、 设置填充(“白色”)
r、 平局(赢)
t=文本(r.getCenter(),txt)#与点(x+w//2,y+h//2)相同
t、 平局(赢)
返回[r,t]
def create_按钮():
全球胜利,场上,广场,味精
按钮退出=按钮模板(win,260,winH-off\u字段//1.25,60,25,“退出”)
按钮重新启动=按钮模板(win,260,winH-off_字段//2.25,60,25,“重新启动”)
返回(按钮退出,按钮重新启动)
def临时颜色(r、c):
全球广场
颜色=正方形[r][c].config['fill']
正方形[r][c]。设置填充(“红色”)
睡眠(0.5)
正方形[r][c]。设置填充(颜色)
def移动用户(r、c):
全局消息,用户,用户
pr,pc=用户[user][0:2]
dx,dy=方形尺寸*(c-pc),方形尺寸*(r-pr)
用户[user][2]。移动(dx,dy)
用户[user][0:2]=[r,c]
msg.setText(“用户移入:”+str((r,c)))
def pygame():
全球胜利,场上,广场,味精
尽管如此:
pt=win.getMouse()
如果pt.x>260,pt.y>winH-off_字段//1.25:
msg.setText(“退出”)
打破
如果pt.x>260,pt.y>winH-off_字段//2.25:
msg.setText(“重新启动”)
打破
如果pt.y<行*方形尺寸:
r、 c=pt.y//square\u尺寸,pt.x//square\u尺寸
msg.setText(str((r,c)))
#问题-我是否在这里的某个地方使用“break”?
如果有效(r,c):
移动用户(r、c)
#我用elif吗?打破下一步怎么办?
其他:
临时颜色(r、c)
msg.setText(“无效移动”)
持续
如果有效(r,c):
正方形[r][c].setFill('orange')的颜色
字段[r][c]=“B”
其他:
msg.setText(“不在字段中”)
main()
你需要一个更复杂的循环来轮换。循环必须一直接受鼠标输入,直到进行有效移动,然后切换用户。下面是一个稍微简化的程序版本,其中包含这样一个循环:
from time import sleep
from graphics import *
COLUMNS, ROWS = 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 = 20 # Display off the field
WIN_W, WIN_H = COLUMNS * SQUARE_SIZE, ROWS * SQUARE_SIZE + OFF_FIELD
# pygame_reset(): resets positions and squares turn white
def pygame_reset():
for r in range(ROWS):
for c in range(COLUMNS):
squares[r][c].setFill('white')
# User 1 and 2 are repositioned
users[0][:2] = [ROWS // 2, 0]
users[1][:2] = [ROWS // 2, COLUMNS - 1]
# make_field(): makes visual grid of squares
def make_field():
for r in range(ROWS):
y = r * SQUARE_SIZE
for c in range(COLUMNS):
x = c * 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 + ROWS//2 * SQUARE_SIZE), SQUARE_SIZE//2 - 5)
users[0][2].setFill('green')
users[1][2] = Circle(Point(SQUARE_SIZE * 11.5, SQUARE_SIZE//2 + ROWS//2 * SQUARE_SIZE), SQUARE_SIZE//2 - 5)
users[1][2].setFill('yellow')
# Reset user's positions and all squares to white:
pygame_reset()
users[0][2].draw(win)
users[1][2].draw(win)
# 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][:2]
for dc, dr in ADJACENT:
if pr + dr == r and pc + dc == c:
return True
return False
def temporary_color(r, c):
color = squares[r][c].config['fill']
squares[r][c].setFill('red')
sleep(0.5)
squares[r][c].setFill(color)
def move_user(r, c):
pr, pc = users[user][:2]
dx, dy = SQUARE_SIZE * (c - pc), SQUARE_SIZE * (r - pr)
users[user][2].move(dx, dy)
users[user][:2] = r, c
msg.setText("User {} moves to: {}".format(user, (r, c)))
squares[r][c].setFill('orange')
field[r][c] = 'B'
def pygame():
global user
while True:
finished_move = False
while not finished_move:
pt = win.getMouse()
if pt.y < ROWS * SQUARE_SIZE:
r, c = int(pt.y // SQUARE_SIZE), int(pt.x // SQUARE_SIZE)
if valid(r, c):
move_user(r, c)
finished_move = True
else:
temporary_color(r, c)
msg.setText("Invalid move for user {}".format(user))
else:
msg.setText("Not in field")
user = 1 - user # switch user
# field : main game variable; 'I' = ice, 'B': broken
field = [['I' for c in range(COLUMNS)] for r in range(ROWS)]
# squares: grid of squares, initially white, later changed to blue:
squares = [[None for c in range(COLUMNS)] for r in range(ROWS)]
# 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 = [
[ROWS // 2, 0, None],
[ROWS // 2, COLUMNS - 1, None]
]
win = GraphWin("PYGAME", WIN_W, WIN_H) # main app window
msg = Text(Point(WIN_W//2, WIN_H - OFF_FIELD//2), "PYGAME") # main information Text
msg.draw(win)
make_field()
pygame()
win.getMouse()
win.close()
从时间导入睡眠
从图形导入*
列,行=12,10
相邻的=(
(-1, -1), (0, -1), (1, -1),
(-1, 0), (1, 0),
(-1, 1), (0, 1), (1, 1)
)
方块大小=30#场上每个方块的大小
OFF_FIELD=20#显示OFF FIELD
WIN\u W,WIN\u H=列*平方大小,行*平方大小+关闭字段
#pygame_reset():重置位置并使方块变白
def pygame_reset():
对于范围内的r(行):
对于范围内的c(列):