|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如何实现点击第一次选棋子,第二次移动到该位置的功能?import pyglet
from pyglet.window import mouse
from pyglet import shapes
title = "三子棋(卵子棋)"
window = pyglet.window.Window(520, 640,title)
window.set_location(600, 200)
batch1 = pyglet.graphics.Batch()
batch2 = pyglet.graphics.Batch()
background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
background_image = background_pattern.create_image(520, 640)
SIZE=160
reds=[]
blacks=[]
shape_list = []
first=True
turn='红'
last_turn='黑'
redchess=[[0,0],[0,1],[0,2]]
blackchess=[[2,0],[2,1],[2,2]]
Board=[['红','0','黑'],
['红','0','黑'],
['红','0','黑']]
x1,y1,x2,y2=0,0,0,0
for i in range(3):
circle1 = shapes.Circle(100+redchess[i][0]*SIZE, 170 + redchess[i][1]* SIZE, 40, color=(255, 80, 0), batch=batch2)
circle2 = shapes.Circle(100 + blackchess[i][0] * SIZE, 170 + redchess[i][1] * SIZE, 40, color=(0, 0, 0),batch=batch2)
reds.append(circle1)
blacks.append(circle2)
def drawBoard():
for i in range(3):
linex = shapes.Line(i*160+100, 170, i*160+100, 440, width=5, color=(0, 230, 0), batch = batch1 )
linex.opacity = 250
circle1 = shapes.Circle(i*160+100, 490, 60, color=(0, 230, 0), batch = batch1)
circle2 = shapes.Circle(i * 160 + 100, 490, 55, color=(63, 154, 225), batch = batch1)
shape_list.append(linex)
shape_list.append(circle1)
shape_list.append(circle2)
for i in range(2):
liney = shapes.Line(100, 170+i*160, 420, 170+i*160, width=5, color=(0, 230, 0), batch = batch1)
liney.opacity = 250
shape_list.append(liney)
drawBoard()
def change_side():
global turn, last_turn
last_turn=turn
if turn=='红':
turn='黑'
else:
turn='红'
@window.event
def on_mouse_press(x, y, button, modifiers):
global first, x1,y1, turn, Board
if button == mouse.LEFT:
x = round((x - 100) / SIZE)
y = round((y - 170) / SIZE)
if first:
x1 = x
y1 = y
print('x1,y1=',x1,y1)
if Board[y1][x1] != turn:
return None
if Board[y1][x1] == turn:
print('第一次单击')
first = False
else:
x2 = x
y2 = y
print('x2,y2=',x2,y2)
print(turn)
if Board[y2][x2] != '0':
return None
if turn == '红':
redchess.remove([x1, y1])
redchess.append([x2, y2])
Board[y1][x1] = '0'
Board[y2][x2] = turn
elif turn == '黑':
blackchess.remove([x1, y1])
blackchess.append([x2, y2])
Board[y1][x1] = '0'
Board[y2][x2] = turn
change_side()
first = True
@window.event()
def on_draw():
window.clear()
background_image.blit(0, 0)
batch1.draw()
batch2.draw()
pyglet.app.run()
要解决棋子布局被打乱的问题,我们需要确保在初始化时正确设置棋子的位置,并在移动棋子时正确更新它们的位置。以下是一些调整建议:
1. **初始化棋子位置**:确保 `redchess` 和 `blackchess` 在初始化时正确设置。
2. **更新棋子位置**:在移动棋子时,确保更新 `redchess` 和 `blackchess` 的位置。
以下是修改后的代码示例:
import pyglet
from pyglet.window import mouse
from pyglet import shapes
title = "三子棋(卵子棋)"
window = pyglet.window.Window(520, 640, title)
window.set_location(600, 200)
batch1 = pyglet.graphics.Batch()
batch2 = pyglet.graphics.Batch()
background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
background_image = background_pattern.create_image(520, 640)
SIZE = 160
reds = []
blacks = []
shape_list = []
first = True
turn = '红'
last_turn = '黑'
redchess = [0, 0, 0] # 红棋子初始位置
blackchess = [2, 2, 2] # 黑棋子初始位置
Board = [
['红', '0', '黑'],
['红', '0', '黑'],
['红', '0', '黑']
]
x1, y1, x2, y2 = 0, 0, 0, 0
for i in range(3):
circle1 = shapes.Circle(100 + redchess[i] * SIZE, 170 + i * SIZE, 40, color=(255, 80, 0), batch=batch2)
circle2 = shapes.Circle(100 + blackchess[i] * SIZE, 170 + i * SIZE, 40, color=(0, 0, 0), batch=batch2)
reds.append(circle1)
blacks.append(circle2)
def drawBoard():
for i in range(3):
linex = shapes.Line(i * 160 + 100, 170, i * 160 + 100, 440, width=5, color=(0, 230, 0), batch=batch1)
linex.opacity = 250
circle1 = shapes.Circle(i * 160 + 100, 490, 60, color=(0, 230, 0), batch=batch1)
circle2 = shapes.Circle(i * 160 + 100, 490, 55, color=(63, 154, 225), batch=batch1)
shape_list.append(linex)
shape_list.append(circle1)
shape_list.append(circle2)
for i in range(2):
liney = shapes.Line(100, 170 + i * 160, 420, 170 + i * 160, width=5, color=(0, 230, 0), batch=batch1)
liney.opacity = 250
shape_list.append(liney)
drawBoard()
def change_side():
global turn, last_turn
last_turn = turn
if turn == '红':
turn = '黑'
else:
turn = '红'
@window.event
def on_mouse_press(x, y, button, modifiers):
global first, x1, y1, turn, Board
if button == mouse.LEFT:
x = round((x - 100) / SIZE)
y = round((y - 170) / SIZE)
if first:
x1 = x
y1 = y
print('x1,y1=', x1, y1)
if Board[y1][x1] != turn:
return None
if Board[y1][x1] == turn:
print('第一次单击')
first = False
else:
x2 = x
y2 = y
print('x2,y2=', x2, y2)
print(turn)
if Board[y2][x2] != '0':
return None
if turn == '红':
reds[y1].x = 100 + x2 * SIZE
reds[y1].y = 170 + y2 * SIZE
redchess[y1] = x2
Board[y1][x1] = '0'
Board[y2][x2] = turn
elif turn == '黑':
blacks[y1].x = 100 + x2 * SIZE
blacks[y1].y = 170 + y2 * SIZE
blackchess[y1] = x2
Board[y1][x1] = '0'
Board[y2][x2] = turn
change_side()
first = True
@window.event
def on_draw():
window.clear()
background_image.blit(0, 0)
batch1.draw()
batch2.draw()
pyglet.app.run()
在这个代码中,`redchess` 和 `blackchess` 用于跟踪红黑棋子的位置。`on_mouse_press` 函数在移动棋子时更新这些位置,并更新 `Board` 的状态。这样可以确保棋子在移动后不会打乱布局。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|