根据之前的博文,我把给同学做的三子棋小游戏的代码发出来,只是界面很丑很丑,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 ...
随机推荐
- SAR命令
前面已经介绍了 vmstat和top命令的解析及使用,下面我们来学习一个更重要的命令sarsar命令可以通过参数单独查看系统某个局部的使用情况 sar 命令行的常用格式: sar [options] ...
- RSpec自定义matcher
链接 https://relishapp.com/rspec/rspec-expectations/v/3-4/docs/custom-matchers/define-a-custom-matcher ...
- .NET 中文转缩写拼音
public class CNToSpell { /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 要转换的汉字字符串 ...
- crm 2011 plugin setparent setbusiness 用户更改经理 更改办事处
背景: 在更改经理或者更改办事处时,使用plugin处理相应的团队. 问题:plugin写完,注册时发现使用update注册没有效果,然后bing得到,这里要使用setbusiness 和 setpa ...
- CodeForces 604C 【思维水题】`
题意: 给你01字符串的长度再给你一个串. 然后你可以在这个串中选择一个起点和一个终点使得这个连续区间内所有的位取反. 求: 经过处理后最多会得到多少次01变换. 例如:0101是4次,0001是2次 ...
- Integer cache
View.findViewById采用深度遍历,找到第一个匹配的控件 Integer Cache public static void testIntegerCache() { Class cache ...
- HDU 4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Grunt 之通配符
在描述源码路径的时候,经常有一些特殊的奇怪的要求.Grunt 通过内建的 node-glob 和 minimatch 库提供了文件名的扩展机制. 常见的通配符如下: * 匹配除了 / 之外的任意数量的 ...
- vc读写注册表
注册表是Windows重要组成部分,注册表记录了大量有关电脑软硬件的信息.注册表中的值通过其名称标识.值名称由与键名相同的字符组成.值本身可以是字符串.二进制数据或者是32位无符号值.在这里我们主要运 ...
- 酷我音乐API
今天把酷我音乐API分享给大家: 歌曲搜索API:http://search.kuwo.cn/r.s?all={0}&ft=music& itemset=web_2013&cl ...