#井字棋
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
pygame实现井字棋小游戏
最新推荐文章于 2025-02-05 19:30:00 发布