Python游戏之Trivia游戏

根据《Python 游戏编程入门》第三章的内容,经过一个问答小游戏对python的基本使用进行了了解。因为书上的面向对象的实现方法没有跑起来,因而用面向过程的思想实现了下。下面将一些细节部分记录一下。python

          流程:1.加载数据。经过Open函数读入题目相关数据。编程

                     2.其他的是业务逻辑部分,静下心来想清楚就能够了。app

                        main函数部分,实现对按键的响应(1,2,3,4,enter键)。其实中间也有一些地方没有想明白,但实际敲了一遍之后有些地方天然就清楚了。ide

                     3.变量 scored failed 设置两个变量其中一个目的是判断是否可以进入下一道题,若是只设置一个变量的话不能区分是选错了仍是没有选择。函数

                     4.关于变量做用域的问题等往后补上。字体

源代码:ui

# -*- coding: utf-8 -*-
"""
Created on Sat Jun 27 16:41:03 2015

@author: liuchang
"""


import sys, pygame
from pygame.locals import *


reload(sys)
#定义一些变量:
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("trivia.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()


截图:



其中题库文件:code

what is the name of the 4th planet from the Sun?
saturn
mars
earth
venus
2
which planet has the most moons in the solar system?
uranus
saturn
neptune
jupiter
4
approximately how large is the sun's diameter (width)?
65 thousand miles
45 million miles
1 million miles
825 thousnad miles
3
how far is the earth from the sun in its orbit (on average)?
13 million miles
93 million miles
250 million miles
800 million miles
2
what causes the earth's ocean to have tides?
the moon
the sun
earth's motion core
oxygen
1
对象