五子棋是长玩的一种游戏,以下代码是基于python实现的一个源码,供大家参考: 非AI版本
1。界面显示,图像及音效
2.简单二维数组模拟棋盘
3.逻辑实现黑白棋轮流下棋
4.简单算法判断八个方向五子连线
5.游戏胜利及重置逻辑
import pygame from pygame.locals import * from pygame import Rect import sys pygame.init() screen = pygame.display.set_mode((800,800)) bac = pygame.image.load('images/bacc.png') black = pygame.image.load('images/black.png') white = pygame.image.load('images/white.png') bac2 = pygame.image.load('images/1.png') bac2 = pygame.transform.smoothscale(bac2,(800,800)) new_black = pygame.image.load('images/new_black.png') new_white = pygame.image.load('images/new_white.png') #加载声音 sound=pygame.mixer.Sound('images/duang.wav') sound1 = pygame.mixer.Sound('images/9586.wav') #lst = [] #for i in range(19): #lst.append([]) #for j in range(19): #lst[i].append(0) lst = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] def is_win(f,s): #print('*'*50) num = 5 flag = 1 new_kind = lst[f][s] # 0 1 2 #判断方向有八个 #1.向右 for i in range(1,num): if i+s<=18: if lst[f][i+s] == new_kind: flag+=1 else: break #2.向左 for i in range(1,num): if s-i>=0: if lst[f][s-i] == new_kind: flag+=1 else: break if flag>=num: return True else: flag = 1 #3.向上 for i in range(1,num): if f-i>=0: if lst[f-i][s] == new_kind: flag+=1 else: break #4.向下 for i in range(1,num): if i+f<=18: if lst[f+i][s] == new_kind: flag+=1 else: break if flag>=num: return True else: flag = 1 #5.左向上 for i in range(1,num): if f-i>=0 and s-i>=0: if lst[f-i][s-i] == new_kind: flag+=1 else: break #6.右向下 for i in range(1,num): if f+i<=18 and s+i<=18: if lst[f+i][s+i] == new_kind: flag+=1 else: break if flag>=num: return True else: flag = 1 #7.右向上 for i in range(1,num): if f-i>=0 and s+i<=18: if lst[f-i][s+i] == new_kind: flag+=1 else: break #8.左向下 for i in range(1,num): if f+i<=18 and s-i>=0: if lst[f+i][s-i] == new_kind: flag+=1 else: break if flag>=num: return True else: flag = 1 return False def get_pos(): for i in range(19): for j in range(19): if Rect(20+i*40,20+j*40,40,40).collidepoint(event.pos): return j,i while True: #screen.fill((0,250,150)) screen.blit(bac2,(0,0)) dm = 'START' #dm1 = 'Gobang' f1 = pygame.font.Font(None,60) f2 = f1.render(dm,True,(255,0,0)) #f3 = f1.render(dm1,True,(255,0,0)) screen.blit(f2,(300,400)) #screen.blit(f3,(280,100)) r1 = Rect(235,390,250,60) pygame.draw.rect(screen,(255,0,0),r1,4) event = pygame.event.poll() if event.type == MOUSEBUTTONDOWN: if r1.collidepoint(event.pos): break pygame.display.update() put_black = True winner = '' new_f = 0 mew_s = 0 print('--------------game_start---------------') while True: screen.blit(bac,(0,0)) #for i in range(19): #for j in range(19): #rr = Rect(20+i*40,20+j*40,40,40) #pygame.draw.rect(screen,(255,0,0),rr,5) for j in range(19): for i in range(19): if lst[i][j] == 1: screen.blit(black,(20+j*40,20+i*40)) if lst[i][j] == 2: screen.blit(white,(20+j*40,20+i*40)) event = pygame.event.poll() if event.type == MOUSEBUTTONDOWN and event.button == 1 and not winner: sound.play() #if get_pos(): f,s = get_pos() if not lst[f][s]: new_f = f mew_s = s #print(f,s) if put_black: lst[f][s] = 1 else: lst[f][s] = 2 if is_win(f,s):#如果函数返回值 是 True 证明黑或白已经连成五个棋子 sound1.play() print('-----------win------------') if put_black: winner = 'black' else: winner = 'white' put_black = not put_black if new_f: if not put_black: screen.blit(new_black,(20+mew_s*40,20+new_f*40)) else: screen.blit(new_white,(20+mew_s*40,20+new_f*40)) if winner: str1 = 'return_restart' dm = winner+'_win' fun1 = pygame.font.Font(None,100) fun2 = fun1.render(dm,True,(0,0,0)) fun3 = fun1.render(str1,True,(255,0,0)) while True: screen.blit(fun2,(230,200)) screen.blit(fun3,(180,300)) pygame.display.update() event = pygame.event.poll() if event.type == KEYDOWN and event.key == K_RETURN: put_black = True winner = '' new_f = 0 mew_s = 0 lst = [] for i in range(19): lst.append([]) for j in range(19): lst[i].append(0) break for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()