python小游戏——井字棋
python小游戏实战——井字棋,pygame实现
·
前言
井字棋,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,由于棋盘一般不画边框,格线排成井字故得名。(直接百度照抄哈哈哈 )
另外,百度还给出了一些攻略,反正我是没看懂。。。(想不到小小井字棋都要勾心斗角 )
当然,在这个小游戏中,要让电脑像人一样智能是不太可能的,因为要涉及到很多的技术。因此,为了简化游戏,电脑下棋的位置将采用随机数的方式来确定。
绘制屏幕
import pygame
import sys
# 屏幕大小
screen_width, screen_height = 600, 600
# 格子大小
lattice_width = screen_width/3 - 1
lattice_height = screen_height/3 - 1
# 绘制屏幕
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("井字棋")
格子类
# 格子
class Lattice():
def __init__(self, rect, screen):
self.rect = rect
self.screen = screen
# 格子状态 -1表示格子被用户占领,1表示格子被电脑占领
self.stats = 0
# 格子样式
self.text_color = (30, 30, 30)
self.bg_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 100)
def draw(self):
msg = ""
# 用户字符U,电脑字符C
if self.stats == -1:
msg = "U"
elif self.stats == 1:
msg = "C"
else:
msg = ""
# 绘制格子
if msg:
self.msg_image = self.font.render(msg, True, self.text_color, self.bg_color)
self.msg_rect = self.msg_image.get_rect()
self.msg_rect.center = self.rect.center
self.screen.blit(self.msg_image, self.msg_rect)
九宫格
# 存储九宫格的内容
rects = [0] * 9
# 存储每个格子左上顶点的坐标
rect_wh = [(1, 1), (lattice_width+3, 1), (lattice_width*2+5, 1),
(1, lattice_height+3), (lattice_width+3, lattice_height+3), (lattice_width*2+5, lattice_height+3),
(1, lattice_height*2+5), (lattice_width+3, lattice_height*2+5), (lattice_width*2+5, lattice_height*2+5)
]
# 绘制九宫格
for i in range(len(rects)):
rects[i] = pygame.rect.Rect(*rect_wh[i], lattice_width, lattice_height)
rects[i] = Lattice(rects[i], screen)
说明:屏幕的原点位于左上角,x轴横向延伸,y轴纵向延伸。
说明:绘制格子时涉及到参数的封装与解封装。
弹窗类
# 弹窗
class Popup():
def __init__(self, screen, message):
self.msg = message
self.screen = screen
self.bg_color = (0, 0, 0)
self.text_color = (230, 230, 230)
self.font = pygame.font.SysFont(None, 48)
self.msg_image = self.font.render(self.msg, True, self.text_color, self.bg_color)
self.msg_rect = self.msg_image.get_rect()
self.screen_rect = self.screen.get_rect()
self.msg_rect.centerx = self.screen_rect.centerx
self.msg_rect.bottom = self.screen_rect.bottom
self.screen.blit(self.msg_image, self.msg_rect)
更新屏幕
# 更新屏幕
def update(time_sleep = 0, msg = ""):
screen.fill((255, 228, 181))
for rect in rects:
pygame.draw.rect(screen, (255, 255, 255), rect.rect)
rect.draw()
if msg:
Popup(screen, msg)
pygame.display.flip()
if time_sleep:
time.sleep(time_sleep)
电脑回合
# 电脑回合
def computer_round():
# 可选序号列表
random_num = [i for i in range(len(rects)) if not rects[i].stats]
# 没有可选序号
if not random_num:
update(3, "Draw!")
pygame.quit()
sys.exit()
rects[random.choice(random_num)].stats = 1
判断胜负
# 判断胜负
def judge_result():
global result
# 用户序号列表
stats_U = [i for i in range(len(rects)) if rects[i].stats == -1]
# 电脑序号列表
stats_C = [i for i in range(len(rects)) if rects[i].stats == 1]
# 胜出条件列表
win = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
# 遍历胜出条件列表,判断哪方胜出
for i in win:
if i == [j for j in i if j in stats_U]:
update(3, "You win!")
pygame.quit()
sys.exit()
elif i == [j for j in i if j in stats_C]:
update(3, "Computer win!")
pygame.quit()
sys.exit()
判断先手
# 判断先手
def judge_first():
# 0表示用户先手,1表示电脑先手
x = random.randint(0, 1)
if x:
update(1, "Computer's round!")
computer_round()
else:
update(1, "Your round!")
游戏入口
# 游戏入口
judge_first()
while True:
update()
# 获取屏幕事件
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 点击屏幕
elif event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标坐标
mouse_x, mouse_y = pygame.mouse.get_pos()
# 鼠标点击位置是否有效
position_valid = 0
for i in rects:
# 鼠标点击有效
if not i.stats and i.rect.collidepoint(mouse_x, mouse_y):
position_valid = 1
i.stats = -1
update()
judge_result()
update(1, "Computer's round!")
time.sleep(1)
computer_round()
update()
judge_result()
update(1, "Your round!")
# 鼠标点击无效
if not position_valid:
update(1, "Your choice is invalid!")
运行游戏
lattice.py
import pygame
# 格子
class Lattice():
def __init__(self, rect, screen):
self.rect = rect
self.screen = screen
# 格子状态 -1表示格子被用户占领,1表示格子被电脑占领
self.stats = 0
# 格子样式
self.text_color = (30, 30, 30)
self.bg_color = (255, 255, 255)
self.font = pygame.font.SysFont(None, 100)
def draw(self):
msg = ""
# 用户字符U,电脑字符C
if self.stats == -1:
msg = "U"
elif self.stats == 1:
msg = "C"
else:
msg = ""
# 绘制格子
if msg:
self.msg_image = self.font.render(msg, True, self.text_color, self.bg_color)
self.msg_rect = self.msg_image.get_rect()
self.msg_rect.center = self.rect.center
self.screen.blit(self.msg_image, self.msg_rect)
popup.py
import pygame
# 弹窗
class Popup():
def __init__(self, screen, message):
self.msg = message
self.screen = screen
self.bg_color = (0, 0, 0)
self.text_color = (230, 230, 230)
self.font = pygame.font.SysFont(None, 48)
self.msg_image = self.font.render(self.msg, True, self.text_color, self.bg_color)
self.msg_rect = self.msg_image.get_rect()
self.screen_rect = self.screen.get_rect()
self.msg_rect.centerx = self.screen_rect.centerx
self.msg_rect.bottom = self.screen_rect.bottom
self.screen.blit(self.msg_image, self.msg_rect)
main.py
import pygame
import sys
import random
import time
from lattice import Lattice
from popup import Popup
# 屏幕大小
screen_width, screen_height = 600, 600
lattice_width = screen_width/3 - 1
lattice_height = screen_height/3 - 1
# 绘制屏幕
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("井字棋")
# 建立九宫格
rects = [0] * 9
rect_wh = [(1, 1), (lattice_width+3, 1), (lattice_width*2+5, 1),
(1, lattice_height+3), (lattice_width+3, lattice_height+3), (lattice_width*2+5, lattice_height+3),
(1, lattice_height*2+5), (lattice_width+3, lattice_height*2+5), (lattice_width*2+5, lattice_height*2+5)
]
# 绘制九宫格
for i in range(len(rects)):
rects[i] = pygame.rect.Rect(*rect_wh[i], lattice_width, lattice_height)
rects[i] = Lattice(rects[i], screen)
# 更新屏幕
def update(time_sleep = 0, msg = ""):
screen.fill((255, 228, 181))
for rect in rects:
pygame.draw.rect(screen, (255, 255, 255), rect.rect)
rect.draw()
if msg:
Popup(screen, msg)
pygame.display.flip()
if time_sleep:
time.sleep(time_sleep)
# 电脑回合
def computer_round():
# 可选序号列表
random_num = [i for i in range(len(rects)) if not rects[i].stats]
# 没有可选序号
if not random_num:
update(3, "Draw!")
pygame.quit()
sys.exit()
rects[random.choice(random_num)].stats = 1
# 判断胜负
def judge_result():
global result
# 用户序号列表
stats_U = [i for i in range(len(rects)) if rects[i].stats == -1]
# 电脑序号列表
stats_C = [i for i in range(len(rects)) if rects[i].stats == 1]
# 胜出条件列表
win = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
]
# 遍历胜出条件列表,判断哪方胜出
for i in win:
if i == [j for j in i if j in stats_U]:
update(3, "You win!")
pygame.quit()
sys.exit()
elif i == [j for j in i if j in stats_C]:
update(3, "Computer win!")
pygame.quit()
sys.exit()
# 判断先手
def judge_first():
# 0表示用户先手,1表示电脑先手
x = random.randint(0, 1)
if x:
update(1, "Computer's round!")
computer_round()
else:
update(1, "Your round!")
# 游戏入口
judge_first()
while True:
update()
# 获取屏幕事件
for event in pygame.event.get():
# 退出游戏
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 点击屏幕
elif event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标坐标
mouse_x, mouse_y = pygame.mouse.get_pos()
# 鼠标点击位置是否有效
position_valid = 0
for i in rects:
# 鼠标点击有效
if not i.stats and i.rect.collidepoint(mouse_x, mouse_y):
position_valid = 1
i.stats = -1
update()
judge_result()
update(1, "Computer's round!")
time.sleep(1)
computer_round()
update()
judge_result()
update(1, "Your round!")
# 鼠标点击无效
if not position_valid:
update(1, "Your choice is invalid!")
敲黑板!!!祝各位兄弟姐妹六一儿童节快乐!!!
python小游戏——贪吃蛇:
https://blog.csdn.net/KissMoon_/article/details/117452272
凉梦空间
更多推荐
已为社区贡献1条内容
所有评论(0)