【原创】pygame学习笔记(3)--triviagame答题游戏

 

一 容易出错的地方

def _init_(self,name,score)

要记住, __init__ 前后都是两个下划线,而不只是1个下划线

 

二 代码测试

显示效果正常了,可以玩了

现在题目会循环玩

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

import sys
import pygame
from pygame.locals import *

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 filename
		f=open("temp\\pygame4_2.txt","r")
		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	

	def show_question(self):
		print_text(font1,210,5,"trivia game")
		print_text(font2,190,500-20,"Press Key (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(first)
		self.correct=int(self.data[self.current+5])
	
		#display question
		question=self.current//6+1
		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.correct-1]=red
			print_text(font1,230,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
			
		
#main progame begins
pygame.init()
screen=pygame.display.set_mode((600,500))
caption=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("temp\\pygame4_2.txt")




			
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))



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

 

 

三 优化显示

总觉得老外的这些游戏,可以改编一下后,会变得好玩很多?

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值