根据之前的博文,我把给同学做的三子棋小游戏的代码发出来,只是界面很丑很丑,AI算法很笨很笨,过几天我传到网盘上,提供大家下载娱乐
background_image_filename = 'blackground.png'
black_mouse_image_filename = 'black.png'
white_mouse_image_filename = 'white.png'
restart_icon_filename='restart.png'
pingju_icon_filename='pingju.jpg'
win_icon_filename='win.jpg'
lose_icon_filename='lose.jpg' import pygame
import time
import sys from pygame.locals import *
from sys import exit HIGHLIGHTCOLOR = (255,255,255) def AI(pp):
#status is now white and black key in the board
#taozi = white
#computer = black num = 0
x = 0
y = 0 if pp[1][1]=='none':
return 4 for i in range(0,3):
for j in range(0,3):
if pp[i][j]=='none':
pp[i][j]='white'
cnt=0 tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
ty=ty+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
ty=ty-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
ty=ty+1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1
ty=ty-1 if cnt > num:
num = cnt
x=i
y=j cnt=0
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx+1
ty=ty-1
tx=i
ty=j
while tx>=0 and tx<3 and ty>=0 and ty<3 and pp[tx][ty]=='white':
cnt=cnt+1
tx=tx-1
ty=ty+1 if cnt > num:
num = cnt
x=i
y=j pp[i][j]='none' return x*3+y def main(): pygame.init()
screen = pygame.display.set_mode((800,500),0,32)
pygame.display.set_caption("you can never win!")
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(white_mouse_image_filename).convert_alpha() white_sign = pygame.image.load(white_mouse_image_filename).convert_alpha()
restart = pygame.image.load(restart_icon_filename).convert_alpha()
pingju = pygame.image.load(pingju_icon_filename).convert_alpha()
winicon = pygame.image.load(win_icon_filename).convert_alpha()
lose = pygame.image.load(lose_icon_filename).convert_alpha() black_sign = pygame.image.load(black_mouse_image_filename).convert_alpha()
position=[ [27,11,93,68,0],[240,6,304,60,1],[476,4,525,42,2],[24,206,80,265,3],[246,212,292,257,4],[477,212,528,258,5],[26,440,73,479,6],[244,435,292,478,7],[475,435,527,480,8] ]
positionMid=[ [48-26,24-24],[269-26,24-24],[500-26,24-24],[48-26,233-24],[269-26,234-24],[500-26,234-24],[48-26,459-24],[269-26,460-24],[500-26,460-24] ]
positionColor=[ 'none','none','none','none','none','none','none','none','none'] whoIsTurn = ['computer','taozi']
turnKey = 1 # 1 means taozi
# 0 means computer
cnt = 0
pp=[ ['none','none','none'],
['none','none','none'],['none','none','none']]
#pp = [ [
win=0
while 1:
newx =0
newy =0 color = 'white'
color1 = 'black'
color2 = 'none'
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN :
newx,newy = pygame.mouse.get_pos()
for i in position:
if (newx>=i[0] and newx <= i[2]) and (newy >= i[1] and newy <= i[3]):
j = i[4]
if positionColor[j]!='none':
continue
positionColor[j]= color
#positionColor[j+1]= color
for ii in range(0,3):
for jj in range(0,3):
pp[ii][jj]=positionColor[ii*3+jj] # AI begin res = AI( pp )
positionColor[res]=color1 # AI finished cnt=cnt+1
if newx>=674 and newx<=774 and newy>=396 and newy<= 496:
for i in range(0,3):
for j in range(0,3):
positionColor[i*3+j]='none'
pp[i][j]='none'
win=0 screen.blit(background,(0,0))
screen.blit(restart,(674,396))
#x,y = pygame.mouse.get_pos()
#x -= mouse_cursor.get_width()/2
#y -= mouse_cursor.get_height()/2
#if win==0:
# screen.blit(mouse_cursor,(x,y)) #x,y = pygame.mouse.get_pos()
for i in position:
x,y = pygame.mouse.get_pos()
if (x>=i[0] and x <= i[2]) and (y >= i[1] and y <= i[3]):
x-=26
y-=26
if positionColor[ int(i[4]) ]=='none':
#pygame.draw.rect(screen,HIGHLIGHTCOLOR,(x,y,52,52),0)
screen.blit(mouse_cursor,(x,y))
print("done it")
else:
print("color= ",positionColor[i[4]]) for ii in [0,1,2,3,4,5,6,7,8]:
if positionColor[ii] != 'none':
if positionColor[ii] == 'white':
#print("write white_sign")
screen.blit(white_sign,positionMid[ii] )
else :
#print("write black_sign")
screen.blit(black_sign,positionMid[ii]) for i in range(0,3):
for j in range(0,3):
pp[i][j]=positionColor[i*3+j] for i in range(0,3):
if pp[i][0]==pp[i][1] and pp[i][0]==pp[i][2] and pp[i][0]!='none':
win=1
winner=pp[i][0]
if pp[0][i]==pp[1][i] and pp[0][i]==pp[2][i] and pp[0][i]!='none':
win=1
winner=pp[0][i]
if pp[0][0]==pp[1][1] and pp[0][0]==pp[2][2] and pp[0][0]!='none':
win=1
winner=pp[0][0]
if pp[2][0]==pp[1][1] and pp[1][1] == pp[0][2] and pp[1][1]!='none':
win =1
winner=pp[1][1]
if win==0:
cntcolor=0
for i in range(0,3):
for j in range(0,3):
if pp[i][j]!='none':
cntcolor=cntcolor+1
if cntcolor==9 :
win =1
winner='pingju' if win==1 :
#print("someone win!")
if winner=='white':
screen.blit(winicon,(50,20))
elif winner=='pingju':
screen.blit(pingju,(100,100))
else:
screen.blit(lose,(100,100))
#print("winner = ",winner)
#time.sleep(10000) pygame.display.update() if __name__ == '__main__':
main()
根据之前的博文,我把给同学做的三子棋小游戏的代码发出来,只是界面很丑很丑,AI算法很笨很笨,过几天我传到网盘上,提供大家下载娱乐的更多相关文章
- 使用pcs api往免费的百度网盘上传下载文件
百度个人云盘空间大,完全免费,而且提供了pcs api供调用操作文件,在平时的项目里往里面保存一些文件是很实用的. 环境准备: 开通读写网盘的权限及获取access_token:http://blog ...
- 【推荐网站】下载国外网盘+强大的离线下载站—offcloud.com
博主在网上浏览时看到一篇帖子,推荐了一个离线下载网站–offcloud.com,支持上传种子文件.磁力链和几十家网盘的直连下载,厉害了我的哥,这是个啥网站这么666.即使之前咱们写了几篇文章来自建下载 ...
- 一个国外网盘pCloud——支持离线下载
给大家分享一个国外网盘<支持离线下载> https://my.pcloud.com/#page=register&invite=HiegZ8aBrt7
- For macOS.百度网盘 破解SVIP、下载速度限制~
For macOS.百度网盘 破解SVIP.下载速度限制~ 是插件的 https://github.com/CodeTips/BaiduNetdiskPlugin-macOS 2019-01-03 让 ...
- MultCloud – 支持数据互传的网盘管理
MultCloud https://www.multcloud.com/ 是一款在线服务,可以在一个地方管理众多网盘,支持国产百度盘, 最具有特色的地方是你可以直接在 MultCloud 里操作将 D ...
- 百度网盘免VIP全速下载!
不知道大家在用百度网盘下载文件时会不会遇到这样一个问题: 过分! 太过分了! 100M的宽带你就给我限速到20KB/s... 当然 解决办法有很多 1.充钱(这辈子都不可能的) ······ 百度上有 ...
- Visual Studio 2017 发布 15.5 版本,百度网盘离线安装包下载。
Visual Studio 2017 15.5 版本已正式发布,同时发布的还有 Visual Studio for Mac 7.3 .此次更新包含主要性能改进,新特性以及 bug 修复.发行说明中文版 ...
- 百度网盘无vip高速下载的方法
我拿的是谷歌浏览器做实验 首先下载一个可以改user-agent的插件 我chorm里面下载的是User-Agent Switcher for Chrome插件 将百度网盘的url地址中的baidu. ...
- 百度网盘上下载文件,调用api接口的请求方式和参数
REST api 功能:下载单个文件. Download接口支持HTTP协议标准range定义,通过指定range的取值可以实现断点下载功能. 例如: 如果在request消息中指定“Range: b ...
随机推荐
- [实变函数]4.2 Egrov 定理
1 一致收敛很重要, 但可惜的是很多时候不一致收敛. 比如 $$\bex f_n(x)=x^n\to f(x)=\sedd{\ba{ll} 0,&x\in [0,1)\\ 1,&x=1 ...
- vim使用快捷键
vim使用快捷键 索引 1. 关于Vim 1.1 Vim的几种模式 2. 启动Vim 3. 文档操作 4. 光标的移动 4.1 基本移动 4.2 翻屏 4.3 标记 5. 插入文本 5.1 基本插入 ...
- $.ajax()中dataType
$.ajax()中dataType的默认值为TEXT,如果没有指明json,而返回来的数据又就是json格式,则无法显示.!!
- 通过 HTTP 头进行 SQL 注入
在漏洞评估和渗透测试中,确定目标应用程序的输入向量是第一步.有时,当做web应用程序测试时,SQL注入漏洞的测试用例通常局限于特殊的输入向量GET和POST变量.那么对于其他的HTTP头部参数呢?难道 ...
- Think in UML笔记第1章--为什么要UML
1.1 面向过程还是面向对象 面向过程和面向对象都是一种软件技术.例如把面向过程归纳为结构化程序设计.DFD图.ER模型.UC矩阵等,而面向对象则被归纳为继承.封装.多态.复用等具体的技术.事实上,上 ...
- Ubuntu配置网络命令(转载)
From:http://blog.csdn.net/ithomer/article/details/6264881 以eth0为例 1. 以DHCP方式配置网卡 编辑文件: /etc/networ ...
- 学习MongoDB--(11):应用举例(利用java操作MongoDB)
原文地址:http://blog.csdn.net/drifterj/article/details/7948090 目录地址:http://blog.csdn.net/DrifterJ/articl ...
- (C) Windows 32 API程序设计目录
(一)第一个窗口程序 01 创建第一个窗口. (二)输出文本 01 获取Windows图形构件大小信息
- Python补充05 字符串格式化 (%操作符)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输 ...
- Python进阶07 函数对象
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...