pygame的字体画不出来_python玩具——pygame扫雷

项目地址:ListeningRift/Minesweeper

其实扫雷说是使用pygame写游戏,但其实这非常锻炼思维,一个编程与解决问题的思路的养成非常重要,这篇文章的主要内容不是讲解功能的实现方式,而是介绍我遇到的一些问题

我自己的界面设计的能力有点差,所以在界面设计部分我借鉴了《python写扫雷小游戏(pygame)》—— 在校学渣一枚这位朋友的,再次表示十分感谢

原文:https://blog.csdn.net/qq_42847252/article/details/83153696*

好了,闲言少叙,一部分一部分的上

这个扫雷的主要实现思路就是用pygame划出格子后,界面上格子的行数列数,与另外一个二维列表的对应关系。

  1. 难度选择界面
import pygame
import sys
import main_game
from pygame.locals import *

#初始化模块
pygame.init()
pygame.mixer.init()

#音效加载
button_sound = pygame.mixer.Sound('material/sound/button.wav')
button_sound.set_volume(0.2)
background_sound = pygame.mixer.music.load('material/sound/background.ogg')
pygame.mixer.music.set_volume(0.3)

sys.setrecursionlimit(2000)

    #画出主界面
def draw_choose_interface(level):

    #level为难度等级,prompt为显示界面所显示的文字

    bg_size = width,height = 450,700
    screen = pygame.display.set_mode(bg_size)
    screen.fill((237,237,237))
    #画出选择框
    pygame.draw.rect(screen,(0,0,0),[175,100,100,300],5)
    #分割出三部分存放+,-和等级的位置
    pygame.draw.line(screen,(0,0,0),(175,200),(275,200),5)
    pygame.draw.line(screen,(0,0,0),(175,300),(275,300),5)
    #画出+的横线以及-
    pygame.draw.line(screen,(0,0,0),(195,150),(255,150),20)
    pygame.draw.line(screen,(0,0,0),(195,350),(255,350),20)
    #画出+的竖线
    pygame.draw.line(screen,(0,0,0),(225,120),(225,180),20)
    #开始游戏的选择框
    pygame.draw.rect(screen,(0,0,0),[100,450,250,100],5)
    #定义字体跟大小
    s_font1=pygame.font.Font('material/benmoyouyuan.ttf',50)
    s_font2=pygame.font.Font('material/benmoyouyuan.ttf',16)
    s_font3=pygame.font.Font('material/benmoyouyuan.ttf',34)
    #文本确定
    s_text1=s_font1.render(str(level),True,(0,0,0))
    s_text2=s_font1.render("开始游戏",True,(0,0,0))
    s_text3=s_font2.render('Listening_Rift',True,(0,0,0))
    s_text4=s_font3.render('难度选择:',True,(255,0,0))
    #将字放在窗口指定位置
    screen.blit(s_text1,(200,220))
    screen.blit(s_text2,(120,470))
    screen.blit(s_text3,(22,650))
    screen.blit(s_text4,(100,50))

    pygame.display.set_caption('难度选择')
    pygame.display.flip()

def _interface():
    level = '低'
    pygame.mixer.music.play(-1)
    draw_choose_interface(level)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if 175<event.pos[0]<275 and 100<event.pos[1]<200: if level="=" '低': button_sound.play() draw_choose_interface(level) elif '中': '高': 175<event.pos[0]<275 300<event.pos[1]<400: 100<event.pos[0]<350 450<event.pos[1]<550: main_game.game_main(level) __name__="=" '__main__': _interface()< code></event.pos[0]<275>

这部分其实就主要是界面的绘制以及事件的确定,本身没有什么难度,都是一些简单的用法,整体在不到100行,这也是扫雷游戏的登入部分。

  1. 接下来就要将整个游戏分块考虑并且编写,首先我把布雷部分写出来,这部分并不是很难,甚至可以说是最简单的,我是个初学者,五六十行就可以实现。
import random

def lay_mines(level,map1):
    if level == '&#x4F4E;':
        a = 0
        while a<10: x="random.randint(0,9)" y="random.randint(0,9)" map1[x][y]="9" print('布雷功能运行正常') a="0" for i in range(10): j if map1[i][j]="=" '9': +="1" print(a) elif level="=" '中': while a<40: range(16): '高': a<99: range(32): print(a)< code></10:>

布雷的部分实际上就是分等级并排出不同数量的雷

这里我遇到最大的问题就是”a = 0″ 这一句,这里是想通过一个遍历的方法,数出十个雷,确保没有重复,但实际上random好像是有这个函数的,但是也有一定bug,还是要通过一些方法规避,如果没有这一句的话,a实际上是一个类加量,并不会真正布十个雷,初级4个,中级9个,高级14个,所以在计算之前一定要将其初始化为0

  1. 点开事件

