import pygame
import sys
import time
import random pygame.init()
screen_size = (800,560)
WIDTH = 720
HEIGHT = 720
GRID_WIDTH = WIDTH // 20 WHITE = (255, 250, 255)
BLACK = (0, 0, 0)
GREEN = (0, 0xff, 0)
RED = (0xff, 0, 0)
color = [BLACK,WHITE]
class checker:
def __init__(self,x,y):
self.ox = x
self.oy = y
self.color = (0,0,0)
self.state = 0 # 0 1 2 checks = []
colorful = []
aiful = []
for i in range(1,16):
for j in range(1,16):
achecker = checker(i,j)
checks.append(achecker)
colorful.append(0)
aiful.append(0)
for i in range(1,16):
colorful.append(0)
aiful.append(0)
movements=[]
movements1 = []
movements2 = [] screen = pygame.display.set_mode(screen_size,0, 32)
pygame.display.set_caption('欢乐五子棋')
clock = pygame.time.Clock()
bg = pygame.image.load("image\\checker.jpg").convert()
weixiao = pygame.image.load("image\\weixiao.jpg").convert()
huaji = pygame.image.load("image\\huaji.jpg").convert()
arrow = pygame.image.load("image\\arrow.jpg").convert()
bgcolor = (255,255,255) turn = 0
def draw_check(surf):
screen.fill(bgcolor)
surf.blit(bg, (0, 0))
def draw_column(surf,where):
surf.blit(huaji, (650, 100))
surf.blit(weixiao, (650, 300))
if where == 1:
surf.blit(arrow, (580, 100))
else:
surf.blit(arrow, (580, 300))
def draw_checker(surf):
for c in checks:
if c.state == 1:
pygame.draw.circle(surf, c.color, (c.ox * 35, c.oy * 35), 10, 0)
if c.state == 2:
pygame.draw.circle(surf, c.color, (c.ox * 35, c.oy * 35), 10, 0)
def tmax(a):
max = a[0]
for i in a:
if max < i:
max = i
return max
def Ai_choose():
for i in aiful:
i = 0
if len(movements) == 0:
return (7,7)
for i in movements1:
aiful[i[0]+i[1]*15] = 0
if i[0] > 0:
aiful[i[0]-1+i[1]*15] +=1
if i[0] < 15:
aiful[i[0]+1 + i[1] * 15] += 1
if i[1] > 0:
aiful[i[0]+ (i[1]-1) * 15] += 1
if i[1] < 15:
aiful[i[0] + (i[1] + 1) * 15] += 1
c = tmax(aiful)
print(c) def check_win(x,y):
i = x
j = y
count = 0
ck = colorful[x+y*15]
if ck == 0:
return False
#横着
while i>=0 and colorful[i+y*15] == ck:
i -= 1
count += 1
i = x+1
while i< 15 and colorful[i+y*15] == ck:
i += 1
count += 1
if count >= 5:
print("win")
return True
#竖着
i = x
count = 0
while j>=0 and colorful[i+15*j] == ck:
j-=1
count +=1
j = y+1
while j < 15 and colorful[i+15*j] == ck:
j += 1
count +=1
if count >= 5:
print("win")
return True
#斜着左上
j = y
count = 0
while i >= 0 and j >=0 and colorful[i +15* j] == ck:
j -= 1
i -= 1
count += 1
i = x+1
j = y+1
while i< 15 and j <15 and colorful[i+15*j] == ck:
i += 1
j +=1
count += 1
if count >= 5:
print("win")
return True
#斜着右上
j = y
i = x
count = 0
while i >= 0 and j <15 and colorful[i + 15 * j] == ck:
i -= 1
j += 1
count += 1
i = x + 1
j = y - 1
while i< 15 and j >0 and colorful[i+15*j] == ck:
i += 1
j -=1
count += 1
if count >= 5:
print("win")
return True
return False
runing = 1
Ai_wait = 0
while runing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if turn == 1:
pos = event.pos
grid = (int(round(pos[0] / 35)), int(round(pos[1] / 35)))
for c in checks:
if c.state == 0:
if c.ox == grid[0] and c.oy == grid[1]:
movements.append(grid)
movements2.append(grid)
colorful[(c.ox) + (c.oy) * 15] = 2
c.color = WHITE
c.state = 2
turn = 0
if True == check_win(c.ox,c.oy):
runing = 0
print("bingo")
if turn == 0:
if Ai_wait == 30:
Ai_wait= 0
#pos = Ai_choose()
grid = pos
for i in movements:
while i == pos:
pos = (random.randint(1, 19), random.randint(1, 19))
grid = pos
for c in checks:
if c.state == 0:
if c.ox == grid[0] and c.oy == grid[1]:
movements.append(grid)
movements1.append(grid)
colorful[(c.ox) + (c.oy) * 15] = 1
c.color = BLACK
c.state = 1
turn = 1
if True == check_win(c.ox, c.oy):
runing = 0
print("bingo")
else:
Ai_wait+=1
draw_check(screen)
draw_column(screen,turn)
draw_checker(screen)
pygame.display.update()
clock.tick(50) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

