pygame实现井字棋小游戏

#井字棋
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值