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】 简易五子棋
猜你喜欢
转载自www.cnblogs.com/tao-zhu-forever/p/9008143.html
今日推荐
周排行