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()
04-18 14:17