python实现五子棋小游戏


Posted in Python onMarch 25, 2020

本文实例为大家分享了python实现五子棋小游戏的具体代码,供大家参考,具体内容如下

暑假学了十几天python,然后用pygame模块写了一个五子棋的小游戏,代码跟有缘人分享一下。

import numpy as np
import pygame
import sys
import traceback
import copy
from pygame.locals import *


pygame.init()
pygame.mixer.init()

#颜色
background=(201,202,187)
checkerboard=(80,80,80)
button=(52,53,44)



#音乐
play_chess_sound = pygame.mixer.Sound("music/play_chess.wav")
play_chess_sound.set_volume(0.2)
button_sound = pygame.mixer.Sound("music/button.wav")
button_sound.set_volume(0.2)
victor_sound = pygame.mixer.Sound("music/victory.wav")
victor_sound.set_volume(0.2)

#绘制棋盘
def Draw_a_chessboard(screen): 
 #填充背景色
 screen.fill(background)
 Background=pygame.image.load("background.jpg").convert_alpha()
 screen.blit(Background,(0,0))
 #画棋盘
 for i in range(21):
 pygame.draw.line(screen, checkerboard, (40*i+3, 3), (40*i+3, 803)) 
 pygame.draw.line(screen, checkerboard, (3, 40*i+3), (803, 40*i+3))
 #画边线
 pygame.draw.line(screen, checkerboard, (3, 3), (803, 3),5) 
 pygame.draw.line(screen, checkerboard, (3, 3), (3, 803),5) 
 pygame.draw.line(screen, checkerboard, (803, 3), (803, 803),5) 
 pygame.draw.line(screen, checkerboard, (3, 803), (803, 803),5) 
 
 #画定位点
 pygame.draw.circle(screen, checkerboard, (163, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (163, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 163), 6) 
 pygame.draw.circle(screen, checkerboard, (643, 643), 6) 
 pygame.draw.circle(screen, checkerboard, (403, 403), 6) 
 
 #画‘悔棋'‘重新开始'跟‘退出'按钮
 pygame.draw.rect(screen,button,[900,350,120,100],5)
 pygame.draw.rect(screen,button,[900,500,200,100],5)
 pygame.draw.rect(screen,button,[900,650,200,100],5)
 s_font=pygame.font.Font('font.ttf',40)
 text1=s_font.render("悔棋",True,button)
 text2=s_font.render("重新开始",True,button)
 text3=s_font.render("退出游戏",True,button)
 screen.blit(text1,(920,370))
 screen.blit(text2,(920,520))
 screen.blit(text3,(920,670))

#绘制棋子(横坐标,纵坐标,屏幕,棋子颜色(1代表黑,2代表白))
def Draw_a_chessman(x,y,screen,color): 
 if color==1: 
 Black_chess=pygame.image.load("Black_chess.png").convert_alpha()
 screen.blit(Black_chess,(40*x+3-15,40*y+3-15))
 if color==2:
 White_chess=pygame.image.load("White_chess.png").convert_alpha()
 screen.blit(White_chess,(40*x+3-15,40*y+3-15))

#绘制带有棋子的棋盘
def Draw_a_chessboard_with_chessman(map,screen): 
 screen.fill(background)
 Draw_a_chessboard(screen)
 for i in range(24):
 for j in range(24):
 Draw_a_chessman(i+1,j+1,screen,map[i][j])



#定义存储棋盘的列表,
#列表为24列24行是因为判断是否胜利函数里的索引会超出19
#列表大一点不会对游戏有什么影响
map=[]
for i in range(24):
 map.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])

#清零map列表
def clear():
 global map
 for i in range(24):
 for j in range(24):
 map[i][j]=0

