【闲来无事,py写game】知识竞答游戏 完美运行版本!

550 阅读6分钟

正文之前

这几天忙于写一个游戏程序,用于学院的一个毕业活动。。。。MMP我还是负责人,简直要死。。。所以都在赶制这个游戏程序中!!今天终于完成了逻辑上的设计,只差后面添加一些图片,以及扩充题库了!

正文

今天其实主要是在解决一个问题,那就是为啥不能显示中文??!!

一开始确实是有我从windows下拷贝过来一个文件,然后导致有个BOM还是什么的鬼东西。但是后来解决了。然后整天沉迷于编码解码。。。。到最后才发现,根本没啥卵子用!!不论是GBK还是UTF-8,在我的界面上都是一片方框。乱码表示!!沃日啊!!!

然后,我无意中搜了下,pygame如何显示中文。因为前面一直搜索python显示中文,结果都是叫我编码解码!!!我日哦 !!

结果,一大片答案!!!

最有用的还是这个:

怎样在pygame中显示中文

很有用啊!!!

要解决中文的显示问题,有两种方法。

  • 第一种方法:外带字体 在网上下载一个中文字体文件,将这个文件与我们的程序放在同一个文件夹,如果是中文的文件名,将它改成英文文件名。例如,下载了迷你简毡笔黑.TTF,将文件名改成了mnjzbh.ttf,并将程序的第一句改成: ZiTiDuiXiang=pygame.font.Font('mnjzbh.ttf',32) 这样,中文就能正确显示了。不过,有些下载的字体文件无法正常显示,可以多下载几个试试。
  • 第二种方法:使用系统字体 将程序的第一句更改成: ZiTiDuiXiang=pygame.font.SysFont('SimHei',32) 也就是用SysFont代替Font,并且使用系统自带字体,也可以正常显示中文。 不过,系统自带有很多字体,要选择其中的中文字体。如何查看系统带了哪些字体呢? 可以使用:
ZiTi=pygame.font.get_font()
for i in ZiTi:
   print(i)

来查看所有系统字体。

所以我就去下了两个字体。。

下载地址在这儿,有的付费,有的免费!

站长字体 免费的很多。尽情找!


然后今天还发现了几个好用的网站,一并推荐给大家!!

RGB配色表 很齐全 很贴心的配色表,比114那个强多了

www.pygame.org/docs/ref/im…

用Python和Pygame写游戏-从入门到精通(1) 很优秀的个人博主的内容。深有感触!

下面是我的代码和图片文件!

#!/usr/bin/python
#coding:utf-8

import time
import math
import pygame
import sys
from pygame.locals import *
white=255, 255, 255
blue = 1, 1, 200

def print_text( font, x, y, text, color=white, shadow=True ):
    if shadow:
        imgText = font.render(text,  True,  (0,  0,  0))
        screen.blit(imgText,  (x-2,  y-2))
    imgText=font.render(text,  True,  color)
    screen.blit(imgText,  (x,  y))


class Trivia(object):
    def __init__(self, filename):
        self.data = []
        self.current = 0
        self.total = 0
        self.correct = 0
        self.score1 = 0
        self.score2 = 0
        self.scored = False
        self.failed = False
        self.wronganswer = 0
        self.colors = [white,  white,  white,  white]

        f=open(filename,  'r', encoding="utf-8")
        tdata = f.readlines()
        for x in tdata:
            self.data.append(x.strip())
            self.total += 1

    def show_question(self):
        print_text(font1,  width/5, height/15,  "机械学院14级\"头脑风暴\"知识竞赛!", blue)
        print_text(font3,  width/3, height/20*19,  "***最终解释权归机械学院14级年级学生会所有***", white)
        print_text(font2,  width/12, height/25*4, "SCORE1", red)
        print_text(font2,  width/10*8 , height/25*4, "SCORE2", red)
        self.correct = int(self.data[self.current+5])
        question=int(self.current/6)+1
        print_text(font1, width/10*4, height/20*4, "Question "+str(question), orange)
        fontsize = int(1000/len(self.data[self.current]))
        if fontsize > 50:
            fontsize = 50
        if fontsize < 30:
            fontsize = 30
        if len(self.data[self.current])>35:
            str1 = self.data[self.current][0:35]
            if len(self.data[self.current])<=70:
                str2 = self.data[self.current][35:]
                print_text(pygame.font.Font('piaoyi.ttf',  fontsize),  width/6,  height/20*6,  str1, yellow)
                print_text(pygame.font.Font('piaoyi.ttf',  fontsize),  width/6,  height/20*7,  str2, yellow)
            else:
                str2 = self.data[self.current][35:70]
                str3 = self.data[self.current][70:]
                print_text(pygame.font.Font('piaoyi.ttf', 20),  width/6,  height/60*17,  str1, yellow)
                print_text(pygame.font.Font('piaoyi.ttf',  20),  width/6,  height/60*19,  str2, yellow)
                print_text(pygame.font.Font('piaoyi.ttf',  20),  width/6,  height/60*21,  str2, yellow)
        else:
            print_text(pygame.font.Font('piaoyi.ttf',  fontsize),  width/6,  height/20*6,  self.data[self.current],  yellow)

        if self.scored:
            self.colors[self.correct-1] = green
        elif self.failed:
            self.colors[self.wronganswer-1] = red
        print_text(font1, width/10*4, height/20*8, "Answer:",  orange)
        maxlen = 0
        for i in range(4):
            if len(self.data[self.current+i+1])>maxlen:
                maxlen = len(self.data[self.current+i+1])
        print_text(font2,  width/20*9-maxlen*20,  height/20*10,  "1 ->  "+self.data[self.current+1],  self.colors[0])
        print_text(font2,  width/20*9-maxlen*20,  height/20*12,  "2->  "+self.data[self.current+2],  self.colors[1])
        print_text(font2,  width/20*9-maxlen*20,  height/20*14,  "3->  "+self.data[self.current+3],  self.colors[2])
        print_text(font2,  width/20*9-maxlen*20,  height/20*16,  "4->  "+self.data[self.current+4],  self.colors[3])
        print_text(font2,  width/8,  height/5+10,  str(self.score1),  red)
        print_text(font2,  width/20*17,  height/5+10, str(self.score2),  red)

    def handle_input(self,  number):
        if not self.scored:
            if number == self.correct :
                self.scored = True
            else:
                self.failed = True
                self.wronganswer = number
        self.show_question()

    def next_question(self):
        if self.scored or self.failed:
            self.failed = False
            self.scored = False
            self.correct = 0
            self.colors = [white, white, white, white]
            self.current += 6
            if self.current >= self.total:
                self.current = 0

