博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小朋友做对题才能玩的游戏
阅读量:6257 次
发布时间:2019-06-22

本文共 7527 字,大约阅读时间需要 25 分钟。

做题的脚本:

#^_^coding=gbk ^_^

import linecache,sys,os
score_list = 8
def 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 sys
import os
import linecache
list = 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 = 8
for 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' %mark

if 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,random
from 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=20
windowWidth=cellSize*30
windowHeight=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=False

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

 

转载于:https://www.cnblogs.com/10087622blog/p/7613796.html

你可能感兴趣的文章
架構設計_案例說明_by 高老師
查看>>
VR內容開發(PAGE-2)
查看>>
飞舞的蝴蝶
查看>>
Linux环境下C语言模拟内存负载测试
查看>>
专栏《轻松玩转ELK海量可视化日志分析系统》已完结【附代码下载】
查看>>
新手IT人员,如何找到适合自己专业书籍的6个问题?
查看>>
初学者学习linux运维的几个问题及老鸟建议
查看>>
Server 2008 R2 AD RMS完整部署:RMS部署篇
查看>>
SFB 项目经验-45-用培训课件当运维文档,聪明
查看>>
使用Kubernetes创建PHP留言板系统
查看>>
时间管理,从洗碗开始
查看>>
我用EDM卖约会秘籍的半个月
查看>>
运营这个职业的诞生缘由「社区运营入门系列④」
查看>>
在VMM2012R2中使用二代虚拟机创建的模板无法创建虚拟机的解决方法
查看>>
大道至简 电话号码重新成为O2O新宠
查看>>
Office 365离线安装
查看>>
jar包与was版本不兼容怎么办
查看>>
将Windows Server 2008 R2网络升级到Windows Server 2012
查看>>
修改计算机名的注意事项
查看>>
WIN7关闭共享后怎样去掉图标上的小锁
查看>>