鱼C论坛

 找回密码
 立即注册
查看: 293|回复: 3

[已解决]用pyglet如何写棋类游戏

[复制链接]
发表于 2024-11-3 10:46:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如何实现点击第一次选棋子,第二次移动到该位置的功能?
  1. import pyglet
  2. from pyglet.window import mouse
  3. from pyglet import shapes
  4. title = "三子棋(卵子棋)"
  5. window = pyglet.window.Window(520, 640,title)
  6. window.set_location(600, 200)
  7. batch1 = pyglet.graphics.Batch()
  8. batch2 = pyglet.graphics.Batch()
  9. background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
  10. background_image = background_pattern.create_image(520, 640)
  11. SIZE=160
  12. reds=[]
  13. blacks=[]
  14. shape_list = []
  15. first=True
  16. turn='红'
  17. last_turn='黑'
  18. redchess=[[0,0],[0,1],[0,2]]
  19. blackchess=[[2,0],[2,1],[2,2]]
  20. Board=[['红','0','黑'],
  21.        ['红','0','黑'],
  22.        ['红','0','黑']]
  23. x1,y1,x2,y2=0,0,0,0
  24. for i in range(3):
  25.     circle1 = shapes.Circle(100+redchess[i][0]*SIZE, 170 + redchess[i][1]* SIZE, 40, color=(255, 80, 0), batch=batch2)
  26.     circle2 = shapes.Circle(100 + blackchess[i][0] * SIZE, 170 + redchess[i][1] * SIZE, 40, color=(0, 0, 0),batch=batch2)
  27.     reds.append(circle1)
  28.     blacks.append(circle2)

  29. def drawBoard():
  30.     for i in range(3):
  31.         linex = shapes.Line(i*160+100, 170, i*160+100, 440, width=5, color=(0, 230, 0), batch = batch1 )
  32.         linex.opacity = 250
  33.         circle1 = shapes.Circle(i*160+100, 490, 60, color=(0, 230, 0), batch = batch1)
  34.         circle2 = shapes.Circle(i * 160 + 100, 490, 55,  color=(63, 154, 225), batch = batch1)
  35.         shape_list.append(linex)
  36.         shape_list.append(circle1)
  37.         shape_list.append(circle2)
  38.     for i in range(2):
  39.         liney = shapes.Line(100, 170+i*160, 420, 170+i*160, width=5, color=(0, 230, 0), batch = batch1)
  40.         liney.opacity = 250
  41.         shape_list.append(liney)

  42. drawBoard()
  43. def change_side():
  44.     global turn, last_turn
  45.     last_turn=turn
  46.     if turn=='红':
  47.         turn='黑'
  48.     else:
  49.         turn='红'
  50. @window.event
  51. def on_mouse_press(x, y, button, modifiers):
  52.     global first, x1,y1, turn, Board
  53.     if button == mouse.LEFT:
  54.         x = round((x - 100) / SIZE)
  55.         y = round((y - 170) / SIZE)
  56.         if first:
  57.             x1 = x
  58.             y1 = y
  59.             print('x1,y1=',x1,y1)
  60.             if Board[y1][x1] != turn:
  61.                 return None
  62.             if Board[y1][x1] == turn:
  63.                 print('第一次单击')
  64.                 first = False
  65.         else:
  66.             x2 = x
  67.             y2 = y
  68.             print('x2,y2=',x2,y2)
  69.             print(turn)
  70.             if Board[y2][x2] != '0':
  71.                 return None
  72.             if turn == '红':
  73.                 redchess.remove([x1, y1])
  74.                 redchess.append([x2, y2])
  75.                 Board[y1][x1] = '0'
  76.                 Board[y2][x2] = turn

  77.             elif turn == '黑':
  78.                 blackchess.remove([x1, y1])
  79.                 blackchess.append([x2, y2])
  80.                 Board[y1][x1] = '0'
  81.                 Board[y2][x2] = turn
  82.             change_side()
  83.             first = True

  84. @window.event()
  85. def on_draw():
  86.     window.clear()
  87.     background_image.blit(0, 0)
  88.     batch1.draw()
  89.     batch2.draw()

  90. pyglet.app.run()