在扫雷游戏中,我们在点到一个非雷格子的时候,有的会出现数字有的会直接点开一片,在明确这个规则之后,我们就可以开始编写了

import pygame
import main_game
from pygame.locals import *

pygame.mixer.init()
boom_sound = pygame.mixer.Sound('material/sound/BOOM.wav')
boom_sound.set_volume(0.2)

#&#x6BD4;&#x8F83;&#x5F97;&#x51FA;&#x6570;&#x5B57;
def _digital(i,j,map1):
    count = 0
    if i-1>=0 and j-1>=0 and map1[i-1][j-1] == '9':
        count += 1
    if i-1>=0 and map1[i-1][j] == '9':
        count += 1
    if i-1>=0 and j+1<len(map1[0]) and map1[i-1][j+1]="=" '9': count +="1" if j-1>=0 and map1[i][j-1] == '9':
        count += 1
    if j+1<len(map1[0]) and map1[i][j+1]="=" '9': count +="1" if i+1<len(map1) j-1>=0 and map1[i+1][j-1] == '9':
        count += 1
    if i+1<len(map1) and map1[i+1][j]="=" '9': count +="1" if i+1<len(map1) j+1<len(map1[0]) map1[i+1][j+1]="=" return #周围判断 def arround(screen,i,j,map1,map2): map1[i][j]="=" omine_image="pygame.image.load('material/picture/mine.gif').convert()" mine_image="pygame.transform.scale(omine_image,(22,22))" screen.blit(mine_image,(i*30+3,j*30+3)) boom_sound.play() pygame.display.flip() for x in range(len(map1)): y range(len(map1[0])): map1[x][y]="=" screen.blit(mine_image,(x*30+3,y*30+3)) pygame.time.delay(100) result="&#x6E38;&#x620F;&#x5931;&#x8D25;" pygame.time.delay(1000) main_game.result_screen(result) #显示lose else: digital="_digital(i,j,map1)" print('内容填写部分正常') '0': print('判断成功') i-1>=0 and j-1>=0 and len(map1[i-1][j-1]) == 0 and map2[i-1][j-1] == '':
                arround(screen,i-1,j-1,map1,map2)
            if i-1>=0 and len(map1[i-1][j]) == 0 and map2[i-1][j] == '':
                arround(screen,i-1,j,map1,map2)
            if i-1>=0 and j+1<len(map1[0]) 0 and len(map1[i-1][j+1])="=" map2[i-1][j+1]="=" '': arround(screen,i-1,j+1,map1,map2) if j-1>=0 and len(map1[i][j-1]) == 0 and map2[i][j-1] == '':
                arround(screen,i,j-1,map1,map2)
            if j+1<len(map1[0]) 0 and len(map1[i][j+1])="=" map2[i][j+1]="=" '': arround(screen,i,j+1,map1,map2) if i+1<len(map1) j-1>=0 and len(map1[i+1][j-1]) == 0 and map2[i+1][j-1] == '':
                arround(screen,i+1,j-1,map1,map2)
            if i+1<len(map1) 0 and len(map1[i+1][j])="=" map2[i+1][j]="=" '': arround(screen,i+1,j,map1,map2) if i+1<len(map1) j+1<len(map1[0]) len(map1[i+1][j+1])="=" map2[i+1][j+1]="=" arround(screen,i+1,j+1,map1,map2) s_font="pygame.font.Font('material/benmoyouyuan.ttf',19)" map1[i][j]="=" '1': color="(86,98,166)" elif '2': '3': else: s_text="s_font.render(str(map1[i][j]),True,color)" #将字放在窗口指定位置 screen.blit(s_text,(i*30+9,j*30+3)) #将为零的格子填充为新颜色 for i in range(len(map1)): j range(len(map1[0])): ' ': pygame.draw.rect(screen,(200,200,200),[i*30,j*30,29,29])< code></len(map1)></len(map1[0])></len(map1[0])></len(map1)></len(map1[0])></len(map1[0])>

靠通过审查周围雷的数量填入数字,如果为零就计算他周围的进行循环计算,这样就可以实现点开一大片。

问题:

  • [ ] a、最初,当计算周围的时候在周围的两个空格子无限死循环下去。解决方法:将为零的格子转换为空格,只计算内部内容有长度的格子。

  • [ ] b、但是,之后发现int变量是没有长度的,就只好将所有的内容全部变为字符串,原本用9表示雷,现在则用’9’表示。

这个部分实际上最重要的就是判断标准的确定,需要仔细地进行思考。

  1. 主体游戏

