前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【闲来无事,py写game】一个问答游戏Trivia -来自《Python游戏编程入门》

【闲来无事,py写game】一个问答游戏Trivia -来自《Python游戏编程入门》

作者头像
用户1687088
发布2018-05-07 17:05:43
1.4K0
发布2018-05-07 17:05:43
举报

正文之前

嗯,没错,我只是为了规范化,就写这么多了!要洗澡了,明早有事!

正文

Trivia是一款书籍阅读类软件,支持Android 2.3.3。具体的内容是:

  1. 从网是络上收集比较人们比较容易忽视的冷知识
  2. 每周服务器端会自动更新冷知识
  3. 在阅读页面长按会弹出收藏和分享的list进行收藏和分享。

而我,做了个简化版的,好吧,这就是个问答游戏!没了~人家是卖内容,我啥都没得卖!!人家的高端版本的都是这么玩的!!

代码语言:javascript
复制
import pygame,sys
import time
import math
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.score = 0 
        self.scored = False
        self.failed = False
        self.wronganswer = 0
        self.colors = [white,white,white,white]

        f=open(filename,"r")
        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)

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

        question=self.current
        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.current-1] = green
            print_text(font1,230,380,"CORRECT!",green)
            print_text(font2,170,420,"Press Enter for Next One",green)
        elif self.failed:
            self.colors = [white,white,white,white]
            self.colors[self.current-1] = red
            print_text(font1,230,380,"INCORRECT!",red)
            print_text(font2,170,420,"Press Enter for Next One",red)
        print_text(font1,5,170,"Answer")
        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.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()
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("The Trivia Game")
font1=pygame.font.Font(None,40)
font2=pygame.font.Font(None,24)
cyan=0,255,255
yellow=255,255,0
purple=255,0,255
green=0,255,0
red=255,0,0

trivia=Trivia("/Users/zhangzhaobo/test.txt")

while(True):
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.type == pygame.K_ESCAPE:
                sys.exit()
            elif event.type == pygame.K_LEFT:
                trivia.handle_input(1)
            elif event.type == pygame.K_RIGHT:
                trivia.handle_input(2)
            elif event.type == pygame.K_UP:
                trivia.handle_input(3)
            elif event.type == pygame.K_DOWN:
                trivia.handle_input(4)
            elif event.type == pygame.K_RETURN:
                trivia.next_question()
    screen.fill((0,0,200))
    trivia.show_question()
    pygame.display.update()

不知道为啥,我没法进入下一个问题。感觉自己是错了。但是没找到跟书上的有差别的地方,有兴趣的可以玩下,我是没法了。要洗澡了,明早要去南一楼找教学主任签字。我要去计算机学院做毕设真是前路漫漫!!

英文版本的简介:

The trivia (singular trivium) are three lower Artes Liberales, i.e. grammar, logic, and rhetoric. These were the topics of basic education, foundational to the quadrivia of higher education, and hence the material of basic education and an important building block for all undergraduates.

The ancient Romans used the word triviae to describe where one road split or forked into two roads. Triviae was formed from tri (three) and viae (roads) – literally meaning "three roads", and in transferred use "a public place" and hence the meaning "commonplace."[2]

The pertaining adjective is triviālis. The adjective trivial was adopted in Early Modern English, while the noun trivium only appears in learned usage from the 19th century, in reference to the Artes Liberales and the plural trivia in the sense of "trivialities, trifles" only in the 20th century.[citation needed]

The Latin adjective triviālis in Classical Latin besides its literal meaning could have the meaning "appropriate to the street corner, commonplace, vulgar." In late Latin, it could also simply mean "triple." In medieval Latin, it came to refer to the lower division of the Artes Liberales, namely grammar, rhetoric, and logic. (The other four Liberal Arts were the quadrivium, namely arithmetic, geometry, music, and astronomy, which were more challenging.) Hence, trivial in this sense would have meant "of interest only to an undergraduate."[citation needed]

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 工科狗和生物喵 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正文之前
  • 正文
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档