复制代码
最佳答案
2024-11-3 22:45:41
ydwb 发表于 2024-11-03 22:28
修改的代码实现了棋子移动的功能,但将棋子布局打乱了 ...


要解决棋子布局被打乱的问题,我们需要确保在初始化时正确设置棋子的位置,并在移动棋子时正确更新它们的位置。以下是一些调整建议:

1. **初始化棋子位置**:确保 `redchess` 和 `blackchess` 在初始化时正确设置。

2. **更新棋子位置**:在移动棋子时,确保更新 `redchess` 和 `blackchess` 的位置。

以下是修改后的代码示例:

  1. import pyglet
  2. from pyglet.window import mouse
  3. from pyglet import shapes

  4. title = "三子棋(卵子棋)"
  5. window = pyglet.window.Window(520, 640, title)
  6. window.set_location(600, 200)
  7. batch1 = pyglet.graphics.Batch()
  8. batch2 = pyglet.graphics.Batch()
  9. background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
  10. background_image = background_pattern.create_image(520, 640)
  11. SIZE = 160

  12. reds = []
  13. blacks = []
  14. shape_list = []
  15. first = True
  16. turn = '红'
  17. last_turn = '黑'
  18. redchess = [0, 0, 0]  # 红棋子初始位置
  19. blackchess = [2, 2, 2]  # 黑棋子初始位置
  20. Board = [
  21.     ['红', '0', '黑'],
  22.     ['红', '0', '黑'],
  23.     ['红', '0', '黑']
  24. ]

  25. x1, y1, x2, y2 = 0, 0, 0, 0

  26. for i in range(3):
  27.     circle1 = shapes.Circle(100 + redchess[i] * SIZE, 170 + i * SIZE, 40, color=(255, 80, 0), batch=batch2)
  28.     circle2 = shapes.Circle(100 + blackchess[i] * SIZE, 170 + i * SIZE, 40, color=(0, 0, 0), batch=batch2)
  29.     reds.append(circle1)
  30.     blacks.append(circle2)

  31. def drawBoard():
  32.     for i in range(3):
  33.         linex = shapes.Line(i * 160 + 100, 170, i * 160 + 100, 440, width=5, color=(0, 230, 0), batch=batch1)
  34.         linex.opacity = 250
  35.         circle1 = shapes.Circle(i * 160 + 100, 490, 60, color=(0, 230, 0), batch=batch1)
  36.         circle2 = shapes.Circle(i * 160 + 100, 490, 55, color=(63, 154, 225), batch=batch1)
  37.         shape_list.append(linex)
  38.         shape_list.append(circle1)
  39.         shape_list.append(circle2)
  40.     for i in range(2):
  41.         liney = shapes.Line(100, 170 + i * 160, 420, 170 + i * 160, width=5, color=(0, 230, 0), batch=batch1)
  42.         liney.opacity = 250
  43.         shape_list.append(liney)

  44. drawBoard()

  45. def change_side():
  46.     global turn, last_turn
  47.     last_turn = turn
  48.     if turn == '红':
  49.         turn = '黑'
  50.     else:
  51.         turn = '红'

  52. @window.event
  53. def on_mouse_press(x, y, button, modifiers):
  54.     global first, x1, y1, turn, Board
  55.     if button == mouse.LEFT:
  56.         x = round((x - 100) / SIZE)
  57.         y = round((y - 170) / SIZE)
  58.         if first:
  59.             x1 = x
  60.             y1 = y
  61.             print('x1,y1=', x1, y1)
  62.             if Board[y1][x1] != turn:
  63.                 return None
  64.             if Board[y1][x1] == turn:
  65.                 print('第一次单击')
  66.                 first = False
  67.         else:
  68.             x2 = x
  69.             y2 = y
  70.             print('x2,y2=', x2, y2)
  71.             print(turn)
  72.             if Board[y2][x2] != '0':
  73.                 return None
  74.             if turn == '红':
  75.                 reds[y1].x = 100 + x2 * SIZE
  76.                 reds[y1].y = 170 + y2 * SIZE
  77.                 redchess[y1] = x2
  78.                 Board[y1][x1] = '0'
  79.                 Board[y2][x2] = turn
  80.             elif turn == '黑':
  81.                 blacks[y1].x = 100 + x2 * SIZE
  82.                 blacks[y1].y = 170 + y2 * SIZE
  83.                 blackchess[y1] = x2
  84.                 Board[y1][x1] = '0'
  85.                 Board[y2][x2] = turn
  86.             change_side()
  87.             first = True

  88. @window.event
  89. def on_draw():
  90.     window.clear()
  91.     background_image.blit(0, 0)
  92.     batch1.draw()
  93.     batch2.draw()

  94. pyglet.app.run()
