鱼C论坛

 找回密码
 立即注册
查看: 2341|回复: 5

[技术交流] 一个简单的五子棋(完成度90%)

[复制链接]
发表于 2018-3-31 22:21:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 qwe1231069 于 2018-4-2 14:21 编辑

第一次更新
基本上能玩了,判断什么的都写好了(写的很烂,以后再改)
输赢会在终端上显示出来
文件已改
===================================================================
近来想写个象棋,发现有点难。于是决定先写个五子棋

我还没写完,花了大概两晚写了个界面。发出来让新手看看
肯定还有好多地方做的不好,希望大家帮我指出来
希望一个月之内能写个简单的带AI的五子棋

一共三个文件,图片是50像素的棋子
settings

  1. class Settings():
  2.         def __init__(self):
  3.                 #屏幕设置
  4.                 self.screen_width = 1200
  5.                 self.screen_height = 800
  6.                 self.bg_color = (230,230,230)

  7.                 #棋盘设置
  8.                 #行数
  9.                 self.row_num = 10
  10.                 #列数
  11.                 self.columns_num = 10

  12.                 self.row_width = 50
  13.                 self.qipan_color = (200,0,0)
复制代码


chess.py
  1. import pygame
  2. from settings import Settings

  3. class Chess():
  4.        
  5.         def __init__(self,screen):
  6.                 #flag 0 为黑棋
  7.                 self.flag = 0
  8.                 self.screen = screen
  9.                 self.set = Settings()
  10.                 #加载棋子图片
  11.                 self.white = pygame.image.load('baiqi1.png')
  12.                 self.black = pygame.image.load('heiqi1.png')

  13.                 self.white_rect = self.white.get_rect()
  14.                 self.black_rect = self.black.get_rect()
  15.                 #棋盘坐标列表
  16.                 self.chesslist = [[0 for i in range(self.set.columns_num + 1)]for i in range(self.set.row_num + 1)]

  17.         def chessboard(self,screen):
  18.                 self.zerox = self.set.screen_width/2 - self.set.columns_num/2*self.set.row_width
  19.                 self.zeroy = self.set.screen_height/2 - self.set.row_num/2*self.set.row_width

  20.                 #画棋盘
  21.                 for i in range(0,self.set.row_num + 1):
  22.                         a = i * self.set.row_width
  23.                         pygame.draw.line(screen,self.set.qipan_color,(self.zerox,self.zeroy + a),(self.zerox + self.set.row_width * self.set.columns_num,self.zeroy + a),5)
  24.                 for i in range(0,self.set.columns_num + 1):
  25.                         a = i * self.set.row_width
  26.                         pygame.draw.line(screen,self.set.qipan_color,(self.zerox + a,self.zeroy),(self.zerox + a,self.zeroy + self.set.row_num * self.set.row_width),5)

  27.         def blitme(self,flag):
  28.                 pass

  29.         def save_chess(self):
  30.                 if self.newx < 0 or self.newy < 0 or self.newy > self.set.row_num or self.newx > self.set.columns_num or self.chesslist[self.newy][self.newx] != 0:
  31.                         return 0
  32.                 if self.flag:
  33.                         #bai qi  -1
  34.                         self.chesslist[self.newy][self.newx] = -1
  35.                 else:               
  36.                         self.chesslist[self.newy][self.newx] = 1
  37.                 #print(self.chesslist)

  38.         def coordinate(self):
  39.                 #棋子坐标由鼠标决定
  40.                 x,y = pygame.mouse.get_pos()
  41.                 #鼠标点击地方的棋子坐标
  42.                 self.newx = int((x + 25 - self.zerox)//50)
  43.                 self.newy = int((y + 25 - self.zeroy)//50)
  44.                
  45.                 #print(self.newx,self.newy)
  46.                 if self.newx < 0 or self.newy < 0 or self.newy > self.set.row_num or self.newx > self.set.columns_num or self.chesslist[self.newy][self.newx] != 0:
  47.                         return 0
  48.                 self.white_rect.centerx = self.newx * 50 + self.zerox
  49.                 self.white_rect.bottom = self.newy * 50 + self.zeroy + 25
  50.                 self.black_rect.centerx = self.newx * 50 + self.zerox
  51.                 self.black_rect.bottom = self.newy * 50 + self.zeroy + 25
  52.                 if self.flag:
  53.                         #flag 1 为白色
  54.                         self.screen.blit(self.white,self.white_rect)               
  55.                 else:
  56.                         self.screen.blit(self.black,self.black_rect)               
  57.                 self.flag = ~self.flag
  58.                 return 1
  59.        
  60.         def win(self):
  61.                 #x,y是坐标        i,j是移动方向        num是此方向个数
  62.                 def a(x,y,i,j,num):
  63.                         while(x >= 0 and y >= 0 and x <= self.set.columns_num and y <= self.set.row_num):
  64.                                 a = self.chesslist[y][x]
  65.                                 y = y + j
  66.                                 x = x + i
  67.                                 if x < 0 or y < 0 or x > self.set.columns_num or y > self.set.row_num:
  68.                                         break
  69.                                 b = self.chesslist[y][x]
  70.                                 if a == b:
  71.                                         num = num + 1       
  72.                                 else:
  73.                                         break
  74.                         return num


  75.                 #白棋赢了-1 没人获胜 0
  76.                 winflag = 0
  77.                 i = self.chesslist[self.newy][self.newx]

  78.                 x = self.newx
  79.                 y = self.newy
  80.                 numi = a(x,y,0,-1,0) + a(x,y,0,1,0) + 1
  81.                 nums = a(x,y,-1,0,0) + a(x,y,1,0,0) + 1
  82.                 num1 = a(x,y,-1,-1,0) + a(x,y,1,1,0) + 1
  83.                 num2 = a(x,y,-1,1,0) + a(x,y,1,-1,0) + 1

  84.                 if numi > 4 or nums > 4 or num1 > 4 or num2 > 4:
  85.                         winflag = i
  86.                         return winflag
  87.                 return 0
复制代码


wuziqi.py
  1. #五子棋
  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. from settings import Settings
  6. from chess import Chess
  7. def run_game():
  8.         global flag
  9.        
  10.         #创建屏幕对象
  11.         pygame.init()
  12.         qiset = Settings()
  13.         screen = pygame.display.set_mode((qiset.screen_width,qiset.screen_height))
  14.         pygame.display.set_caption("五子棋")
  15.         chess = Chess(screen)
  16.         a = 0
  17.                
  18.         #bg_color
  19.         screen.fill(qiset.bg_color)
  20.         #绘制棋盘
  21.         chess.chessboard(screen)       
  22.         #main()
  23.         while True:
  24.                 for event in pygame.event.get():
  25.                         if event.type == pygame.QUIT:
  26.                                 sys.exit()
  27.                         elif event.type == MOUSEBUTTONDOWN:
  28.                                 pressed_array = pygame.mouse.get_pressed()
  29.                                 for i in range(len(pressed_array)):
  30.                                         if pressed_array[i]:
  31.                                                 if i == 0:
  32.                                                         #left button
  33.                                                         #chess.blitme(flag)
  34.                                                         a=chess.coordinate()
  35.                                                         if a:
  36.                                                                 chess.save_chess()
  37.                                                                 b = chess.win()
  38.                                                                 if b == 1:
  39.                                                                         print('白棋赢了')
  40.                                                                 elif b == -1:
  41.                                                                         print('黑棋赢了')
  42.                                                 elif i == 1:
  43.                                                         #mouse wheel
  44.                                                         pass
  45.                                                 elif i == 2:
  46.                                                         pass
  47.                                                         #right button
  48.                 #让最近绘制的屏幕可见
  49.                 pygame.display.flip()
  50. run_game()
复制代码

2018-04-01 17-56-47屏幕截图.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-31 22:28:33 | 显示全部楼层
本帖最后由 qwe1231069 于 2018-3-31 22:32 编辑

其实我有git的
https://github.com/enotsGNU/python.git
只看五子棋就好了,其他的写的有点差。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-1 10:50:04 From FishC Mobile | 显示全部楼层
路过学习,写的很好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-1 18:04:13 | 显示全部楼层
BngThea 发表于 2018-4-1 10:50
路过学习,写的很好

能不能帮我看看是什么问题
我一运行这东西cpu占用率直接到100%,我笔记本风扇就开始响
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-2 12:06:45 | 显示全部楼层
qwe1231069 发表于 2018-4-1 18:04
能不能帮我看看是什么问题
我一运行这东西cpu占用率直接到100%,我笔记本风扇就开始响

你这么配置,不会是0几年的老电脑了吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-2 12:08:06 | 显示全部楼层
qwe1231069 发表于 2018-4-1 18:04
能不能帮我看看是什么问题
我一运行这东西cpu占用率直接到100%,我笔记本风扇就开始响

你可以换个角度,优化你的代码。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 00:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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