pygame.init()
width,  height = 1378,  776
screen = pygame.display.set_mode((width, height))
background = pygame.image.load("kate.png").convert()
frontground = pygame.image.load("dragon.png").convert_alpha()
pygame.display.set_caption("机械学院14级\"头脑风暴\"知识竞赛!")
font1 = pygame.font.Font('piaoyi.ttf',  60)
font2 = pygame.font.Font('piaoyi.ttf',  40)
font3 = pygame.font.Font('piaoyi.ttf',  20)

cyan = 0, 255, 255
yellow = 255, 255, 0
purple = 255, 0, 255
green = 0, 255, 0
red = 255, 0, 0
gray = 63, 63, 63
black = 8, 8, 8
orange = 255, 165, 0
Magenta = 255, 0, 255
trivia = Trivia("test.txt")

while True:
    screen.blit(background,  (0, 0))
    # screen.fill((0,255,0))
    # screen.blit(frontground, (-100, 88))
    trivia.show_question()
    pygame.display.update()
    Flag = True
    while Flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_ESCAPE:
                    sys.exit()
                elif event.key == pygame.K_w:
                    trivia.handle_input(1)
                elif event.key == pygame.K_h:
                    trivia.score1 += 2
                    pygame.display.update()
                elif event.key == pygame.K_j:
                    trivia.score2 += 2
                    pygame.display.update()
                elif event.key == pygame.K_k:
                    if trivia.score1>0:
                        trivia.score1 -= 1
                    pygame.display.update()
                elif event.key == pygame.K_l:
                    if trivia.score2>0:
                        trivia.score2 -= 1
                    pygame.display.update()
                elif event.key == pygame.K_a:
                    trivia.handle_input(2)
                elif event.key == pygame.K_s:
                    trivia.handle_input(3)
                elif event.key == pygame.K_d:
                    trivia.handle_input(4)
                elif event.key == pygame.K_n:
                    trivia.next_question()
                    Flag = False
                pygame.display.update()

同一个文件夹下命名为test.txt即可,测试内容:

噪声对人体哪个系统有害:
心血管系统
消化系统
呼吸系统
泌尿系统
1
地球上的人观看晴朗的天空呈现蓝色,这是因为:
大陆上的海水把天空映成蓝色
太阳光中的蓝色被物体反射成蓝色
太阳光中的蓝色光被天空中的微粒散射成蓝色
宇宙空间本身是蓝色
3
植物是很多动物的食物,植物自身则主要是利用光合作用制造养料。植物在进行光合作用的时候,除利用太阳能和自身的叶绿素外,还需要空气中的
二氧化碳
一氧化碳
氧气
一氧化氮
1
下列哪种喝水方式不宜采用:
饭后大量喝水
渴了再喝水
根据膳食营养素构成等因素增减喝水量
运动后喝水
1
哺乳动物是最高等的脊椎动物,靠母体的乳腺分泌乳汁哺育初生幼体。下面哪种是哺乳动物?
海豚
海马
海星
海龙
1
乒乓球瘪了用什么办法能使它鼓起来?
放到冰柜里
向里吹气
泡在开水里
捂在手里
3
蝙蝠晚上捉蚊子不靠眼睛,靠什么呢?
超声波
电磁波
脑电波
念力
1
用冰块冷冻食物,最好把冰块放在食物:
上方
下方
中间
外面
2

dragon.png

kate.png

还有两个字体文件没法上传?!!心痛。

正文之后

下面进行一定的效果展示,虽然丑。但是我的好用啊!!只要扩充题库,美化界面就好了。开玩笑!!