五子棋游戏中等版
inside.py
def newBoard():
'''初始化棋盘'''
board = [[0 for i in range(15)] for j in range(15)]
return board
def possiableToChoice(board,i,j):
'''可以下棋子的地方'''
if board[i][j] == 0:
return True
else:
return False
def win(board,player,i,j):
'''胜利条件'''
direction = [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]
for a in direction:
for l in range(5):
sum = 0
for k in range(0-l,5-l):
if i+k*a[0] <= 14 and j+k*a[1] <= 14 and board[i+k*a[0]][j+k*a[1]] == player and i+k*a[0] >= 0 and j+k*a[1] >= 0 :
sum = sum + 1
if sum == 5:
return False
return True
game.py
import pygame
import inside
def displayBoard(background,colors):
'''初始化棋盘'''
font = pygame.font.SysFont("arial",25)
for i in range(1,16):
'''画棋盘及边框文字'''
pygame.draw.line(background,colors[0],(45,15+30*i),(465,15+30*i),1)
pygame.draw.line(background,colors[0],(30*i+15,45),(30*i+15,465),1)
word = font.render(chr(64+i),False,colors[0])
number = font.render(str(i),False,colors[0])
background.blit(word,(9+i*30,470))
background.blit(number,(12,480-i*30))
direction = [(1,1),(1,-1),(-1,1),(-1,-1),(0,0)]
for a in direction:
pygame.draw.circle(background,colors[0],(255+a[0]*120,255+a[1]*120),4)
def possiblePosition(mousePosition,board,background,colors,player):
i = int((mousePosition[0]-30)/30)
j = int((mousePosition[1]-30)/30)
while inside.possiableToChoice(board,i,j):
pygame.draw.circle(background,colors[player%2],(45+30*i,45+30*j),12)
player = player%2+1
board[i][j] = player
return player,i,j
return player,-1,-1
def fiveChess():
colors = [(0,0,0),(255,255,255),(249,234,154)]
pygame.init()
pygame.display.set_caption("Five Chess")
window = pygame.display.set_mode((720,520))
background = pygame.Surface(window.get_size())
background.fill(colors[2])
displayBoard(background,colors)
stop = False
player = 1
board = inside.newBoard()
i = -1
j = -1
while not stop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop = True
elif event.type == pygame.MOUSEMOTION:
mousePosition = event.pos
if event.type == pygame.MOUSEBUTTONDOWN:
mousePosition = event.pos
if 30 <= mousePosition[0] <= 470 and 30 <= mousePosition[1] <= 470:
player,i,j = possiblePosition(mousePosition,board,background,colors,player)
if not inside.win(board,player,i,j):
font = pygame.font.SysFont("arial",25)
words = font.render(str(player)+'WIN',False,colors[0])
background.blit(words,(250,200))
window.blit(background,(0,0))
pygame.display.flip()
fiveChess()