复制代码


在这个代码中,`redchess` 和 `blackchess` 用于跟踪红黑棋子的位置。`on_mouse_press` 函数在移动棋子时更新这些位置,并更新 `Board` 的状态。这样可以确保棋子在移动后不会打乱布局。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-11-3 10:55:15 | 显示全部楼层
要实现点击棋子并移动到新位置的功能,你需要在代码中进行一些调整。以下是一些关键步骤和代码示例,帮助你实现这个功能:

1. **初始化棋盘和棋子**:确保棋盘和棋子的位置在初始化时正确设置。

2. **处理鼠标点击事件**:在第一次点击时记录选中的棋子位置,第二次点击时移动棋子。

3. **更新棋盘状态**:在棋子移动后更新棋盘的状态。

以下是一个简化的实现示例:

  1. import pyglet
  2. from pyglet.window import mouse
  3. from pyglet import shapes

  4. title = "三子棋(卵子棋)"
  5. window = pyglet.window.Window(520, 640, title)
  6. window.set_location(600, 200)
  7. batch1 = pyglet.graphics.Batch()
  8. batch2 = pyglet.graphics.Batch()
  9. background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
  10. background_image = background_pattern.create_image(520, 640)
  11. SIZE = 160

  12. reds = []
  13. blacks = []
  14. shape_list = []
  15. first = True
  16. turn = '红'
  17. last_turn = '黑'
  18. redchess = [0, 1, 2]
  19. blackchess = [0, 1, 2]
  20. Board = [['红', '0', '黑'],
  21.          ['0', '红', '0'],
  22.          ['黑', '0', '红']]

  23. x1, y1, x2, y2 = 0, 0, 0, 0

  24. for i in range(3):
  25.     circle1 = shapes.Circle(100 + redchess[i] * SIZE, 170 + i * SIZE, 40, color=(255, 80, 0), batch=batch2)
  26.     circle2 = shapes.Circle(100 + blackchess[i] * SIZE, 170 + i * SIZE, 40, color=(0, 0, 0), batch=batch2)
  27.     reds.append(circle1)
  28.     blacks.append(circle2)

  29. def drawBoard():
  30.     for i in range(3):
  31.         linex = shapes.Line(i * 160 + 100, 170, i * 160 + 100, 440, width=5, color=(0, 230, 0), batch=batch1)
  32.         linex.opacity = 250
  33.         circle1 = shapes.Circle(i * 160 + 100, 490, 60, color=(0, 230, 0), batch=batch1)
  34.         circle2 = shapes.Circle(i * 160 + 100, 490, 55, color=(63, 154, 225), batch=batch1)
  35.         shape_list.append(linex)
  36.         shape_list.append(circle1)
  37.         shape_list.append(circle2)
  38.     for i in range(2):
  39.         liney = shapes.Line(100, 170 + i * 160, 420, 170 + i * 160, width=5, color=(0, 230, 0), batch=batch1)
  40.         liney.opacity = 250
  41.         shape_list.append(liney)

  42. drawBoard()

  43. def change_side():
  44.     global turn, last_turn
  45.     last_turn = turn
  46.     if turn == '红':
  47.         turn = '黑'
  48.     else:
  49.         turn = '红'

  50. @window.event
  51. def on_mouse_press(x, y, button, modifiers):
  52.     global first, x1, y1, turn, Board
  53.     if button == mouse.LEFT:
  54.         x = round((x - 100) / SIZE)
  55.         y = round((y - 170) / SIZE)
  56.         if first:
  57.             x1 = x
  58.             y1 = y
  59.             print('x1,y1=', x1, y1)
  60.             if Board[y1][x1] != turn:
  61.                 return None
  62.             if Board[y1][x1] == turn:
  63.                 print('第一次单击')
  64.                 first = False
  65.         else:
  66.             x2 = x
  67.             y2 = y
  68.             print('x2,y2=', x2, y2)
  69.             print(turn)
  70.             if Board[y2][x2] != '0':
  71.                 return None
  72.             if turn == '红':
  73.                 reds[redchess.index(x1)].x = 100 + x2 * SIZE
  74.                 reds[redchess.index(x1)].y = 170 + y2 * SIZE
  75.                 redchess[redchess.index(x1)] = x2
  76.                 Board[y1][x1] = '0'
  77.                 Board[y2][x2] = turn
  78.             elif turn == '黑':
  79.                 blacks[blackchess.index(x1)].x = 100 + x2 * SIZE
  80.                 blacks[blackchess.index(x1)].y = 170 + y2 * SIZE
  81.                 blackchess[blackchess.index(x1)] = x2
  82.                 Board[y1][x1] = '0'
  83.                 Board[y2][x2] = turn
  84.             change_side()
  85.             first = True

  86. @window.event
  87. def on_draw():
  88.     window.clear()
  89.     background_image.blit(0, 0)
  90.     batch1.draw()
  91.     batch2.draw()

  92. pyglet.app.run()