【Pygame】 简易五子棋的更多相关文章

  1. 简易五子棋 V1.1.0

    main.cpp #include "fivechess.cpp" int main() { fivechess a; a.RunGame(); getchar(); return ...

  2. Web版简易五子棋

    前些时候把大三写的C++版五子棋改成Web板挂到了网上,具有一定傻瓜式智能,欢迎体验使用拍砖:http://www.zhentiyuan.com/Games/QuickFiveChess.aspx 现 ...

  3. c语言进阶4-有返回值函数

    一.         从函数返回 从函数返回就是返回语句的第一个主要用途.在程序中,有两种方法可以终止函数的执行,并返回到调用函数的位置.第一种方法是在函数体中,从第一句一直执行到最后一句,当所有语句 ...

  4. c语言进阶3-有参函数

    一.       有参函数的定义 有参函数的定义格式如下: 类型标识符  函数名(形式参数表列) { 语句: } 如 void fun(int a,int b) { printf(“a+b=%d”,a ...

  5. html+js+node实现五子棋线上对战,五子棋最简易算法

    首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...

  6. [收藏]C++简单五子棋

    #include<iostream> #include<iomanip> using namespace std; ; //棋盘行数 ; //棋盘列数 char p[X][Y] ...

  7. [深度学习]实现一个博弈型的AI,从五子棋开始(2)

    嗯,今天接着来搞五子棋,从五子棋开始给小伙伴们聊AI. 昨天晚上我们已经实现了一个五子棋的逻辑部分,其实讲道理,有个规则在,可以开始搞AI了,但是考虑到不够直观,我们还是顺带先把五子棋的UI也先搞出来 ...

  8. Python:游戏:五子棋之人机对战

    本文代码基于 python3.6 和 pygame1.9.4. 五子棋比起我之前写的几款游戏来说,难度提高了不少.如果是人与人对战,那么,电脑只需要判断是否赢了就可以.如果是人机对战,那你还得让电脑知 ...

  9. 贪吃蛇(简易版)Leslie5205912著

    # include <stdio.h># include <string.h># include <windows.h># include <stdlib.h ...

随机推荐

  1. C#正则表达式合并连续空格为单个空格

    第一种方法: 使用 System.Text.RegularExpressions.Regex.Replace()方法 string result = String.Empty; string str ...

  2. 这个PHP无解深坑,你能解出来吗?(听说能解出来的都很秀)

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由horstxu发表于云+社区专栏 1. 问题背景 PHP Laravel框架中的db migration是比较常用的一个功能了.在每个 ...

  3. elasticsearch版本不同,批量查询也不相同

    网上搜到批量查询可以通过TransportClient实现,但官方推荐使用RestHighLevelClient实现 注意: We plan on deprecating the TransportC ...

  4. RabbitMQ 上手记录-part 1-基础概念

    RabbitMQ是什么,不用多介绍了,毕竟名声在那,江湖地位摆着,搜索引擎收录着.为什么突然去学习这个框架了,毕竟工作中没有用得上(说来也惭愧,工作中开发的项目没有使用这个框架).但是作为互联网分布式 ...

  5. Qt Model View 框架

    Model-View及Qt实现 Model-View-Controller架构最早出现在SmallTalk语言中,至今出现了很多变体. Model是负责维护数据(如管理数据库),View负责显示与用户 ...

  6. JS中的拖动之—— ondragstart,ondrag,ondragend , ondragenter , ondragover , ondragleave, ondrop 的区别

    关于 HTML5 中的拖动功能. 更多信息可以查看我们 HTML 教程中的 HTML5 拖放.以下 我只做一下简介. 1 如果你想让元素变得可拖动,首先 你得对元素设置 draggable 属性 此属 ...

  7. Java的工厂模式(一)

    Java的工厂模式在框架中是用的到很多的,所谓的工厂模式,其实也就是用一个接口来创建对象,把实例化的工作推迟到子类去实现.这样在主函数中就可以直接创建一个工厂类,再通过这个工厂类实现操作. 假设有一个 ...

  8. 统计SQL Server所有表记录数

    SELECT SCHEMA_NAME(t.schema_id) AS [schema] ,t.name AS tableName ,i.rows AS [rowCount] FROM sys.tabl ...

  9. css 文本和div垂直居中方法汇总

    https://blog.csdn.net/u014607184/article/details/51820508 https://blog.csdn.net/liuying1802028915/ar ...

  10. SQL Server 2017搭建主从备份

    SQL Server 2017搭建主从¶ 关于日志传输¶ 和Oracle DG,Mysql主从一样,SQL Server也支持主从的高可用.进一步提高数据的安全性和业务的高可用.通过将主库上的日志传输 ...