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()

最新文章

  1. 求两个数字的最大公约数-Python实现,三种方法效率比较,包含质数打印质数的方法
  2. C#程序开发中经常遇到的10条实用的代码
  3. &#39;hibernate.dialect&#39; must be set when no Connection avalable’
  4. job interview
  5. 一台Ubuntu server上安装多实例MySQL
  6. mysql create database 指定utf-8编码
  7. Leap Years
  8. (转) 值不能为空。参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explorer)
  9. [知识库分享系列] 二、Web(高性能Web站点建设)
  10. hdu4223(dp)
  11. Lucene入门教程
  12. Hadoop权威指南:压缩
  13. python回归分析五部曲
  14. JavaScrip相关知识总结
  15. 微信公众号申请+新浪SAE申请
  16. 找回Android studio的帮助文档
  17. Python 基础关于编码
  18. 第二个spring冲刺第5天
  19. Difference between ReLU、LReLU、PReLU、CReLU、ELU、SELU
  20. Redis之List 列表

热门文章

  1. 使用Xshell和Xftp部署简单的项目
  2. ruby 数据sql操作
  3. Java 获取汉字串首字母并大写和获取汉字的全拼,英文字符不变
  4. Firebird 获取用户表及字段
  5. SpringBoot访问NoSQL和简单的Thymeleaf-Spring-Spring-boot整合
  6. .net core 2.2 部署CentOS7(5)部署.net core mvc
  7. [日常] MySQL的哈希索引和原理研究测试
  8. Java学习-jsp内置对象Session
  9. ajax请求下载Execl表
  10. JAVA成员变量的隐藏