python五子棋游戏
时间: 2023-08-28 17:08:10 浏览: 127
python游戏-五子棋
抱歉,我是语言模型AI,无法为您提供可运行的游戏。但是,我可以给您提供一个五子棋游戏的python代码示例:
```python
import pygame
import sys
import numpy as np
ROW = 15
COL = 15
SIZE = 40
WIDTH = COL * SIZE
HEIGHT = ROW * SIZE
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
def draw_board(screen):
for i in range(ROW):
pygame.draw.line(screen, BLACK, (SIZE//2, SIZE//2+i*SIZE), (WIDTH-SIZE//2, SIZE//2+i*SIZE), 2)
for i in range(COL):
pygame.draw.line(screen, BLACK, (SIZE//2+i*SIZE, SIZE//2), (SIZE//2+i*SIZE, HEIGHT-SIZE//2), 2)
pygame.draw.circle(screen, BLACK, (WIDTH//2, HEIGHT//2), 5, 0)
pygame.display.update()
def draw_piece(screen, x, y, color):
pygame.draw.circle(screen, color, (SIZE//2+x*SIZE, SIZE//2+y*SIZE), SIZE//2-2, 0)
pygame.display.update()
def check_win(board, x, y, color):
count = 0
for i in range(ROW):
if board[i][y] == color:
count += 1
else:
count = 0
if count >= 5:
return True
count = 0
for j in range(COL):
if board[x][j] == color:
count += 1
else:
count = 0
if count >= 5:
return True
count = 0
for i in range(-min(x,y),min(ROW-x,COL-y)):
if board[x+i][y+i] == color:
count += 1
else:
count = 0
if count >= 5:
return True
count = 0
for i in range(-min(x,COL-y-1),min(ROW-x-1,y+1)):
if board[x+i][y-i] == color:
count += 1
else:
count = 0
if count >= 5:
return True
return False
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Five in a Row")
screen.fill(WHITE)
draw_board(screen)
board = np.zeros((ROW, COL))
color = BLACK
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if x<SIZE//2 or x>WIDTH-SIZE//2 or y<SIZE//2 or y>HEIGHT-SIZE//2:
continue
i, j = (y-SIZE//2)//SIZE, (x-SIZE//2)//SIZE
if board[i][j] == 0:
board[i][j] = color
draw_piece(screen, j, i, color)
if check_win(board, i, j, color):
font = pygame.font.Font(None, 60)
text = font.render("Winner: "+("Black" if color==BLACK else "White"), True, RED)
screen.blit(text, ((WIDTH-text.get_width())//2, (HEIGHT-text.get_height())//2))
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
sys.exit()
color = WHITE if color == BLACK else BLACK
if __name__ == '__main__':
main()
```
这是一个简单的五子棋游戏,使用了pygame库。在游戏中,黑色先手,白色后手,通过鼠标点击来下棋。当有一方获得五子连珠时,游戏结束,显示胜利者。
阅读全文