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. MYSQL根据字段名查询所属表

    MYSQL里面需要根据某个字段名,查询该字段名所在的表.这种情况主要是出现在比如你忘了表名,只知道有这样一个字段名,想找出那张表.第二种情况可能是,同一个字段名属于外键,你想找出例如  ID 这个字段 ...

  2. eclipse + maven 环境配置

    软件152 余建强 第一步:准备以下软件并进行安装 1. jdk1.7或者以上为最佳: 官方下载地址:http://www.oracle.com/technetwork/java/javase/dow ...

  3. ZJOI2018 Round2 游记

    day0 高铁上颓了一部电影,然后闭上眼睛就到了 醒来之后发现被绑了艹,袖子被打了个结,搞了 \(20\) 分钟才解开,真想把绑我的人吊起来 \(xxx\) 公交车上碰到一位长者,被教育了一顿 长者: ...

  4. 从数据库表中随机获取N条记录的SQL语句

    Oracle: select * from (select * from tableName order by dbms_random.value) where rownum < N MS SQ ...

  5. WebForm控件多字段绑定

    一.这里的多字段绑定是什么意思? 多字段绑定控件其实就是把两个字段显示在一起作为一个字段现在控件上! 可能读者看了可能还是有点懵逼,说的还是比较抽象!的确,光从这上面的确是无法具体到某特定一种情况!那 ...

  6. Java Swing实战(二)下拉菜单组件JComboBox及其事件监听

    接下来给"数据源配置"面板添加下拉框. /** * @author: lishuai * @date: 2018/11/26 13:51 */ public class Weimi ...

  7. java基础之运算符与语句

    一.运算符 1.算数运算符 运算符 名称 举例 + 加法 A等于10,B等于3 则A+B=13 - 减法 A等于10,B等于3 则A-B=7 * 乘法 A等于10,B等于3 则A*B=30 / 除法 ...

  8. Java - Float与Double类型比较

    https://blog.csdn.net/wcxiaoych/article/details/42806313

  9. Java 中 List 和 数组之间的转换

    前言:在 java 开发过程中,经常会遇到数组和 list 互转的情况,这里记录一下,免得以后又去到处百度. 一.数组转为 LIST 这里转自 https://www.cnblogs.com/lius ...

  10. Python 创建字典的多种方式

    1.通过关键字dict和关键字参数创建 >>> dic = dict(spam = 1, egg = 2, bar =3) >>> dic {'bar': 3, ' ...