复制代码


在这个示例中,`on_mouse_press` 函数处理鼠标点击事件,第一次点击记录棋子的位置,第二次点击则移动棋子并更新棋盘状态。确保在每次移动后调用 `change_side()` 函数来切换玩家。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-3 22:28:24 | 显示全部楼层
修改的代码实现了棋子移动的功能,但将棋子布局打乱了。
原来的代码中redchess=[[0,0],[0,1],[0,2]]
blackchess=[[2,0],[2,1],[2,2]] 代表红黑子在棋盘中的位置。
即开始游戏时,红三子在左,黑三子在右。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-11-3 22:45:41 | 显示全部楼层    本楼为最佳答案   
ydwb 发表于 2024-11-03 22:28
修改的代码实现了棋子移动的功能,但将棋子布局打乱了 ...


要解决棋子布局被打乱的问题,我们需要确保在初始化时正确设置棋子的位置,并在移动棋子时正确更新它们的位置。以下是一些调整建议:

1. **初始化棋子位置**:确保 `redchess` 和 `blackchess` 在初始化时正确设置。

2. **更新棋子位置**:在移动棋子时,确保更新 `redchess` 和 `blackchess` 的位置。

以下是修改后的代码示例:

  1. import pyglet
  2. from pyglet.window import mouse
  3. from pyglet import shapes

  4. title = "三子棋(卵子棋)"
  5. window = pyglet.window.Window(520, 640, title)
  6. window.set_location(600, 200)
  7. batch1 = pyglet.graphics.Batch()
  8. batch2 = pyglet.graphics.Batch()
  9. background_pattern = pyglet.image.SolidColorImagePattern(color=(63, 154, 225, 255))
  10. background_image = background_pattern.create_image(520, 640)
  11. SIZE = 160

  12. reds = []
  13. blacks = []
  14. shape_list = []
  15. first = True
  16. turn = '红'
  17. last_turn = '黑'
  18. redchess = [0, 0, 0]  # 红棋子初始位置
  19. blackchess = [2, 2, 2]  # 黑棋子初始位置
  20. Board = [
  21.     ['红', '0', '黑'],
  22.     ['红', '0', '黑'],
  23.     ['红', '0', '黑']
  24. ]

  25. x1, y1, x2, y2 = 0, 0, 0, 0

  26. for i in range(3):
  27.     circle1 = shapes.Circle(100 + redchess[i] * SIZE, 170 + i * SIZE, 40, color=(255, 80, 0), batch=batch2)
  28.     circle2 = shapes.Circle(100 + blackchess[i] * SIZE, 170 + i * SIZE, 40, color=(0, 0, 0), batch=batch2)
  29.     reds.append(circle1)
  30.     blacks.append(circle2)

  31. def drawBoard():
  32.     for i in range(3):
  33.         linex = shapes.Line(i * 160 + 100, 170, i * 160 + 100, 440, width=5, color=(0, 230, 0), batch=batch1)
  34.         linex.opacity = 250
  35.         circle1 = shapes.Circle(i * 160 + 100, 490, 60, color=(0, 230, 0), batch=batch1)
  36.         circle2 = shapes.Circle(i * 160 + 100, 490, 55, color=(63, 154, 225), batch=batch1)
  37.         shape_list.append(linex)
  38.         shape_list.append(circle1)
  39.         shape_list.append(circle2)
  40.     for i in range(2):
  41.         liney = shapes.Line(100, 170 + i * 160, 420, 170 + i * 160, width=5, color=(0, 230, 0), batch=batch1)
  42.         liney.opacity = 250
  43.         shape_list.append(liney)

  44. drawBoard()

  45. def change_side():
  46.     global turn, last_turn
  47.     last_turn = turn
  48.     if turn == '红':
  49.         turn = '黑'
  50.     else:
  51.         turn = '红'

  52. @window.event
  53. def on_mouse_press(x, y, button, modifiers):
  54.     global first, x1, y1, turn, Board
  55.     if button == mouse.LEFT:
  56.         x = round((x - 100) / SIZE)
  57.         y = round((y - 170) / SIZE)
  58.         if first:
  59.             x1 = x
  60.             y1 = y
  61.             print('x1,y1=', x1, y1)
  62.             if Board[y1][x1] != turn:
  63.                 return None
  64.             if Board[y1][x1] == turn:
  65.                 print('第一次单击')
  66.                 first = False
  67.         else:
  68.             x2 = x
  69.             y2 = y
  70.             print('x2,y2=', x2, y2)
  71.             print(turn)
  72.             if Board[y2][x2] != '0':
  73.                 return None
  74.             if turn == '红':
  75.                 reds[y1].x = 100 + x2 * SIZE
  76.                 reds[y1].y = 170 + y2 * SIZE
  77.                 redchess[y1] = x2
  78.                 Board[y1][x1] = '0'
  79.                 Board[y2][x2] = turn
  80.             elif turn == '黑':
  81.                 blacks[y1].x = 100 + x2 * SIZE
  82.                 blacks[y1].y = 170 + y2 * SIZE
  83.                 blackchess[y1] = x2
  84.                 Board[y1][x1] = '0'
  85.                 Board[y2][x2] = turn
  86.             change_side()
  87.             first = True

  88. @window.event
  89. def on_draw():
  90.     window.clear()
  91.     background_image.blit(0, 0)
  92.     batch1.draw()
  93.     batch2.draw()

  94. pyglet.app.run()
复制代码


在这个代码中,`redchess` 和 `blackchess` 用于跟踪红黑棋子的位置。`on_mouse_press` 函数在移动棋子时更新这些位置,并更新 `Board` 的状态。这样可以确保棋子在移动后不会打乱布局。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 16:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表