首先确保你的电脑上已经安装了python3.x以上的python环境,并且安装有pycharm和pygame库(pygame直接windows键+r键,cmd,输入pip install pygame即可),如果这些都装完了,就开始。

python是一种解释型语言,变量可以先是数字,然后赋值为字符串。

如果你想把数字转变为字符串,可以用str()函数,其它转换为int或flote也是类似,具体使用演示如下。

print()函数用于向屏幕输出,不同的类型用逗号隔开就可以一同输出。

open()用于打开文件。

写入用write()函数,模式和上图类似。

读取用read()可以在括号里传入具体读入字符数,readline()、readlines()读取一行、多行。

trivia_data.txt中的内容


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 thousand miles
3
How far is the Earth from the Sun in its orbit (on average)?
13 million miles
93 million miles
250 thousand miles
800 thousand miles
2
What causes the Earth's oceans to have tides?
The Moon
The Sun
Earth's molten core
Oxygen
1

代码


# The Trivia Game
# Chapter 3
#导入需要的库
import sys, pygame
from pygame.locals import *class Trivia(object):#参数object3.x后可以不添加def __init__(self, filename):#构造函数self.data = []self.current = 0self.total = 0self.correct = 0self.score = 0self.scored = Falseself.failed = Falseself.wronganswer = 0self.colors = [white,white,white,white]#read trivia data from filef = open(filename, "r")trivia_data = f.readlines()f.close()#count and clean up trivia datafor text_line in trivia_data:self.data.append(text_line.strip())self.total += 1def 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)self.correct = int(self.data[self.current+5])#display questionquestion = self.current // 6 + 1print_text(font1, 5, 80, "QUESTION " + str(question))print_text(font2, 20, 120, self.data[self.current], yellow)#respond to correct answerif self.scored:self.colors = [white,white,white,white]self.colors[self.correct-1] = greenprint_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.wronganswer-1] = redself.colors[self.correct-1] = greenprint_text(font1, 220, 380, "INCORRECT!", red)print_text(font2, 170, 420, "Press Enter For Next Question", red)#display answersprint_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 = Trueself.score += 1else:self.failed = Trueself.wronganswer = numberdef next_question(self):if self.scored or self.failed:self.scored = Falseself.failed = Falseself.correct = 0self.colors = [white,white,white,white]self.current += 6if self.current >= self.total:self.current = 0def 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 screenscreen.fill((0,0,200))#display trivia datatrivia.show_question()#update the displaypygame.display.update()

运行结果如上图,代码部分有注释,核心流程就是读取一部分文件,显示,然后根据输入的1、2、3、4判断对错,统计得分,然后继续游戏。

(以下是我的公众号:知识技术与人生,需要请关注)

Python游戏编程入门第二课相关推荐

  1. python游戏编程入门 免费-python游戏编程入门 python游戏编程入门课

    python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...

  2. python游戏编程入门免费_python游戏编程入门 python游戏编程入门课

    python游戏编程入门 python游戏编程入门课 什么是python游戏编程入门?首先我们需要认识什么是Python Python既是一个软件工具包,也是一种语言.Python软件包包含了一个名为 ...

  3. C# 编程入门第二课 注释变量,VS2019快捷键,String和string,命名规则,赋值运算符,+号作用占位符,转义字符算术运算符,类型转换

    C# 编程入门第二课 文章目录 C# 编程入门第二课 1. 注释 2 变量 3.VS2019快捷键 4. String和string 5. 命名规则 6. 赋值运算符,+号作用 7. 占位符 8.转义 ...

  4. 青少年Python游戏编程入门(Beginning Game Programming for Teens with Python译文)

    青少年Python游戏编程入门 Beginning Game Programming for Teens with Python   Julian Meyer on January 22, 2013 ...

  5. python游戏编程入门-python游戏编程入门

    <Python游戏编程入门> 这些文章负责整理在这本书中的知识点.注意事项和课后习题的尝试实现. 并且对每一个章节给出的最终实例进行分析和注释. 初识pygame:pie游戏 pygame ...

  6. 《Python游戏编程入门》——1.2 初识Python

    本节书摘来自异步社区<Python游戏编程入门>一书中的第1章,第1.2节,作者[美]Jonathan S. Harbour ,李强 译,更多章节内容可以访问云栖社区"异步社区& ...

  7. python游戏编程入门p_Python游戏编程入门 PDF高清完整版

    Python游戏编程入门 PDF高清完整版 作者: 哈伯 (Jonathan S.Harbour) 译者: 李强 出版年: 2015-1-1 页数: 292 装帧: 平装 ISBN: 97871153 ...

  8. python游戏编程入门源代码_python游戏编程入门源代码

    [实例简介] <Python游戏编程入门>的源代码,作者Jonathan S.Harbour 很好的资源,希望对学习python的同学有帮助,代码都可以运行 [实例截图] [核心代码] p ...

  9. pygame 学习笔记(4)推荐一本python入门游戏书籍《PYTHON游戏编程入门》

    简介 <PYTHON游戏编程入门>(More Python Programming for the Absolute Beginner)是 S.Harbour写的一本入门书籍,基于pyga ...

最新文章

  1. oracle connect by用法
  2. ace unlck工具下载_压缩工具:WinRAR 曝出代码执行漏洞,该升级了
  3. VMware支持客户构建多云未来
  4. C++PrimerPlus学习——第十四章编程练习
  5. CCF NOI1072 爬楼梯
  6. jQuery清空div内容
  7. Idea Java代码生成器使用及模板自定义
  8. dedecms分页样式修改 内容页 上一页 下一页
  9. html5车牌效果,车牌自编效果预览软件【京牌】
  10. 读书笔记 | 财务会计理论(第7版 William R.Scott)(中)
  11. 新华社-中国移动联手打造盘古搜索 2月22日上线
  12. 设计圈都这么卷的吗!看霜降海报哪家强
  13. linux嵌入式reboot不生效,Embeded linux之reboot
  14. Ubuntu下编译OpenHarmony
  15. 零基础掌握计算机入门
  16. 阿里一p7员工为了证明自己确实年入百万,晒出了他的工资
  17. SQL Server 配置管理器中Browser灰色无法启动解决办法
  18. Linux一键安装xrdp,如何在Linux系统Ubuntu 20.04中安装xrdp实现远程桌面连接RDP
  19. python strftime 中文 年 月 日
  20. 基于SpringBoot旅游信息管理系统网站

热门文章

  1. 分布式session的问题
  2. 旋转数组(将数组元素循环右移k次)
  3. Python 小数位保留
  4. 20年的沉浮,中国操作系统的前世今生!
  5. Python入门之函数结构——第3关:函数的使用范围:Python作用域
  6. oracle partial write,页断裂(partial write)与doublewrite技术
  7. php 一位数组合并,php数组合并
  8. Mysql 死锁问题
  9. crc16的c语言函数 计算ccitt_CCITT标准CRC-16计算C源代码
  10. 植物大战僵尸java 僵尸_生存僵尸启示录:文字,路径和基本动画