#井字棋
import pygame
import sys
s = 0
l = 0
z = 0
my_list = []
my_list_black = []
my_list_red = []
win = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]
pygame.init()
pygame.display.set_caption('井字棋')
size = 300, 330
screen = pygame.display.set_mode(size)
color = (255, 255, 255)
color1 = (0,0,0)
color2 = (255,0,0)
screen.fill(color)
#设置中文包路径以及字体大小
font_path = "ziti.ttf"
font = pygame.font.Font(font_path, 24)
#渲染中文文本为图像
text_surface = font.render("敲击空格重置游戏", True,(0,0,0))
text_rect = text_surface.get_rect()
text_rect.center = (150, 315)
screen.blit(text_surface, text_rect)
#不设置width默认填充内部
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 300, 300), 5)
pygame.draw.line(screen, (0, 0, 0), (0, 100), (300, 100), 5)
pygame.draw.line(screen, (0, 0, 0), (0, 200), (300, 200), 5)
pygame.draw.line(screen, (0, 0, 0), (100, 0), (100, 300), 5)
pygame.draw.line(screen, (0, 0, 0), (200, 0), (200, 300), 5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
screen.fill((255,255,255))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 300, 300), 5)
pygame.draw.line(screen, (0, 0, 0), (0, 100), (300, 100), 5)
pygame.draw.line(screen, (0, 0, 0), (0, 200), (300, 200), 5)
pygame.draw.line(screen, (0, 0, 0), (100, 0), (100, 300), 5)
pygame.draw.line(screen, (0, 0, 0), (200, 0), (200, 300), 5)
screen.blit(text_surface, text_rect)
pygame.display.flip()
my_list.clear()
my_list_black.clear()
my_list_red.clear()
z = 0
print("游戏重新开始")
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
if x < 100 and y < 100:
l = 1
s = 1
elif 100 <= x < 200 and y < 100:
l = 2
s = 1
elif 200 <= x < 300 and y < 100:
l = 3
s = 1
elif x < 100 and 100 <= y < 200:
l = 4
s = 1
elif 100 <= x < 200 and 100 <= y < 200:
l = 5
s = 1
elif 200 <= x < 300 and 100 <= y < 200:
l = 6
s = 1
elif x < 100 and 200 <= y < 300:
l = 7
s = 1
elif 100 <= x < 200 and 200 <= y < 300:
l = 8
s = 1
elif 200 <= x < 300 and 200 <= y < 300:
l = 9
s = 1
while s:
if l == 1:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(50,50),40)
s=0
l=0
if l == 2:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(150,50),40)
s=0
l=0
if l == 3:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(250,50),40)
s=0
l=0
if l == 4:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(50,150),40)
s=0
l=0
if l == 5:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(150,150),40)
s=0
l=0
if l == 6:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(250,150),40)
s=0
l=0
if l == 7:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(50,250),40)
s=0
l=0
if l == 8:
if l in my_list:
s=0
l=0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(150,250),40)
s=0
l=0
if l == 9:
if l in my_list:
s = 0
l = 0
else:
my_list.append(l)
if z== 0:
c= color1
z= 1
my_list_black.append(l)
else:
c= color2
z= 0
my_list_red.append(l)
pygame.draw.circle(screen,c,(250,250),40)
s=0
l=0
if all(p in my_list_black for p in win[0]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (250, 50), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[1]):
pygame.draw.line(screen, (255, 255, 0), (50, 150), (250, 150), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[2]):
pygame.draw.line(screen, (255, 255, 0), (50, 250), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[3]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (50, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[4]):
pygame.draw.line(screen, (255, 255, 0), (150, 50), (150, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[5]):
pygame.draw.line(screen, (255, 255, 0), (250, 50), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[6]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_black for p in win[7]):
pygame.draw.line(screen, (255, 255, 0), (250, 50), (50, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[0]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (250, 50), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[1]):
pygame.draw.line(screen, (255, 255, 0), (50, 150), (250, 150), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[2]):
pygame.draw.line(screen, (255, 255, 0), (50, 250), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[3]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (50, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[4]):
pygame.draw.line(screen, (255, 255, 0), (150, 50), (150, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[5]):
pygame.draw.line(screen, (255, 255, 0), (250, 50), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[6]):
pygame.draw.line(screen, (255, 255, 0), (50, 50), (250, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
elif all(p in my_list_red for p in win[7]):
pygame.draw.line(screen, (255, 255, 0), (250, 50), (50, 250), 3)
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
pygame.display.flip()
'''
print(len(my_list))
print(my_list_red)
print(my_list_black)
'''
直接复制不能运行是因为我用了中文包,因为pygame本身不支持打印中文在界面上,想要使用必须自己外加中文包,在里面渲染之后才能使用,如果不想使用的可以直接注释掉相应部分,中文只为提示敲击空格重置游戏。
如果想要使用下载链接在这里https://www.fonts.net.cn/fonts-zh-1.html
在里面随便挑一个免费的(或者你有喜欢的自己但是要付费你打算买的)下载一个,并且和游戏的py文件放一个文件夹里改名为ttf即可(或者改代码中的文件名)