好吧,我承认我的架构思路确实略有问题,原本是想把不同部分分开的后来在调试优化过程中,就逐渐的加到一起了。。。

import pygame
import traceback
import sys
import random
import choose
import lay_mines
import open_event
from pygame.locals import *

#9&#x4EE3;&#x8868;&#x96F7;
#10&#x4EE3;&#x8868;&#x65D7;&#x5E1C;

sys.setrecursionlimit(1000)
#&#x677F;&#x5757;&#x521D;&#x59CB;&#x5316;
pygame.init()

#&#x97F3;&#x6548;&#x8F7D;&#x5165;&#xFF0C;&#x80CC;&#x666F;&#x97F3;&#x4E50;&#xFF0C;&#x70B9;&#x51FB;&#x97F3;&#x6548;&#xFF0C;&#x7206;&#x70B8;&#x97F3;&#x6548;&#xFF0C;&#x97F3;&#x91CF;&#x5747;&#x4E3A;0.2
pygame.mixer.init()
button_sound = pygame.mixer.Sound('material/sound/button.wav')
button_sound.set_volume(0.2)
boom_sound = pygame.mixer.Sound('material/sound/BOOM.wav')
boom_sound.set_volume(0.2)
background_sound = pygame.mixer.music.load('material/sound/background.ogg')
pygame.mixer.music.set_volume(0.2)

#&#x5224;&#x5B9A;&#x7ED3;&#x679C;
def result_judge(map1,map2):

    mine = []
    flag = []

    for x in range(len(map1)):
        for y in range(len(map1[0])):
            if map1[x][y] == '9':
                mine.append((x,y))
            if map2[x][y] == '10':
                flag.append((x,y))
    if mine == flag:
        result = '&#x6E38;&#x620F;&#x80DC;&#x5229;'
    else:
        result = '&#x6E38;&#x620F;&#x5931;&#x8D25;'

    return result

def  result_screen(result):

    #&#x5EFA;&#x7ACB;&#x754C;&#x9762;
    bg_size = width,height = 450,700
    r_screen = pygame.display.set_mode(bg_size)
    r_screen.fill((237,237,237))

    r_font1 = pygame.font.Font('material/benmoyouyuan.ttf',67)
    r_font2 = pygame.font.Font('material/benmoyouyuan.ttf',50)

    pygame.draw.rect(r_screen,(0,0,0),[100,450,250,100],5)

    r_text1 = r_font1.render(str(result),True,(0,0,0))
    r_text2 = r_font2.render("&#x7EE7;&#x7EED;&#x6E38;&#x620F;",True,(0,0,0))

    r_screen.blit(r_text1,(90,100))
    r_screen.blit(r_text2,(120,470))

    pygame.display.set_caption('&#x6E38;&#x620F;&#x7ED3;&#x675F;')

    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if 100<event.pos[0]<350 and 450<event.pos[1]<550: pygame.display.quit() choose._interface() def game_main(level): #生成主界面 if level="=" '低': bg_size="width,height" = 300,300 elif '中': 480,480 '高': 960,480 screen_main="pygame.display.set_mode(bg_size)" screen_main.fill((237,237,237)) pygame.display.set_caption('l_r扫雷:%s级模式'%level) pygame.mixer.music.play(-1) #画格子 for x in range(width 30): y range(height pygame.draw.rect(screen_main,(0,0,0),[x*30,y*30,29,29],1) pygame.display.flip() #初始化地图二维列表 map1="[[0" col range(10)] row map2="[[0" i range(10): j map1[i][j] map2[i][j] range(16)] range(16): range(32)] range(32): #布雷 lay_mines.lay_mines(level,map1) while true: event pygame.event.get(): event.type="=" quit: pygame.quit() sys.exit() mousebuttondown: event.button="=" 1: button_sound.play() (map1[i][j]="=" '' or '9') '': open_event.arround(screen_main,i,j,map1,map2) print('翻开成功') 3: oflag="pygame.image.load('material/picture/flag.gif')" flag="pygame.transform.scale(oflag,(22,22))" screen_main.blit(flag,(i*30+3,j*30+3)) print('标记成功') '10': print('取消标记') oblank="pygame.image.load('material/picture/blank.gif')" mines="10" flags="0" range(len(map2)): range(len(map2[0])): map2[x][y]="=" +="1" mines: result="result_judge(map1,map2)" pygame.time.delay(1000) result_screen(result)< code></event.pos[0]<350>

游戏主体部分包含胜负判断

最大的问题在于、标记时会影响判断,最一开始的标记是把二维列表中的值改编为10就会影响每个格子里的值,所以后来我又建立了另一张二维列表,专门存放旗子,这样就可以将他标记完成并不影响值。

