import time as t
import pygame as py
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的浮点数
Game_start_screen =py.display.set_mode((500, 500))# 创建Pygame显示窗口
py.display.set_caption('井字棋')
Game_start_screen.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)
Game_start_screen_rect = Game_start_screen.get_rect()
l = py.font.SysFont('fangsong',48)
l_image = l.render('按下空格开始游戏',True,(241,0,0),(255, 255, 255))
l_rect = l_image.get_rect()
l_rect.center = Game_start_screen_rect.center
Game_start_screen.blit(l_image,l_rect)
py.display.flip() # 顺带着刷新一下
def win(winer):
t.sleep(0.5)
if winer == 'player':
window =py.display.set_mode((500, 500))# 创建Pygame显示窗口
py.display.set_caption('井字棋')
window.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)
window_rect = window.get_rect()
letter = py.font.SysFont('fangsong',48)
letter_image = letter.render('you are win',True,(241,0,0),(255, 255, 255))
letter_rect = letter_image.get_rect()
letter_rect.center = window_rect.center
window.blit(letter_image,letter_rect)
py.display.flip() # 顺带着刷新一下
while 1:
for event in py.event.get():
if event.type == py.QUIT:
s.exit()
if winer == 'AI':
window =py.display.set_mode((500, 500))# 创建Pygame显示窗口
py.display.set_caption('井字棋')
window.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)
window_rect = window.get_rect()
letter = py.font.SysFont('fangsong',48)
letter_image = letter.render('AI is win',True,(165,162,162),(255, 255, 255))
letter_rect = letter_image.get_rect()
letter_rect.center = window_rect.center
window.blit(letter_image,letter_rect)
py.display.flip() # 顺带着刷新一下
while 1:
for event in py.event.get():
if event.type == py.QUIT:
s.exit()
else:
window =py.display.set_mode((500, 500))# 创建Pygame显示窗口
py.display.set_caption('井字棋')
window.fill((255, 255, 255)) # 窗口背景颜色,注意是RGB格式(此处为白)
window_rect = window.get_rect()
letter = py.font.SysFont('fangsong',48)
letter_image = letter.render('everyone is not win',True,(110,180,140),(255, 255, 255))
letter_rect = letter_image.get_rect()
letter_rect.center = window_rect.center
window.blit(letter_image,letter_rect)
py.display.flip() # 顺带着刷新一下
while 1:
for event in py.event.get():
if event.type == py.QUIT:
s.exit()
def player_win(player,dian):
if player['1'] == '1' and player['2'] == '1' and player['3'] == '1':
win('player')
elif player['1'] == '1' and player['5'] == '1' and player['9'] == '1':
win('player')
elif player['2'] == '1' and player['5'] == '1' and player['8'] == '1':
win('player')
elif player['3'] == '1' and player['6'] == '1' and player['9'] == '1':
win('player')
elif player['3'] == '1' and player['5'] == '1' and player['7'] == '1':
win('player')
elif player['4'] == '1' and player['5'] == '1' and player['6'] == '1':
win('player')
elif player['7'] == '1' and player['8'] == '1' and player['9'] == '1':
win('player')
elif dian == 9:
win('not win')
def AI_win(AI,dian):
if AI['1'] == '1' and AI['2'] == '1' and AI['3'] == '1':
win('AI')
elif AI['1'] == '1' and AI['4'] == '1' and AI['7'] == '1':
win('AI')
elif AI['1'] == '1' and AI['5'] == '1' and AI['9'] == '1':
win('AI')
elif AI['2'] == '1' and AI['5'] == '1' and AI['8'] == '1':
win('AI')
elif AI['3'] == '1' and AI['6'] == '1' and AI['9'] == '1':
win('AI')
elif AI['3'] == '1' and AI['5'] == '1' and AI['7'] == '1':
win('AI')
elif AI['4'] == '1' and AI['5'] == '1' and AI['6'] == '1':
win('AI')
elif AI['7'] == '1' and AI['8'] == '1' and AI['9'] == '1':
win('AI')
elif dian == 9:
win('not win')
image1 = py.image.load('1.png') # 加载图片
image2 = py.image.load('2.png') # 加载图片
imageX = py.image.load('X.png') # 加载图片
imageO = py.image.load('O.png') # 加载图片
while 1:
for event in py.event.get():
if event.type == py.QUIT:
s.exit()
elif event.type == py.KEYDOWN:
if event.key == py.K_SPACE:
py.mixer.music.play() # 播放音频
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(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()
dian += 1
list_j.remove(a)
print(list_j)
AI[a] = '1'
print(AI)
AI_win(AI,dian)