资源简介
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
- 上一篇:基于Python的计算机网络实验设计
- 下一篇:最详细神经网络python描写附注释
相关资源
- 最详细神经网络python描写附注释
- 基于Python的计算机网络实验设计
- 西电数据挖掘作业——k中心聚类pyt
- python实现SVM
- 老男孩python全栈开发学习笔记文字整
- python3 HTMLTestRunner截图&美化&优化
- 爬取网页视频,解析m3u8文件,获取
- dmPython.zip
- python实现的改进的遗传算法解决工件
- Python简易滚动抽奖界面程序
- 超限学习机—逻辑回归Python代码
- python3爬取中国天气网天气并写入csv
- Python2.7 贪吃蛇小游戏源码
- python实现logistics的分叉图
- 对任意关键字爬虫对应图片代码
- 图虫网爬虫python实现
- 网站图片爬取代码
- SIFT算法特征提取的python实现
- 已知两点经纬度坐标,求距离函数
- 最新Python3.6网络爬虫实战案例5章(基
- 一个简单的全覆盖路径规划python
- 徐州地区及周边范围noaa气象数据数据
- python五子棋代码
- 社区发现算法 加权GN算法的Python实现
- 基于用户协同过滤usercf的python代码实
- 21天学通python.txt
- python实现视频直播
- python QQ第三方登陆
- tensorflow2.0实现mnist手写数字识别代码
- Python源码剖析_代码(pythonympx.rar)
评论
共有 条评论