使用pygame制作双人五子棋小游戏
源码如下:
import pygame
import timepygame.init()class Chess:screen = pygame.display.set_mode((690, 690))clock = pygame.time.Clock()def __init__(self):# 未下棋0,黑棋1,白棋-1self.chess_panel = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]self.chess_draw = [[(45, 45), (85, 45), (125, 45), (165, 45), (205, 45),(245, 45), (285, 45), (325, 45), (365, 45), (405, 45),(445, 45), (485, 45), (525, 45), (565, 45), (605, 45),(645, 45)], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]pygame.display.set_caption("单机双人五子棋 version:1.0")black = (0, 0, 0)bg_color = (237, 182, 102)# bg_color = (167, 123, 25)Chess.screen.fill(bg_color)# 横线a, b = 45, 645for i in range(0, 16):pygame.draw.aaline(Chess.screen, black, (a + i*40, a), (a + i*40, b), 1)# 竖线for i in range(0, 16):pygame.draw.aaline(Chess.screen, black, (a, a + i*40), (b, a + i*40), 1)self.image_hei = pygame.image.load("./images/hei.png")self.image_bai = pygame.image.load("./images/bai.png")# 点位计算for i in range(1, 16):for j in range(0, 16):self.chess_draw[i].insert(j, (self.chess_draw[0][j][0], self.chess_draw[i - 1][j][1] + 40))# print(chess_draw[i][j], end=" ")# print()# 在当前点位下棋,黑棋1,白棋-1def chess(self, tuple1, index_chess):index_i, index_j = tuple1[0], tuple1[1]pos_x, pos_y = self.chess_draw[index_i][index_j]tuple2 = pos_x - 17, pos_y - 17if index_chess == 1:Chess.screen.blit(self.image_hei, tuple2)self.chess_panel[index_i][index_j] = 1else:Chess.screen.blit(self.image_bai, tuple2)self.chess_panel[index_i][index_j] = -1# 判断当前下标位能不能下棋def judgment_index(self, tuple1):if tuple1:index_i, index_j = tuple1[0], tuple1[1]return True if self.chess_panel[index_i][index_j] == 0 else Falseelse:return False# return 当前鼠标点击位置转换为的点位下标位置def change(self, tuple1):x, y = tuple1[0], tuple1[1]for ii in range(0, 16):for jj in range(0, 16):left = self.chess_draw[ii][jj][0] - 10right = self.chess_draw[ii][jj][0] + 10top = self.chess_draw[ii][jj][1] - 10bottom = self.chess_draw[ii][jj][1] + 10if left <= x <= right and top <= y <= bottom:return ii, jj# 判断胜负def win(self):# 横for ii in range(0, 16):for jj in range(0, 11):list_slice = self.chess_panel[ii][jj:jj + 5]if list_slice[0] != 0:if list_slice.count(1) == 5:# 黑胜return 1else:if list_slice.count(-1) == 5:# 白胜return -1# 竖for ii in range(0, 11):for jj in range(0, 16):list_slice = self.chess_panel[ii][jj], self.chess_panel[ii + 1][jj], \self.chess_panel[ii + 2][jj], self.chess_panel[ii + 3][jj], self.chess_panel[ii + 4][jj]if list_slice[0] != 0:if list_slice.count(1) == 5:# 黑胜return 1else:if list_slice.count(-1) == 5:# 白胜return -1# 斜# 右下斜for ii in range(0, 12):for jj in range(0, 12):list_slice = self.chess_panel[ii][jj], self.chess_panel[ii + 1][jj + 1], \self.chess_panel[ii + 2][jj + 2], self.chess_panel[ii + 3][jj + 3], \self.chess_panel[ii + 4][jj + 4]if list_slice[0] != 0:if list_slice.count(1) == 5:# 黑胜return 1else:if list_slice.count(-1) == 5:# 白胜return -1# 左下斜for ii in range(4, 16):for jj in range(0, 12):list_slice = self.chess_panel[ii][jj], self.chess_panel[ii - 1][jj + 1], \self.chess_panel[ii - 2][jj + 2], self.chess_panel[ii - 3][jj + 3],\self.chess_panel[ii - 4][jj + 4]if list_slice[0] != 0:if list_slice.count(1) == 5:# 黑胜return 1else:if list_slice.count(-1) == 5:# 白胜return -1# 打印胜负情况@staticmethoddef print_win(win_index):if win_index is not None:text = pygame.font.SysFont("宋体", 70)if win_index == 1:text_fmt = text.render("Black win", 1, (208, 2, 27))else:text_fmt = text.render("White win", 1, (208, 2, 27))Chess.screen.blit(text_fmt, (240, 320))return Trueelse:return Falseif __name__ == '__main__':chess_obj = Chess()flag = Truewhile True:chess_obj.clock.tick(60)for event in pygame.event.get():if event.type == pygame.QUIT:exit()if event.type == pygame.MOUSEBUTTONUP and flag:tuple_point = chess_obj.change(pygame.mouse.get_pos())if chess_obj.judgment_index(tuple_point):chess_obj.chess(tuple_point, 1)flag = Falseelse:if event.type == pygame.MOUSEBUTTONUP:tuple_point = chess_obj.change(pygame.mouse.get_pos())if chess_obj.judgment_index(tuple_point):chess_obj.chess(tuple_point, -1)flag = Trueif Chess.print_win(chess_obj.win()):pygame.display.update()time.sleep(3)chess_obj = Chess()flag = Trueelse:pygame.display.update()
图片素材:
下载棋子1
下载棋子2
如果图片无法下载,请更换自己的图片素材
使用pygame制作双人五子棋小游戏相关推荐
- python3+pygame制作的连连看小游戏,好玩,酷炫,
使用Python3+pygame模块制作的连连看小游戏,好玩,酷炫,很适合初学Python的同学练习 连连看源码下载: 长按左侧二维码 2 秒 回复「连连看」即可获取源码 (非本号) 一.运行效果 二 ...
- 手把手教你使用Pygame制作飞机大战小游戏,4万字超详细讲解!
点击上方"早起Python",关注并"星标" 每日接收原创Python干货! 大家好,偷学Python系列是由小甜同学从初学者的角度学习Python的笔记,其特 ...
- cmd小游戏_使用pygame制作Flappy bird小游戏
原文链接: [Python]使用Pygame做一个Flappy bird小游戏(一)mp.weixin.qq.com 最近看到很多大佬用强化学习玩Flappy bird.所以打算也上手玩一玩,但是苦 ...
- C语言制作一个五子棋小游戏【附代码】
目录 五子棋游戏设计与实现 1.1 系统开发思路(需求分析) 1.2 系统功能设计 1.3 系统详细设计 1.3.1数据结构设计 1.4 系统实现 1.5 系统测试 五子棋游戏设计与实现 1.1 系统 ...
- 用js实现双人五子棋小游戏
这是自己自学js的时候,在网上找的js源码,由于是自学,花了数小时才把这个源码大致弄明白. 大致算法 自定义棋盘规格,直接在棋盘建新div就可以,长度宽度用计算就可以了.下棋,在div里再建class ...
- C++实现五子棋小游戏(源代码)
>求关注,求点赞,求评论< Thanks♪(・ω・)ノ 这次发个简单的 废话不多说 下为代码☟☟☟ #include <iostream> #include <conio ...
- Python简短代码实现五子棋小游戏。。。
五子棋是是一种两人对弈的纯策略型棋类游戏. 玩法介绍: 玩法一:双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成五子连线者获胜.玩法二:自己形成五子连线就替换对方任意一枚棋子.被替换的 ...
- Java实现五子棋小游戏(附思路讲解,全部代码,游戏截图)
本文章是如何实现一个单机版双人五子棋小游戏,通过Swing技术进行可视操作. 个人简介:
- python两人一碰_python运用pygame库实现双人弹球小游戏
使用python pygame库实现一个双人弹球小游戏,两人分别控制一个左右移动的挡板用来拦截小球,小球会在两板间不停弹跳,拦截失败的一方输掉游戏,规则类似于简化版的乒乓球. 因为是第一次用pygam ...
- PYTHON pygame库实战——实现双人弹球小游戏
PYTHON pygame实战--运用pygame库实现双人弹球小游戏 使用python pygame库实现一个双人弹球小游戏,两人分别控制一个左右移动的挡板用来拦截小球,小球会在两板间不停弹跳,拦截 ...
最新文章
- 菜鸟实时数仓技术架构演进
- 刘光瑞php,PHP Markdown 解析器 HyperDown
- 防止IE6出现BUG的十种常见解决方法
- Basic Level 1008. 数组元素循环右移问题 (20)
- Ubuntu 10.10(64位)编译Android 2.3
- 成功解决AttributeError: 'collections.defaultdict' object has no attribute 'iteritems'
- 正则表达式三种模式:贪婪模式、懒惰模式、独占模式
- 解决 Azure AD 在 Azure Front Door 下登录失败的问题
- 为什么到最后还是要 专注于博客写作
- vue获取子组件元素
- mysql可视化界面数据导出_MySQL 使用可视化工具导出与导入数据
- 第三方支付接口怎么测试
- OEM 13c 监控RAC部署
- 统计学中p值计算公式_统计学中的P值应该怎么计算
- php app 银联支付,php银联网页支付实现方法
- archlinux yaourt使用问题
- 禁止“无法验证发行者,确定要运行此软件吗”提示
- Windows CMD命令行进行日期计算及本件备份
- 编写shell脚本,输入一个数字n并计算1~n的和。要求:输入的数字不能小于1和空。
- [Hackthebox]获取社区邀请码