最终还想实现战绩记录,每个模式的最短时间,以及胜率。这个部分连上界面优化就比较简单了,所以就日后慢慢进行吧。

Original: https://blog.csdn.net/weixin_34221154/article/details/112265267
Author: 踏歌西行
Title: pygame的字体画不出来_python玩具——pygame扫雷

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/784202/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Python中的路径

    windows路径使用的是\,linux路径使用的是/。 特别的,在windows系统中如果有这样的一个路径 D:\nxxx\txxx\x1,程序会报错。因为在路径中存在特殊符 \…

    Python 2023年6月3日
    069
  • Python中dataclass库

    一、 简介 dataclass的定义位于PEP-557,根据定义一个dataclass是指”一个带有默认值的可变的namedtuple”,广义的定义就是有一…

    Python 2023年6月9日
    070
  • Python Scrapy爬虫技术

    目录 前言 * 网络爬虫 scrapy框架 scrapy架构 使用方法 * 创建scrapy爬虫 scrapy爬虫配置(setting.py) scrapy爬虫配置(item.py…

    Python 2023年10月4日
    037
  • python中文乱码-ValueError: Key axes.unicode_minus: Could not convert “flase“ to bool错误的解决方法

    一、背景 本人按照下面的参考链接中的永久解决乱码问题对应的方法一步步地执行时,出现了错误【ValueError: Key axes.unicode_minus: Could not…

    Python 2023年8月31日
    062
  • Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:…

    Python 2023年6月10日
    0116
  • 前后端分离项目(六):数据分页查询(前端视图)

    🚀 优质资源分享 🚀 学习路线指引(点击解锁)知识定位人群定位🧡 Python实战微信订餐小程序 🧡 进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯…

    Python 2023年8月14日
    076
  • 单元测试之pytest

    一、命名规则Pytest单元测试中的类名和方法名必须是以test开头,执行中只能找到test开头的类和方法,比unittest更加严谨案例import pytestfrom xml…

    Python 2023年9月15日
    033
  • python绘图-matplotlib应用笔记

    目录 * – figure/axis/axes/plt分别表示什么? – + * plt * Figure:可以解释为画布。 * Axes 是图像中具有数据…

    Python 2023年8月31日
    053
  • opencv cv.line

    python;gutter:true; ''' 本次来学习基于opencv进行各种画图操作,以前只习惯用matplotlib,最近开始用opencv,…

    Python 2023年6月15日
    055
  • 【pytest-fixture】四、fixture的自动执行(无需请求)和使用标记将数据传递给fixtrue

    我基本上是跟着官网去学习fixture,自己先看并学了一遍,为了方便后续查看和记忆,所以将学习笔记记录下来,如果有不对的地方,欢迎大家评论区指出。 目录 * – 1.f…

    Python 2023年9月11日
    043
  • numpy学习记录

    numpy教程 SciPy(Scientific Python):功能:插值matplotlib(绘图库) 在 Win10上安装numpy 在anaconda上安装记录:输入命令行…

    Python 2023年8月30日
    054
  • 基于5G技术的低轨卫星物联网技术

    【摘 要】基于5G技术的低轨卫星物联网融合了地面5G技术灵活有效与低轨卫星网络广域覆盖的优势,是实现全球海量物联终端广域连续性泛在接入的必然选择。针对地面5G技术与低轨卫星物联网的…

    Python 2023年10月10日
    036
  • 刘亦菲生日当天,引发了我对正则的思考

    前两天从网上采集到一条短视频数据(刷短视频),发现六公主连排5部刘亦菲主演的电影!甚是震惊,太有牌面了,看了一下日子是8月25号,嗷,原来当天是刘亦菲的生日。巧了,正好也是我家柴犬…

    Python 2023年10月23日
    038
  • python两个数据表中的对应数据相加

    记录一下自己的学习过程。 有很多省份的数据,想要求全国的综合。这些数据都分别存在csv里。如下: 我希望把全部省份加起来, 算一个全国总和。这时候需要用到数据表对应值相加。 代码如…

    Python 2023年8月6日
    047
  • py214-基于Python+django的网购平台购物商城

    开发语言:Pythonpython框架:django软件版本:python3.7/python3.8数据库:mysql 5.7或更高版本数据库工具:Navicat11开发软件:Py…

    Python 2023年8月5日
    041
  • Ableton Live 11 Suite:强大的音乐创作软件

    Original: https://www.cnblogs.com/aurora-123/p/16733484.htmlAuthor: 佛系女孩Title: Ableton Liv…

    Python 2023年10月28日
    031
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球