少儿编程分享:手把手教你用Python编写圣诞大挑战(一)
更新时间:2023-07-18 19:00:01 阅读量: 实用文档 文档下载
- 少儿编程价格表推荐度:
- 相关推荐
是时候再来一个小游戏制作啦~
游戏分享:手把手教你用Python 编写
圣诞大挑战(一)
2018.1.6
刚刚过去了圣诞节
是不是还意犹未尽?
不如我们一起来做一个圣诞小游戏吧!
是时候再来一个小游戏制作啦~
游戏介绍
圣诞大挑战中,玩家将亲手控制圣诞码趣把圣诞老人(也就是码趣君)扔的礼物全部接住,来比比谁接的礼物更多吧!
TIPS
码趣君在扔礼物的时候还会扔雪球噢,要是砸到了圣诞码趣的头上,那就直接晕倒
啦!准备工作
1.#!/usr/bin/env python
2.
3.import random,os.path
4.import pygame
5.from pygame.locals import *
6.
7.#测试是否能够读取扩展图片格式
8.if not pygame.image.get_extended():
9.raise SystemExit("Sorry,extended image module required")
10.
11.
12.#游戏参数
13.BOMB_ODDS =60
14.SCREENRECT =Rect(0,0,640,480)
15.SCORE =0
16.
17.#素材路径
18.main_dir =os.path.split(os.path.abspath(__file__))[0]
19.
20.def load_image(file):
21."预读取图片"
22.file =os.path.join(main_dir,'data',file)
是时候再来一个小游戏制作啦~
23.try:
24.surface =pygame.image.load(file)
25.except pygame.error:
26.raise SystemExit('Could not load image "%s"%s'%(file,
pygame.get_error()))
27.return surface.convert()
28.
29.def load_images(*files):
30.imgs =[]
31.for file in files:
32.imgs.append(load_image(file))
33.return imgs
34.
35.
36.class dummysound:
37.def play(self):pass
38.
39.def load_sound(file):
40.if not pygame.mixer:return dummysound()
41.file =os.path.join(main_dir,'data',file)
42.try:
43.sound =pygame.mixer.Sound(file)
44.return sound
45.except pygame.error:
46.print ('Warning,unable to load,%s'%file)
47.return dummysound()
#!/usr/bin/env python 目的是在运行python 脚本的时候告诉操作系统我们要用python 解释器去运行py 脚本。
所以我们在第一句往往会写如下两句中的其中一句:#!/usr/bin/python 或#!/usr/bin/env python。
就是说在没有在执行程序时指出用什么程序运行py 脚本时,系统会去调用python 程序来执行。
是时候再来一个小游戏制作啦~
在这里要注意os.path 模块的应用,这个模块会帮助我们更好地链接上我们使用的素材,不仅能减少报错,也能加快读取的速度。
而在之后的类里我们在之前的游戏中都有使用过,而在这里我们增加了except 和raise 使得当程序出问题时能准确汇报错误的部分,并且当有这一部分时,即使程
序出错也能顺利运行下去。
(未完待续)
正在阅读:
少儿编程分享:手把手教你用Python编写圣诞大挑战(一)07-18
LC-GC-BZ-007砌筑工程标准做法(NXPowerLite) - 图文06-29
我爱祖国手抄报图片:我爱中华02-16
新消防员新训感悟范文模版07-30
毕业论文(液压)04-28
工作作风建设心得体会3篇09-07
河北省保定市高阳中学2015届高三英语上学期第十八次周练试题12-24
总会想起那张照片作文550字07-13
个人工作情况检讨书2020年新09-07
根据单词后缀判断词性01-13
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 手把手
- 少儿
- 编写
- 圣诞
- 挑战
- 编程
- Python
- 分享
- 教师证面试教案示例-初中思想品德教案
- 春甘蓝无公害丰产栽培技术
- 信息检索 期刊检索上机练习 试题及答案
- 深圳经济特区物业管理条例(2020年执行)
- 中国民企死亡全书
- 08年应急预案汇编
- 2021年小学英语单词分类大全
- 军事理论论文【强大版】
- 新疆六年级下学期语文期末模拟检测试卷D卷
- 第二十讲 寡头市场与博弈
- 2021年大学英语新四级考试听力素材及答案
- MATLAB数学实验报告
- 2021初中七年级生物教学计划范文(新版)
- 2006年考研数学三真题与答案
- 黑龙江省鹤岗市高二上学期化学期中考试试卷(I)卷
- 高一年级语文教学反思
- 05 第五章 物质的跨膜运输与信号传递
- 专家谈如何从阅读材料中提炼申论答题要点
- 西方经济学(本)阶段性测验答案(齐全的题目和答案)所有题型都有 期末考试 复习
- 招商手册——品牌文化篇