做题的脚本:
#^_^coding=gbk ^_^
import linecache,sys,osscore_list = 8def set_answer():#数据分割 print '-----------------' global items,items1,items2,items3,items4,answers_dict f = [ x.replace('\n','') for x in linecache.getlines('question.txt')] items = [filter(lambda k:k.startswith(str(x)),f) for x in range(1,6)] items1 = filter(lambda k:k.startswith('A:'),f) items2 = filter(lambda k:k.startswith('B:'),f) items3 = filter(lambda k:k.startswith('C:'),f) items4 = filter(lambda k:k.startswith('D:'),f) answers = [ x.replace('\n','') for x in f if len(x) == 1 ] #把正确答案组成一个字典,方便后面字典查询 answers_dict = { x:answers[x] for x in xrange(3)} def get_question_cnt(): return len(items1) def score(): global score_list '判断用户输入的答案和正确答案是否一样,正确加2分,错误-4' for x in range(len(items1)): items_input = raw_input('{0}\n{1}\n{2}\n{3}\n{4} \n'.\ format(str(items[x][0]),items1[x],items2[x],items3[x],items4[x])) if items_input.upper() == answers_dict[x]: score_list += 2 print '恭喜您,回答正确! 您现在的分数是 %d' % score_list else: score_list = score_list - 4 print '回答错误 扣除4分! 您现在的分数是 %d' % score_list if score_list >= (8+2*len(items1)): print 'congratulations you get full score,start to play now' #启动游戏 os.popen('python snake.py') sys.exit(0)def welcome(): welcome_info = raw_input('嘿嘿,欢迎来到python的小游戏世界.(y:进入,q:退出) :' ) while True: if welcome_info == 'y': raw_input('总共%d题,四个答案(A,B,C,D),选择其中一个答案,\n基础分数8分,\ 答对一题加2分,错误减4分\n按回车继续' % 3) break else: print '成功退出!!!!!' score_list=0 sys.exit(0) break # 定义main函数def main(): set_answer() welcome() score() if __name__ == "__main__": main()还有一个简单的方案二:
#coding=gbk
import sysimport osimport linecachelist = raw_input('嘿嘿,欢迎来到python的小游戏世界.(y:进入,q:退出。)')if list.lower() != 'y': print '您已经成功退出!!!' sys.exit(0)listdata = linecache.getlines('question.txt')raw_input("规则:你现在有8个积分,以下5道问题,请输入A,B,C,D确定答案,答对得2分,答错扣4分,请按回车开始答题! ")print "开始进入游戏咯"mark = 8for x in range(3): x *= 7 answers = raw_input(''.join(listdata[x:x+5])) if answers == 'q': break if answers.upper() == listdata[x+5][0]: mark += 2 else: mark -= 4 if mark <= 0: print '您的分数已经小于0!!,请在来一次!!' break print '您现在的分数是 %d' %markif mark >= 14:
print 'congratulations you get full score' #启动游戏 os.popen('python snake.py') sys.exit(0)
做题答对之后,开始进入玩游戏:贪吃蛇
#!/usr/bin/env python
# -- coding: utf-8 --import pygame,sys,time,randomfrom pygame.locals import *# 定义颜色变量redColour = pygame.Color(255,0,0)#背景色设置backColour = pygame.Color(0,150,0)whiteColour = pygame.Color(255,255,255)greyColour = pygame.Color(150,150,150)#高和宽最好是cell的整数倍
cellSize=20windowWidth=cellSize*30windowHeight=cellSize*20# 定义gameOver函数
def gameOver(playSurface): #gameOverFont = pygame.font.Font('arial.ttf',72) #gameOverFont = pygame.font.Font("arial",72) #gameOverFont = pygame.font.SysFont("",72) gameOverFont = pygame.font.SysFont("arial",72) gameOverSurf = gameOverFont.render('Game Over', True, greyColour) gameOverRect = gameOverSurf.get_rect() gameOverRect.midtop = (320, 10)#2为蛇的初始长度
ScoreSurf = gameOverFont.render('Score: %s' % (len(snakeSegments)-2), True, (255, 0, 0)) ScoreRect = ScoreSurf.get_rect() ScoreRect.center = (windowWidth*2/3, windowHeight*2/3)playSurface.blit(gameOverSurf, gameOverRect) playSurface.blit(ScoreSurf, ScoreRect) pygame.display.flip() #从5秒改成了1秒 time.sleep(1) flag=True while(flag): for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key==K_SPACE: pygame.quit() sys.exit() flag=False
#绘制网格def drawgrid(): for i in xrange(cellSize,windowWidth,cellSize): pygame.draw.line(playSurface,(0,255,0),(i,0),(i,windowHeight),1) for j in xrange(cellSize,windowHeight,cellSize): pygame.draw.line(playSurface,(0,255,0),(0,j),(windowWidth,j),1) def gameStart(): playSurface.fill((0, 150, 0)) drawgrid() fontObj1 = pygame.font.Font('freesansbold.ttf', 60) textSurfaceObj1 = fontObj1.render('Greedy Snake', True, (255,255,255)) textRectObj1 = textSurfaceObj1.get_rect() textRectObj1.center = (windowWidth/2, windowHeight/3) playSurface.blit(textSurfaceObj1,textRectObj1)
fontObj2 =pygame.font.Font('freesansbold.ttf', 30)
textSurfaceObj2 = fontObj2.render('Devloper: you', True,(0, 0, 0)) textRectObj2 = textSurfaceObj2.get_rect() textRectObj2.center = (windowWidth/2, windowHeight*2/3) playSurface.blit(textSurfaceObj2,textRectObj2)pygame.display.update()
flag=True while(flag): for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key==K_SPACE: flag=Falsedef insnake(SrcPos):
for pos in snakeSegments[0:]: if SrcPos[0] == pos[0] and SrcPos[1] == pos[1]: print '------------' return 1; else: print SrcPos[0], pos[0],SrcPos[1],pos[1] print '+++++++++' return 0;# 定义main函数def main(): # 初始化pygame pygame.init() fpsClock = pygame.time.Clock() # 创建pygame显示层 global playSurface playSurface= pygame.display.set_mode((windowWidth,windowHeight)) gameStart() pygame.display.set_caption('Raspberry Snake') drawgrid() count=0# 初始化变量
snakePosition = [100,100] global snakeSegments #snakeSegments = [[100,100],[80,100],[60,100],[40,100],[20,100],[0,100],] snakeSegments = [[100,100],[0,0]] raspberryPosition = [300,300] raspberrySpawned = 1 direction = 'right' changeDirection = direction while True: # 检测例如按键等pygame事件 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: # 判断键盘事件 if event.key == K_RIGHT or event.key == ord('d'): changeDirection = 'right' if event.key == K_LEFT or event.key == ord('a'): changeDirection = 'left' if event.key == K_UP or event.key == ord('w'): changeDirection = 'up' if event.key == K_DOWN or event.key == ord('s'): changeDirection = 'down' if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) # 判断是否输入了反方向 if changeDirection == 'right' and not direction == 'left': direction = changeDirection if changeDirection == 'left' and not direction == 'right': direction = changeDirection if changeDirection == 'up' and not direction == 'down': direction = changeDirection if changeDirection == 'down' and not direction == 'up': direction = changeDirection # 根据方向移动蛇头的坐标 if direction == 'right': snakePosition[0] += cellSize if direction == 'left': snakePosition[0] -= cellSize if direction == 'up': snakePosition[1] -= cellSize if direction == 'down': snakePosition[1] += cellSize # 增加蛇的长度 snakeSegments.insert(0,list(snakePosition)) # 判断是否吃掉了树莓 if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]: raspberrySpawned = 0 else: snakeSegments.pop() # 如果吃掉树莓,则重新生成树莓 if raspberrySpawned == 0: Recreate=1 while Recreate: x = random.randrange(1,windowWidth/cellSize) y = random.randrange(1,windowHeight/cellSize) raspberryPosition = [int(x*cellSize),int(y*cellSize)] #如果生成的树莓刚好处于蛇占用的位置,则重新生成 if (not insnake(raspberryPosition)): count+=1 print count Recreate=0;raspberrySpawned = 1 # 绘制pygame显示层 playSurface.fill(backColour) for position in snakeSegments: pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],cellSize,cellSize)) pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],cellSize,cellSize)) drawgrid() # 刷新pygame显示层 pygame.display.flip() # 判断是否死亡 if snakePosition[0] > windowWidth or snakePosition[0] < 0: gameOver(playSurface) if snakePosition[1] > windowHeight or snakePosition[1] < 0: gameOver(playSurface) for snakeBody in snakeSegments[1:]: if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: gameOver(playSurface) # 控制游戏速度 fpsClock.tick(5)
if __name__ == "__main__":
main()