python游戏编程入门 免费-Python游戏编程入门2
I/O、数据和字体:Trivia游戏
本章包括如下内容:
Python数据类型
获取用户输入
处理异常
Mad Lib游戏
操作文本文件
操作二进制文件
Trivia游戏
其他的不说,我先去自己学习文件类型和字符串类型去了。这部分讲道理换行符还
是有点丑陋的,Python还需要想着各种地方加换行符,狗屎。
不去想书中示例丑陋的换行符,只要不影响其中几个重要的游戏效果就行。
实验空行会不会对于trivia游戏的结果产生影响
Python文件类型
f=open("filename","r")
读写模式r w a r+ w+ a+
r只读模式
w只写模式,已有文件内容会被清空,文件不存在则会创建新文件
a添加模式,不清空文件,在文件末尾加入内容
其他的说不了,很复杂。
怎么对文件指针位置进行调整
string.strip()函数删除末尾的换行符
madlib游戏简介:
Mad Lib游戏相当简单。它要求某个人填入一些名称、事情、地点,然后使用这些单词和短语来组成一个
故事,往往会得到出人意料的、幽默的结果。这个小程序的有趣之处在于,故事是如何构建出来的(确实如此)。
这个程序是经过修改的,和书中story部分不同,没有许多令人心烦的换行符。而是直接用三个引号来完成多
行的输入。
论三个引号会怎么影响sublime的Python输出格式(所有的代码一篇黄)。
#!/usr/bin/python
print("MAD LIB GAME")
print("Enter answers to the following prompts")
guy=raw_input("Name of a famous man:")
girl=raw_input("Name of a famous woman:")
food=raw_input("Your favorite food:")
ship=raw_input("Name of a space ship:")
job=raw_input("Name of a profession:")
planet=raw_input("Name of a planet:")
drink=raw_input("Your favorite drink:")
number=raw_input("A number from 1 to 10:")
story="""
A famous married couple,GUY and GIRL,went on
vacation to the planet PLANET.It took NUMBER
weeks to get there travelling by SHIP.They
enjoyed a luxurious candlelight dinner overlooking
a DRINK ocean while eating FOOD.But,since
they were both JOB,they had to cut their
vacation short."""
story=story.replace("GUY",guy)
story=story.replace("GIRL",girl)
story=story.replace("FOOD",food)
story=story.replace("SHIP",ship)
story=story.replace("JOB",job)
story=story.replace("PLANET",planet)
story=story.replace("DRINK",drink)
story=story.replace("NUMBER",number)
print(story)
这个程序值得学习的部分:
对于字符串类型,可以使用str.replace将给定字符替换成相应的变量。
嗯,小故事也不错。
trivia游戏简介:
Trivia游戏,从一个文件中读取出一些问题,并且要求用户从多个可选的答案中做出选择。
这个游戏涉及数据类型的设计,对于现在我面向对象的编程水平来说有点困难。需要一步一步将
整个程序分块分析,看看面向对象的编程设计思路。也可以帮助理解为什么面向对象是一种更高
级的编程方式。
#!/usr/bin/python
import sys,pygame
from pygame.locals import *
pygame.init()
#定义数据类型
#Trivia包括__init__,show_question,handle_input,next_question等。一个初始化和三个自带函数。
#__init__定义了Trivia所应包含的常量。data,current,total,correct,score,scored,failed,
#wronganswer和colors。其中data是从文件中读取的字符串列表。
#show_question负责显示当前问题,它会调用到全局的函数print_text
#handle_input负责判断对错以及对score,scored,failed,wronganswer的进行相应的修改
#next_question负责重置scored,failed,current,correct
class Trivia():
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("trivia_data.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 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(first)
if (self.current+5)
self.correct=int(self.data[self.current+5])
else:
sys.exit()
#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 Quetion",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,230,380,"INCORRECT!",red)
print_text(font2,170,420,"Press Enter For Next Quetion",red)
#display answers
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))
#main program begins
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("trivia_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()
这段代码感觉就是面向对象语言的入门范例。首先设计好数据结构,主函数相对来说简单明了。这个跨度我感觉有一点大,
还需要一定程度的积累才行。
python游戏编程入门 免费-Python游戏编程入门2相关推荐
- python入门编程软件免费-Python编程干货免费领取!!!
原标题:Python编程干货免费领取!!! 早在18 年,教育部就正式将人工智能.物联网.大数据处理正式划入高中新课标,这就意味着现在的学生16岁就要开始学习编程了! 开发岗位的高薪和人工智能的发展, ...
- python零基础电子书免费下载-零基础入门学习Python PDF 扫描版
给大家带来的一篇关于Python编程相关的电子书资源,介绍了关于Python.零基础.入门学习方面的内容,本书是由清华大学出版社出版,格式为PDF,资源大小59.3 MB,小甲鱼编写,目前豆瓣.亚马逊 ...
- python游戏编程入门 免费-python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- python游戏编程入门免费_python游戏编程入门 python游戏编程入门课
python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...
- python游戏编程入门 免费-Python游戏编程入门4
Math和Graphics:Analog Clock示例程序 本章介绍Python的math模块,该模块可以执行计算,如常见的三角正弦函数.余弦函数.正切函数等. 使用正弦和余弦函数绘制圆 创建Anl ...
- python入门编程软件免费-Python 3.7.0编程软件免费下载
软件介绍 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),已经具有十多年的发展历史,成熟且稳定.这种语言具有非常简捷而清晰的语法 ...
- python编程软件免费吗_MRT7-Python编程软件
MRT7-Python编程软件适用于儿童编程,由韩端科技推出,提供Boclky编程.Python代码编程等多种编程模式,支持配合设备进行使用,软件安装操作起来并不困难,用户可以根据自己的系统安装,软件 ...
- 经典按键java手机游戏_各种免费手机游戏可以玩了!
今天,Killer怎能不为大家分享一款超级实用的手机游戏模拟器破解软件,让你能玩到各种类型的免费游戏,喜欢玩游戏的小伙伴一定不要错过哦! | 海星模拟器破解版 首先,这是一款包含了各种类型游戏的游戏平 ...
- 免费python网络课程-2019年10种免费的Python学习课程
近年来,越来越多的人在学习Python.大部分人是为了探索Python提供的数据科学和机器学习库.也有些人学习Python是为了进行Web开发,还有许多人是为了编写脚本并将其自动化.现在为什么要学习P ...
最新文章
- 百万数据修改索引,百万数据修改主键
- C#使用WIN32API来高效率的遍历文件和目录(转)
- shell中数组基础语法
- 【DL4J速成】Deeplearning4j图像分类从模型自定义到测试
- R语言forestmodel包使用教程
- why is pricing callback CRM_PRIDOC_UPDATE_EC called
- 编程随想 关系图_IT什么岗位比较好找工作?一张金字塔图就能明白
- 深度技术Win11 64位最新旗舰版镜像V2021.08
- Mac Nginx 配置 Tomcat 配置 jdk环境变量 Nginx部署服务遇到的坑(1)
- windows下手动安装pyinstaller(python2.7)
- 冲刺第一天 11.23 FRI
- 系统集成项目管理工程师教程 第二版下载,仅供学习交流使用
- u盘扩容盘用什么软件测试,扩容盘,小编教你如何检测U盘是否为扩容盘
- 去掉U盘写保护 修复u盘错误
- Android 微信登陆
- Tensorflow 中文语音识别
- intellij idea 设置代理 代理下载
- 数字企业-数字社会-数字中国,统一一套方法论
- Android离线人脸识别方案对比
- DFS and BFS