#判断是否胜利
def win(i, j):
 k = map[i][j]
 p=[]
 for a in range(20):
 p.append(0)
 for i3 in range(i-4,i+5):
 for j3 in range(j-4,j+5):
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 <= i and j3 <= j):
 p[0]+=1
 if (map[i3][j3] == k and j3 == j and i3 <= i and j3 <= j):
 p[1]+=1
 if (map[i3][j3] == k and i3 == i and i3 <= i and j3 <= j):
 p[2]+=1
 if (map[i3][j3] == k and i3 - i == j3 - j and i3 >= i and j3 >= j):
 p[3]+=1
 if (map[i3][j3] == k and j3 == j and i3 >= i and j3 >= j):
 p[4]+=1
 if (map[i3][j3] == k and i3 == i and i3 >= i and j3 >= j):
 p[5]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i and j3 >= j):
 p[6]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i and j3 <= j):
 p[7]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[8]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[9]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 1 and i3 >= i - 3 and j3 <= j + 1 and j3 >= j - 3):
 p[10]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[11]+=1
 if (map[i3][j3] == k and j == j3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[12]+=1
 if (map[i3][j3] == k and i == i3 and i3 >= i - 1 and i3 <= i + 3 and j3 >= j - 1 and j3 <= j + 3):
 p[13]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 1 and i3 >= i - 3 and j3 >= j - 1 and j3 <= j + 3):
 p[14]+=1
 if (map[i3][j3] == k and i3 - i == j - j3 and i3 >= i - 1 and i3 <= i + 3 and j3 <= j + 1 and j3 >= j - 3):
 p[15]+=1
 if (map[i3][j3] == k and j - j3 == i - i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[16]+=1
 if (map[i3][j3] == k and j == j3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[17]+=1
 if (map[i3][j3] == k and i == i3 and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[18]+=1
 if (map[i3][j3] == k and i - i3 == j3 - j and i3 <= i + 2 and i3 >= i - 2 and j3 <= j + 2 and j3 >= j - 2):
 p[19]+=1
 for b in range(20):
 if p[b]==5:
 return True
 return False

#绘制提示器(类容,屏幕,字大小)
def text(s,screen,x):
 #先把上一次的类容用一个矩形覆盖
 pygame.draw.rect(screen,background,[850,100,1200,100])
 #定义字体跟大小
 s_font=pygame.font.Font('font.ttf',x)
 #定义类容,是否抗锯齿,颜色
 s_text=s_font.render(s,True,button)
 #将字放在窗口指定位置
 screen.blit(s_text,(880,100))
 pygame.display.flip()

#用于控制顺序
t=True

#用于结束游戏后阻止落子
running=True

#主函数
def main():
 #将 t,map,running设置为可改的
 global t,map,running,maps,r,h
 #将map置零
 clear()
 #定义储存所有棋盘状态的列表(用于悔棋)
 map2=copy.deepcopy(map)
 maps=[map2]

 
 #定义窗口
 screen = pygame.display.set_mode([1200,806])
 
 #定义窗口名字
 pygame.display.set_caption("五子棋")
 
 #在窗口画出棋盘,提示器以及按钮
 Draw_a_chessboard(screen)
 pygame.display.flip()
 clock=pygame.time.Clock()
 while True:
 #只有running为真才能落子,主要用于游戏结束后防止再次落子
 if running:
 if t:
 color=1
 text('黑棋落子',screen,54)
 else:
 color=2
 text('白棋落子',screen,54)
 
 for event in pygame.event.get():
 #点击x则关闭窗口
 if event.type ==pygame.QUIT:
 pygame.quit()
 sys.exit()
 
 #点击窗口里面类容则完成相应指令
 elif event.type == MOUSEBUTTONDOWN:
 if event.button == 1:
 x,y=event.pos[0],event.pos[1]
 for i in range(19):
 for j in range(19):
 #点击棋盘相应位置
 if i*40+3+20<x<i*40+3+60 and j*40+3+20<y<j*40+3+60 and not map[i][j] and running:
 #在棋盘相应位置落相应颜色棋子
 Draw_a_chessman(i+1,j+1,screen,color)
 #播放音效
 play_chess_sound.play(0)
 #在map里面记录落子位置
 map[i][j]=color

 #将map存入maps
 map3=copy.deepcopy(map)
 maps.append(map3)

 #判断落子后是否有五子一线
 if win(i,j):
  if t:
  text('黑棋胜利,请重新游戏',screen,30)
  else:
  text('白棋胜利,请重新游戏',screen,30)
  #播放音效
  victor_sound.play(0)
  #阻止再往棋盘落子
  running=False
 pygame.display.flip()
 t=not t
 #如果点击‘重新开始'
 if 900<x<1100 and 500<y<600:
 #取消阻止
 running=True
 #播放音效
 button_sound.play(0)
 #重新开始
 main()
 
 #点击‘退出游戏',退出游戏
 elif 900<x<1100 and 650<y<750:
 #播放音效
 button_sound.play(0)
 pygame.quit()
 sys.exit()
 
 #点击‘悔棋'
 elif 900<x<1020 and 350<y<450 and len(maps)!=1:
 #播放音效
 button_sound.play(0)
 #删除maps里最后一个元素
 del maps[len(maps)-1] 
 #再将最后一个元素copy给map
 map=copy.deepcopy(maps[len(maps)-1])
 #切换顺序
 t=not t
 #将map显示出来
 Draw_a_chessboard_with_chessman(map,screen)
 #悔棋完成,阻止再次悔棋
 x,y=0,0
 clock.tick(60)
if __name__ == "__main__":
 try:
 main()
 except SystemExit:
 pass
 except:
 traceback.print_exc()
 pygame.quit()
 input()

实现效果图如下:

python实现五子棋小游戏

更多关于python游戏的精彩文章请点击查看以下专题:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用mysqldb连接数据库操作方法示例详解
Dec 03 Python
Django验证码的生成与使用示例
May 20 Python
同时安装Python2 &amp; Python3 cmd下版本自由选择的方法
Dec 09 Python
python如何统计序列中元素
Jul 31 Python
Python Django框架url反向解析实现动态生成对应的url链接示例
Oct 18 Python
使用python制作一个解压缩软件
Nov 13 Python
Python实现变声器功能(萝莉音御姐音)
Dec 05 Python
python、PyTorch图像读取与numpy转换实例
Jan 13 Python
解决python图像处理图像赋值后变为白色的问题
Jun 04 Python
虚拟机下载python是否需要联网
Jul 27 Python
Python 如何创建一个简单的REST接口
Jul 30 Python
pycharm导入源码的具体步骤
Aug 04 Python
PyQt5实现五子棋游戏(人机对弈)
Mar 24 #Python
Python制作exe文件简单流程
Jan 24 #Python
PyQt5实现类似别踩白块游戏
Jan 24 #Python
实例讲解Python脚本成为Windows中运行的exe文件
Jan 24 #Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 #Python
Python爬虫实战之12306抢票开源
Jan 24 #Python
python+pyqt5实现24点小游戏
Jan 24 #Python
You might like
php使用post数组的键值创建同名变量并赋值的方法
2015/04/03 PHP
thinkPHP实现MemCache分布式缓存功能
2016/03/23 PHP
PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
2016/09/11 PHP
php和asp语法上的区别总结
2019/05/12 PHP
关于laravel模板中生成URL的几种模式总结
2019/10/18 PHP
jquery.ui.draggable中文文档(原文翻译)
2013/11/15 Javascript
前台js对象在后台转化java对象的问题探讨
2013/12/20 Javascript
javascript常用函数归纳整理
2014/10/31 Javascript
JavaScript中的索引数组、关联数组和静态数组、动态数组讲解
2014/11/08 Javascript
js实现页面跳转的五种方法推荐
2016/03/10 Javascript
jQuery Ztree行政地区树状展示(点击加载)
2016/11/09 Javascript
Javascript oop设计模式 面向对象编程简单实例介绍
2016/12/13 Javascript
Vue计算属性的学习笔记
2017/03/22 Javascript
微信小程序 数据遍历的实现
2017/04/05 Javascript
vue v-model表单控件绑定详解
2017/05/17 Javascript
JavaScript指定断点操作实例教程
2018/09/18 Javascript
vue添加axios,并且指定baseurl的方法
2018/09/19 Javascript
微信小程序发送短信验证码完整实例
2019/01/07 Javascript
ES2020系列之空值合并运算符 '??'
2020/07/22 Javascript
js对象属性名驼峰式转下划线的实例代码
2020/09/17 Javascript
vue实现标签云效果的示例
2020/11/09 Javascript
python基于queue和threading实现多线程下载实例
2014/10/08 Python
用Python的SimPy库简化复杂的编程模型的介绍
2015/04/13 Python
Python3实现的字典遍历操作详解
2018/04/18 Python
详解django中使用定时任务的方法
2018/09/27 Python
Python压缩模块zipfile实现原理及用法解析
2020/08/14 Python
基于python实现图片转字符画代码实例
2020/09/04 Python
美国最大的团购网站:Groupon
2016/07/23 全球购物
台湾菁英交友:结识黄金单身的台湾人
2018/01/22 全球购物
Zatchels官网:英国剑桥包品牌
2021/01/12 全球购物
护士个人自我鉴定
2014/03/24 职场文书
岗位说明书范文
2014/05/07 职场文书
大学生党员承诺书
2014/05/20 职场文书
社会发展项目建议书
2014/08/25 职场文书
2019幼儿教师求职信(3篇)
2019/09/20 职场文书
pytorch 两个GPU同时训练的解决方案
2021/06/01 Python