当前位置: 首页 > news >正文

苏州外贸网站制作星凯网站建设

苏州外贸网站制作,星凯网站建设,协会类网站免费模板,影视广告五子棋是一种源自中国的传统棋类游戏,起源可以追溯到古代。它是一种两人对弈的游戏,使用棋盘和棋子进行。棋盘通常是一个 1515 的网格,棋子分为黑白两色,双方轮流在棋盘上落子。游戏的目标是通过在棋盘上落子,使自己的…

五子棋是一种源自中国的传统棋类游戏,起源可以追溯到古代。它是一种两人对弈的游戏,使用棋盘和棋子进行。棋盘通常是一个 15×15 的网格,棋子分为黑白两色,双方轮流在棋盘上落子。游戏的目标是通过在棋盘上落子,使自己的五个棋子在横向、纵向或斜向形成连续的线路,从而获胜。

五子棋被认为是一种智力游戏,它要求玩家在竞技中思考对手的走法并制定自己的策略。由于规则简单、易于上手,五子棋在中国以及世界各地都很受欢迎,并且有许多不同的变种和玩法。笔者选择了一个最原始最简易的一个简易五子棋的程序,采用文本界面操作。

源码获取:

在这里插入图片描述


主要源代码展示
`

# 检查水平方向
count = 0
for i in range(max(0, col - 4), min(15, col + 5)):if board[row][i] == player:count += 1if count == 5:return Trueelse:count = 0# 检查垂直方向
count = 0
for i in range(max(0, row - 4), min(15, row + 5)):if board[i][col] == player:count += 1if count == 5:return Trueelse:count = 0# 检查斜向(\)方向
count = 0
for i in range(-4, 5):r = row + ic = col + iif r < 0 or r >= 15 or c < 0 or c >= 15:continueif board[r][c] == player:count += 1if count == 5:return Trueelse:count = 0# 检查斜向(/)方向
count = 0
for i in range(-4, 5):r = row + ic = col - iif r < 0 or r >= 15 or c < 0 or c >= 15:continueif board[r][c] == player:count += 1if count == 5:return Trueelse:count = 0return Falseboard = [["." for _ in range(15)] for _ in range(15)]
players = ["X", "O"]
current_player = 0
print_board(board)while True:print(f"Player {players[current_player]}'s turn:")try:row = int(input("Enter row (0-14): "))col = int(input("Enter col (0-14): "))if row < 0 or row >= 15 or col < 0 or col >= 15 or board[row][col] != ".":raise ValueErrorexcept ValueError:print("Invalid input. Try again.")continueboard[row][col] = players[current_player]print_board(board)if check_win(board, row, col, players[current_player]):print(f"Player {players[current_player]} wins!")breakcurrent_player = (current_player + 1) % 2

通过改进,新增了可使用鼠标交互的效果,相比较于原始的代码,大大提高了游戏体验性,并且使游戏界面更加易于操作,在游戏体验上大大增加也玩性。
原始代码
`import pygame
import sys

游戏设置

CELL_SIZE = 40
BOARD_SIZE = 15
WINDOW_WIDTH = CELL_SIZE * BOARD_SIZE
WINDOW_HEIGHT = CELL_SIZE * BOARD_SIZE
LINE_COLOR = (0, 0, 0)
BG_COLOR = (139, 69, 19) # 棕色背景

初始化游戏

pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption(“五子棋”)
clock = pygame.time.Clock()
board = [[’ ’ for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
current_player = ‘X’
game_over = False

def draw_board():
screen.fill(BG_COLOR)
for i in range(BOARD_SIZE):
pygame.draw.line(screen, LINE_COLOR, (CELL_SIZE // 2, CELL_SIZE // 2 + i * CELL_SIZE),
(WINDOW_WIDTH - CELL_SIZE // 2, CELL_SIZE // 2 + i * CELL_SIZE))
pygame.draw.line(screen, LINE_COLOR, (CELL_SIZE // 2 + i * CELL_SIZE, CELL_SIZE // 2),
(CELL_SIZE // 2 + i * CELL_SIZE, WINDOW_HEIGHT - CELL_SIZE // 2))

def draw_piece(row, col, player):
x = col * CELL_SIZE + CELL_SIZE // 2
y = row * CELL_SIZE + CELL_SIZE // 2
if player == ‘X’:
pygame.draw.circle(screen, (0, 0, 0), (x, y), CELL_SIZE // 3)
else:
pygame.draw.circle(screen, (255, 255, 255), (x, y), CELL_SIZE // 3)

def check_win(row, col):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for d in directions:
count = 1
for i in range(1, 5):
if 0 <= row + i * d[0] < BOARD_SIZE and 0 <= col + i * d[1] < BOARD_SIZE and
board[row + i * d[0]][col + i * d[1]] == current_player:
count += 1
else:
break
for i in range(1, 5):
if 0 <= row - i * d[0] < BOARD_SIZE and 0 <= col - i * d[1] < BOARD_SIZE and
board[row - i * d[0]][col - i * d[1]] == current_player:
count += 1
else:
break
if count >= 5:
return True
return False

def display_winner(player):
font = pygame.font.Font(None, 36)
if player == ‘X’:
text = font.render(“Black wins!”, True, (255, 0, 0))
else:
text = font.render(“White wins!”, True, (255, 0, 0))
text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
screen.blit(text, text_rect)

游戏循环

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
x, y = event.pos
col = x // CELL_SIZE
row = y // CELL_SIZE
if 0 <= row < BOARD_SIZE and 0 <= col < BOARD_SIZE and board[row][col] == ’ ':
board[row][col] = current_player
draw_piece(row, col, current_player)
if check_win(row, col):

         print(f"Player {current_player} wins!")game_over = Truecurrent_player = 'O' if current_player == 'X' else 'X'draw_board()
for row in range(BOARD_SIZE):for col in range(BOARD_SIZE):if board[row][col] != ' ':draw_piece(row, col, board[row][col])if game_over:display_winner(current_player)pygame.display.flip()
clock.tick(30)

`import pygame
import sys

游戏设置

CELL_SIZE = 40
BOARD_SIZE = 15
WINDOW_WIDTH = CELL_SIZE * BOARD_SIZE
WINDOW_HEIGHT = CELL_SIZE * BOARD_SIZE
LINE_COLOR = (0, 0, 0)
BG_COLOR = (139, 69, 19) # 棕色背景

初始化游戏

pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption(“五子棋”)
clock = pygame.time.Clock()
board = [[’ ’ for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
current_player = ‘X’
game_over = False

def draw_board():
screen.fill(BG_COLOR)
for i in range(BOARD_SIZE):
pygame.draw.line(screen, LINE_COLOR, (CELL_SIZE // 2, CELL_SIZE // 2 + i * CELL_SIZE),
(WINDOW_WIDTH - CELL_SIZE // 2, CELL_SIZE // 2 + i * CELL_SIZE))
pygame.draw.line(screen, LINE_COLOR, (CELL_SIZE // 2 + i * CELL_SIZE, CELL_SIZE // 2),
(CELL_SIZE // 2 + i * CELL_SIZE, WINDOW_HEIGHT - CELL_SIZE // 2))

def draw_piece(row, col, player):
x = col * CELL_SIZE + CELL_SIZE // 2
y = row * CELL_SIZE + CELL_SIZE // 2
if player == ‘X’:
pygame.draw.circle(screen, (0, 0, 0), (x, y), CELL_SIZE // 3)
else:
pygame.draw.circle(screen, (255, 255, 255), (x, y), CELL_SIZE // 3)

def check_win(row, col):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for d in directions:
count = 1
for i in range(1, 5):
if 0 <= row + i * d[0] < BOARD_SIZE and 0 <= col + i * d[1] < BOARD_SIZE and
board[row + i * d[0]][col + i * d[1]] == current_player:
count += 1
else:
break
for i in range(1, 5):
if 0 <= row - i * d[0] < BOARD_SIZE and 0 <= col - i * d[1] < BOARD_SIZE and
board[row - i * d[0]][col - i * d[1]] == current_player:
count += 1
else:
break
if count >= 5:
return True
return False

def display_winner(player):
font = pygame.font.Font(None, 36)
if player == ‘X’:
text = font.render(“Black wins!”, True, (255, 0, 0))
else:
text = font.render(“White wins!”, True, (255, 0, 0))
text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
screen.blit(text, text_rect)

游戏循环

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
x, y = event.pos
col = x // CELL_SIZE
row = y // CELL_SIZE
if 0 <= row < BOARD_SIZE and 0 <= col < BOARD_SIZE and board[row][col] == ’ ':
board[row][col] = current_player
draw_piece(row, col, current_player)
if check_win(row, col):
print(f"Player {current_player} wins!")
game_over = True
current_player = ‘O’ if current_player == ‘X’ else ‘X’

draw_board()
for row in range(BOARD_SIZE):for col in range(BOARD_SIZE):if board[row][col] != ' ':draw_piece(row, col, board[row][col])if game_over:display_winner(current_player)pygame.display.flip()
clock.tick(30)



结语:
它通过简洁明了的语法和逻辑结构,提高了代码的可读性和可维护性。通过引入交互性更强的用户输入方式,使得玩家可以更直观地参与游戏,增强了游戏的可玩性和用户体验。总的来说,这段 Python 在保持游戏逻辑不变的情况下,提升了代码的质量和可玩性,为开发者和玩家带来了更好的体验。

如有侵权,请联系删除。

http://www.yayakq.cn/news/648808/

相关文章:

  • 深圳宝安区网站建设公司易班网站建设的意义
  • 住房和城乡建设部网站办事大厅里边有心学做网站
  • 大米网站模板ftp工具下载网站源码教程
  • 北京优化网站免费企业查询软件
  • 免费做简历网站有哪些网页美工怎么做
  • 郑州做网站擎天广告公司网站官网
  • 徐州网站开发设计平台公司名称起名大全
  • 做网站的广告词国外的设计网站app吗
  • 小城镇建设 网站官方郑州网站建设优化企业
  • 宁波住房和建设局网站天津开发网站公司
  • 网站结构 seo做兼职的网站有哪些工作内容
  • 刚创业 建网站品牌网页
  • 商业网站开发 说课wordpress 手机版域名
  • 珠海汽车网站建设银川注册公司流程和费用
  • 制作网站用c 做前台企业查询卡在哪里打印
  • c2c网站的功能枣庄网络推广
  • 推广平台网站天柱县住房和城乡建设部网站
  • 本地电脑做视频网站 外网连接六安网站推广
  • 2015百度竞价单页面网站模板源码设计江苏建站管理系统开发
  • 为什么要给企业建设网站?株洲荷塘区
  • 个人网站更换域名网站推广的预算
  • 上海网站开发孵化百度云加速 wordpress
  • 建设网站一般要多钱wordpress foxplayer
  • 顺德网站建设包括哪些最简单的网站模板下载
  • 查看网站浏览量合肥搜索引擎推广
  • 云南专业网站建设网站开发非常之旅:ajax从入门到精通 pdf
  • 多语言网站难做么企业商城网站开发建设
  • 泰安网站推广优化济南专业网站制作公司
  • 网站建设的安全性问题网站备案安全责任书是谁盖章
  • 深圳建网站网站开发人员绩效如何计算