Trivia游戏

import sys, pygame 
from pygame.locals import * 

#游戏逻辑代码在Trivia类中
#所有游戏的逻辑都在该类中实现
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]

        #read trivia data from file 
        f = open(filename,"rt")
        trivia_data = f.readlines()
        f.close()

        #count and clean up trivia data 
        for text_line in trivia_data:
            self.data.append(text_line.strip())
            self.total += 1 
        print(self.data)
    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)

        #get correct answer out of data 
        self.correct = int(self.data[self.current+5])#这里要转换

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

        #respond to correct answer 
        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)

        #display answer 
        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  

#load the Trivia data file 
trivia = Trivia("t_data.txt")


#repeating loop 
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                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()

    #clear the screen 
    screen.fill((0,0,200))

    #display trivia data 
    trivia.show_question()

    #update the display 
    pygame.display.update()

# -*- coding: utf-8 -*-

import sys, pygame
from pygame.locals import *

#定义一些变量:
score = 0
scored = False
failed = False
wronganswer=0
current=0
total=0
data=[]
correct = 0

#先显示主界面
pygame.init()
screen= pygame.display.set_mode((780,500))
pygame.display.set_caption("the trivia game")
screen.fill((200,200,200))
#定义字体
font1=pygame.font.Font(None,50)
font2=pygame.font.Font(None,40)
#定义颜色
white= 255,255,255
cyan = 0,255,255
yellow = 255,255,0
purple = 255,0,255
green = 0,255,0
red= 255,0,0
black = 0,0,0
colors=[white,white,white,white]
currentquestion=1
#读取数据,打印在gui当中
data_file = open("t_data.txt","rt")
datas=data_file.readlines()
print(data)
data_file.close()

for lines in datas:
    total+=1
    data.append(lines.strip())



#打印方法
def print_text(x,y,font,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))
    #print("printing...")
    
def show_question():
    #画基本信息   
    global  score
    print_text(190,5,font1,"trivia game",purple)
    print_text(190,35,font2,"press keys(1-4)to answer ",purple)
    print_text(600,5,font2,"score",purple)
    print_text(600,25,font2,str(score),purple)
   # print(score)
    #画当前答题情况
    global current
    global data
    global colors
    global correct
    global currentquestion
    question =  current
    #question=currentquestion
    correct =int(data[current+5])
    print_text(5,80,font1,"question :"+str(question))
    print_text(20,120,font2,data[current],yellow)
    print_text(5,200,font1,"answers")
    
    #如果已经作答需要将结果用颜色表示出来
    
    if scored:
        colors=[white,white,white,white]
        colors[correct-1]=green
        print_text(230,380,font2,"correct",green)
        print_text(170,420,font2,"press enter for next question",green)
    elif failed:
        colors=[white,white,white,white]
        colors[correct-1]=green
        colors[wronganswer-1]=red
        print_text(230,380,font2,"incorrect",red)
        print_text(170,420,font2,"press enter for next question",red)
    
    print_text(20,240,font2,"1-"+data[current+1],colors[0])
    print_text(20,270,font2,"2-"+data[current+2],colors[1])
    print_text(20,300,font2,"3-"+data[current+3],colors[2])
    print_text(20,330,font2,"4-"+data[current+4],colors[3])

  #判断是否可以进入下一题 


def handle_input(num):
    global score
    global scored 
    global failed
    global wronganswer
    global correct
    
    if num == correct:
        scored= True
        score+=1
        print("correct answer")
    else:
        failed= True
        wronganswer = num
             


def next_question():
    global scored 
    global failed
    global colors
    global current
    
    if scored or failed : 
      print("get next question....")
      #要做一些清理任务
      scored =False
      failed = False
      colors=[white,white,white,white]
      #换题      
      current +=6
      global currentquestion
      currentquestion+=1
      if current >=total:
         current = 0  

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        #添加按键响应,以及对应的题目的填写
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                 print("1 \n")
                 handle_input(1)
            elif event.key == pygame.K_2:
                 print("2 \n")
                 handle_input(2)
            elif event.key == pygame.K_3:
                 print("3 \n")
                 handle_input(3)
            elif event.key == pygame.K_4:
                 print("4 \n")
                 handle_input(4)
            elif event.key == pygame.K_RETURN:
                 print("enter \n")
                 next_question()
            screen.fill((200,200,200))
    show_question()
    #screen.fill((200,200,200))
    pygame.display.update()
        

先点击叉号,然后用exit()结束

rt 的格式打开文件是以读/写方式打开一个文本文件,允许读和写。


以读/写方式打开一个文本文件,允许读和写。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值