首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python游戏编程第三章trivia游戏中为何不能解析出中文的文件,用英文的文件则完美运行???

python游戏编程第三章trivia游戏中为何不能解析出中文的文件,用英文的文件则完美运行???

提问于 2022-05-22 15:09:10
回答 2关注 0查看 442
代码语言:javascript
复制
import sys,pygame
from pygame.locals import *
# -*- coding: UTF-8 -*-
class Trivia(object):
    def __init__(self,filename):
        self.data = []
        self.current = 0
        self.total = 0
        self.correct = 0
        self.score = 0
        self.scored = False
        self.failed = False
        self.wronganswer = 0
        self.colors = [white,white,white,white]

        f = open('Z:\pythonProject\pygame\pygame-2.1.2.dist-info\\filename.txt','r',encoding='utf-8')
        trivia_data = f.readlines()
        f.close()

        for text_line in trivia_data:
            self.data.append(text_line.strip())
            self.total += 1
    def show_question(self):
        print_text(font1,210,5,"TRIVIA GAME")
        print_text(font2,190,500-20,"Press Keys (1-4) To Answer",purple)
        print_text(font2,530,5,"SCORE",purple)
        print_text(font2,550,25,str(self.score),purple)

        self.correct = int(self.data[self.current+5])

        question = self.current // 6 + 1
        print_text(font1,5,80,"QUESTION " + str(question))
        print_text(font2,20,120,self.data[self.current],yellow)

        if self.scored:
            self.colors = [white,white,white,white]
            self.colors[self.correct-1] = green
            print_text(font1,230,380,"CORRECT!",green)
            print_text(font2,170,420,"Press Enter For Next Question",green)
        elif self.failed:
            self.colors = [white,white,white,white]
            self.colors[self.wronganswer-1] = red
            self.colors[self.correct-1] = green
            print_text(font1,220,380,"INCORRECT!",red)
            print_text(font2,170,420,"Press Enter For Next Question",red)

        print_text(font1,5,170,"ANSWERS")
        print_text(font2,20,210,"1- "+self.data[self.current+1],self.colors[0])
        print_text(font2,20,240,"2- "+self.data[self.current+2],self.colors[1])
        print_text(font2,20,270,"3- "+self.data[self.current+3],self.colors[2])
        print_text(font2,20,300,"4- "+self.data[self.current+4],self.colors[3])

    def handle_input(self,number):
        if not self.scored and not self.failed:
            if number == self.correct:
                self.scored = True
                self.score += 1
            else:
                self.failed = True
                self.wronganswer = number

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


def print_text(font,x,y,text,color=(255,255,255),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))

pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("The Trivia Game")
font1 = pygame.font.Font(None,40)
font2 = pygame.font.Font(None,24)
white = 255,255,255
cyan = 0,255,255
yellow = 255,255,0
purple = 255,0,255
green = 0,255,0
red = 255,0,0

trivia = Trivia("filename.txt")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            elif event.key == pygame.K_1:
                trivia.handle_input(1)
            elif event.key == pygame.K_2:
                trivia.handle_input(2)
            elif event.key == pygame.K_3:
                trivia.handle_input(3)
            elif event.key == pygame.K_4:
                trivia.handle_input(4)
            elif event.key == pygame.K_RETURN:
                trivia.next_question()

    screen.fill((0,0,200))
    trivia.show_question()
    pygame.display.update()

上边是我的源代码

这是我的文本
这是我的文本
这是运行出来的结果
这是运行出来的结果

请求大神解释一下,是本身不能用中文,还是我的代码写错了!!!

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档