• 大小: 15KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: Python
  • 标签: python  

资源简介

Pygame重力四子棋设计,可以切换先后手与电脑下棋,AI算法利用蒙特卡罗实现,游戏界面完善

资源截图

代码片段和文件信息

import pygame sys
from pygame.locals import *
import time
import random
import copy

# game parameters
pygame.init()
win_width win_height = 930 700
displaysurf = pygame.display.set_mode((win_width win_height) 0 32)
pygame.display.set_caption(‘Connect_4‘)

# color parameters
backgroundcolor_chess = (244 171 102)
color_white = (255 255 255)
color_black = (0 0 0)
color_tip_white = (225 225 225)
color_tip_black = (25 25 25)
color_green = (0 255 0)

# chess parameters
chess_grid_row chess_grid_col = 6 8
chess_list = []
for i in range(chess_grid_row):
    new_line = [0 for j in range(chess_grid_col)]
    chess_list.append(new_line)
player = True
play_flag = False

# draw chessboard
def draw_chessboard():
    displaysurf.fill(color_white)
    fontobj = pygame.font.SysFont(‘SimHei‘70)
    text = fontobj.render(“重力四子棋“ True color_black color_white)
    textrect = text.get_rect()
    textrect.center = (430 70)
    displaysurf.blit(text textrect)
    pygame.draw.rect(displaysurf backgroundcolor_chess (50 170 640 480))
    for pix_row in range(7):
        pygame.draw.line(displaysurf color_black (50 170 + pix_row * 80) (690 170 + pix_row * 80))
    for pix_col in range(9):
        pygame.draw.line(displaysurf color_black (50 + pix_col * 80 170) (50 + pix_col * 80 650))

def draw_tip_chess(mousex mousey type):
    for row in range(chess_grid_row):
        for col in range(chess_grid_col):
            if chess_list[row][col] in [3 4]:
                chess_list[row][col] = 0
    col = int((mousex - 50) / 80)
    row = int((mousey - 170) / 80)
    if row == chess_grid_row:
        row -= 1
    if col == chess_grid_col:
        col -= 1
    if row < chess_grid_row - 1:
        if chess_list[row + 1][col] == 0:
            return
    if chess_list[row][col] == 0:
        chess_list[row][col] = type

def clear_tip_chess():
    for row in range(chess_grid_row):
        for col in range(chess_grid_col):
            if chess_list[row][col] in [3 4]:
                chess_list[row][col] = 0

def draw_check_chess(mousex mousey type):
    for row in range(chess_grid_row):
        for col in range(chess_grid_col):
            if chess_list[row][col] in [3 4]:
                chess_list[row][col] = 0
    col = int((mousex - 50) / 80)
    row = int((mousey - 170) / 80)
    if row == chess_grid_row:
        row -= 1
    if col == chess_grid_col:
        col -= 1
    if row < chess_grid_row - 1:
        if chess_list[row + 1][col] == 0:
            return
    if chess_list[row][col] in [1 2]:
        return False
    else:
        chess_list[row][col] = type
        return True

def draw_chess():
    for row in range(chess_grid_row):
        for col in range(chess_grid_col):
            if chess_list[row][col] == 0:
                pygame.draw.circle(displaysurf backgroundcolor_chess (90 + col * 80 210 + row * 80) 38)
            eli

评论

共有 条评论