import time as t
import pygame as py
import easygui as g
import random as d
import sys as s
py.init()
py.mixer.init() # 初始化混音器模块(pygame库的通用做法,每一个模块在使用jj时都要初始化pygame.init()为初始化所有的pygame模块,可以使用它也可以单初始化这一个模块)
py.mixer.music.load("C:/Users/dell/Desktop/6757.mp3") # 加载音乐 jj
py.mixer.music.set_volume(0.3)# 设置音量大小0~1的浮点数
b=g.buttonbox(title='Whether to start the game',choices=['开始','退出'],image='C:/Users/dell/Desktop/python/勇者斗恶龙/123.png')
if b != None:
py.mixer.music.play() # 播放音频
t.sleep(1)
image1 = py.image.load('1.png') # 加载图片
image2 = py.image.load('2.png') # 加载图片
imageX = py.image.load('X.png') # 加载图片
imageO = py.image.load('O.png') # 加载图片
def player_win(player,dian):
if player['1'] == '1' and player['2'] == '1' and player['3'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['1'] == '1' and player['4'] == '1' and player['7'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['1'] == '1' and player['5'] == '1' and player['9'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['2'] == '1' and player['5'] == '1' and player['8'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['3'] == '1' and player['6'] == '1' and player['9'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['3'] == '1' and player['5'] == '1' and player['7'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['4'] == '1' and player['5'] == '1' and player['6'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif player['7'] == '1' and player['8'] == '1' and player['9'] == '1':
g.msgbox(title = 'win', msg = 'you are win')
s.exit()
elif dian == 9:
g.msgbox(title = 'not win', msg = '平局')
s.exit()
def AI_win(AI,dian):
if AI['1'] == '1' and AI['2'] == '1' and AI['3'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['1'] == '1' and AI['4'] == '1' and AI['7'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['1'] == '1' and AI['5'] == '1' and AI['9'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['2'] == '1' and AI['5'] == '1' and AI['8'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['3'] == '1' and AI['6'] == '1' and AI['9'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['3'] == '1' and AI['5'] == '1' and AI['7'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['4'] == '1' and AI['5'] == '1' and AI['6'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif AI['7'] == '1' and AI['8'] == '1' and AI['9'] == '1':
g.msgbox(title = 'win', msg = 'AI is win')
s.exit()
elif dian == 9:
g.msgbox(title = 'not win', msg = '平局')
s.exit()
if b == '开始':
zuo_shang_X = {'1': '10', '2': '210', '3': '410', '4': '10', '5': '210', '6': '410', '7': '10', '8': '210', '9': '410'}
zuo_shang_Y = {'1': '10', '2': '10', '3': '10', '4': '210', '5': '210', '6': '210', '7': '410', '8': '410', '9': '410'}
X = dict(zuo_shang_X)
Y = dict(zuo_shang_Y)
list_j = ['1','2','3','4','5','6','7','8','9']
dict_player = {'1': '0', '2': '0', '3': '0', '4': '0', '5': '0', '6': '0', '7': '0', '8': '0', '9': '0'}
dict_AI = {'1': '0', '2': '0', '3': '0', '4': '0', '5': '0', '6': '0', '7': '0', '8': '0', '9': '0'}
player = dict(dict_player)
AI = dict(dict_AI)
screen =py.display.set_mode((610, 610))# 创建Pygame显示窗口
py.display.set_caption('井字棋')
screen.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)
for i in range(0,800,200):
screen.blit(image1, (0, i)) # 进行渲染
for j in range(0,800,200):
screen.blit(image2, (j, 0)) # 进行渲染
py.display.flip() # 顺带着刷新一下
dian = 0
while 1:
for event in py.event.get():
if event.type == py.QUIT:
s.exit()
if event.type == py.MOUSEBUTTONDOWN:
x,y = py.mouse.get_pos()
dian += 1
py.mixer.music.play() # 播放音频
for n in list_j:
if x - int(X[n]) <= 200 and y - int(Y[n]) <= 200 and x - int(X[n]) >= 0 and y - int(Y[n]) >= 0:
screen.blit(imageX, (int(X[n]), int(Y[n]))) # 进行渲染
list_j.remove(n)
print(list_j)
player[n] = '1'
print(dict_player)
py.display.update()
player_win(player,dian)
t.sleep(1)
a = d.choice(list_j)
screen.blit(imageO, (int(X[a]), int(Y[a]))) # 进行渲染
py.display.update()
list_j.remove(a)
print(list_j)
AI[a] = '1'
print(dict_AI)
AI_win(AI,dian)
print(f'x={x},y={y}')
else:
s.exit()