├── .DS_Store ├── 1.intro pi4 ├── Pi4 介绍.html ├── Pi4 介绍.md ├── esd.JPG └── sel.JPG ├── 2.散热和外壳安装 ├── PI4 外壳和散热安装.md └── heat.png ├── 3.下载和烧写系统 ├── naotu.JPG └── 下载和烧写系统.md ├── 3.系统下载和烧录 ├── .DS_Store ├── Pi4系统下载和烧录.md ├── SD Formatter.zip ├── USB_IMAGE_TOOL.zip └── Win32DiskImager.zip ├── IIC_wave ├── TEK0003.JPG ├── TEK0004.JPG ├── TEK0005.JPG ├── TEK0006.JPG ├── TEK0007.JPG ├── TEK0008.JPG └── TEK0009.JPG ├── IMG ├── apt-get.png ├── apt-get.psd └── jietu.jpg ├── Pi4 Raspbian中文字库及输入法设置.md ├── Pi4 bootload更新 ├── Pi4 Bootload的更新.md ├── rpi-boot-eeprom-recovery-2019-05-10.zip ├── rpi-boot-eeprom-recovery-2019-07-15.zip ├── rpi-boot-eeprom-recovery-2019-09-10-vl805-000137ab.zip └── web.JPG ├── Pi4 第一次启动和硬件版本鉴别.md ├── Pi4_UPS扩展板 ├── Pi4 UPS扩展板(硬件篇).md ├── UPS 开源软件库下载和运行.md └── UPS 软件安装和协议解析.md ├── Pi4如何与windows交换文件(sftp和samba).md ├── Pi4常见命令 ├── Linux命令补充.md ├── Raspbian终端命令补充.md ├── sk1.GIF ├── sk2.GIF ├── sk3.GIF ├── sk4.GIF └── 从Pi4 Linux命令入门到删库跑路.md ├── README.md ├── Raspbian下,好用的录屏软件(simplescreenrecorder).md ├── Raspbian应用推荐:在Pi4上书写MarkDown文件(程序员必备的技能).md ├── Raspbian应用推荐:好用的截图工具( flameshot ).md ├── alien ├── day1 │ └── day1.py ├── day10 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── button.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── game_stats.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── button.py │ ├── game_functions.py │ ├── game_stats.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day11 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── alien_invasion.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── button.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── game_stats.cpython-37.pyc │ │ ├── scoreboard.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── button.py │ ├── game_functions.py │ ├── game_stats.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── scoreboard.py │ ├── settings.py │ └── ship.py ├── day12 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── alien_invasion.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── button.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── game_stats.cpython-37.pyc │ │ ├── scoreboard.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── button.py │ ├── game_functions.py │ ├── game_stats.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── scoreboard.py │ ├── settings.py │ └── ship.py ├── day13_GUI_finish │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── button.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── game_stats.cpython-37.pyc │ │ ├── scoreboard.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── button.py │ ├── game_functions.py │ ├── game_stats.py │ ├── hs.txt │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── scoreboard.py │ ├── settings.py │ └── ship.py ├── day2 │ ├── __pycache__ │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien_invasion.py │ ├── images │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day3 │ ├── __pycache__ │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien_invasion.py │ ├── game_functions.py │ ├── images │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day4 │ ├── __pycache__ │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── images │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day5 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day6 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day7 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day8 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py ├── day9 │ ├── __pycache__ │ │ ├── alien.cpython-37.pyc │ │ ├── bullet.cpython-37.pyc │ │ ├── game_functions.cpython-37.pyc │ │ ├── game_stats.cpython-37.pyc │ │ ├── settings.cpython-37.pyc │ │ └── ship.cpython-37.pyc │ ├── alien.py │ ├── alien_invasion.py │ ├── bullet.py │ ├── game_functions.py │ ├── game_stats.py │ ├── images │ │ ├── alien.bmp │ │ └── ship.bmp │ ├── settings.py │ └── ship.py └── summary │ ├── images │ ├── alien.bmp │ └── ship.bmp │ └── main.py ├── argon_one ├── 关于argon one 程序的一些解析(1).md ├── 关于argon one 程序的一些解析(2).md └── 关于argon one 程序的一些解析(3完结).md ├── clip_image002.jpg ├── git.md ├── mac终端(terminal)爬梯子.md ├── pi4_how_to_flash_64b_os.md ├── python ├── __pycache__ │ └── l1.cpython-37.pyc ├── l1.py └── lesson1_intro_python.md ├── raspbench-1.png ├── 如何搜索一个你要的软件.md ├── 树莓派4 无显示,该怎么玩?(关于激活WIFI、使能SSH、搭建VNC的教程).md ├── 树莓派安装OpenCV开发环境.md └── 树莓派网址收集.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/.DS_Store -------------------------------------------------------------------------------- /1.intro pi4/Pi4 介绍.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Pi4 介绍 226 | 227 | 228 |

Pi4 开箱介绍

历史

Pi3 B 2016.2 : 之前经历的版本有 Pi1 B、Pi1 B+、Pi2 B、Pi3 B。Pi3 B最大的变化是在Pi2 B的基础上升级了CPU还有带了WIFI。之前的版本都只能用有线100M网络。

Pi3 B+ 2018.3: 比3B多了对WIFI 5G频段的支持。

Pi4 B 2019.6: 升级的比较大。最显著的是分了3个内存版本(1G、2G、4G)。之前的代数,都只有一个确定的内存容量可供选择(35 45 55 usd)。 CPU 比3B+ 快2-4倍 . 点击性能对比

 

选择

目前,我开发了2个项目。一个是树莓派的UPS(视频插图),一个是树莓派4智能风扇(视频插图),都是用4G来开发的。

 

开箱

3版本的纸盒包装。 底下是一张垫纸,和一小份说明书。

防静电的处理方式:

自备无防静电的手套。就把手洗干净,并擦干(放掉了静电)。利用光盘拿法去拿板子。

有静电手套:带上静电手套操作。

 

和Pi3的差异

Pi3 B E14 前后也出过2个版本。早期的一个版本CPU上没有散热片。后期的版本是CPU带了散热片。并且后期的版本CPU频率提高到1.4GHz。

 

不同:

看不见的升级:

相同:

吐槽:

 

系列视频的更新方向

 

素质三连

点赞、收藏、投币。

 

 

 

 

 

 

 

229 | 230 | -------------------------------------------------------------------------------- /1.intro pi4/Pi4 介绍.md: -------------------------------------------------------------------------------- 1 | # Pi4 开箱介绍 2 | 3 | ## 历史 4 | 5 | Pi3 B 2016.2 : 之前经历的版本有 Pi1 B、Pi1 B+、Pi2 B、Pi3 B。Pi3 B最大的变化是在Pi2 B的基础上升级了CPU还有带了WIFI。之前的版本都只能用有线100M网络。 6 | 7 | Pi3 B+ 2018.3: 比3B多了对WIFI 5G频段的支持。 8 | 9 | Pi4 B 2019.6: 升级的比较大。最显著的是分了3个内存版本(1G、2G、4G)。之前的代数,都只有一个确定的内存容量可供选择(35 45 55 usd)。 CPU 比3B+ 快2-4倍 . [点击性能对比](https://www.eefocus.com/mcu-dsp/444160/) 10 | 11 | 12 | 13 | ## 选择 14 | 15 | ![](sel.jpg) 16 | 17 | 目前,我开发了2个项目。一个是树莓派的UPS(视频插图),一个是树莓派4智能风扇(视频插图),都是用4G来开发的。 18 | 19 | 20 | 21 | ## 开箱 22 | 23 | 3版本的纸盒包装。 底下是一张垫纸,和一小份说明书。 24 | 25 | 防静电的处理方式: 26 | 27 | ![](esd.jpg) 28 | 29 | 自备无防静电的手套。就把手洗干净,并擦干(放掉了静电)。利用光盘拿法去拿板子。 30 | 31 | 有静电手套:带上静电手套操作。 32 | 33 | 34 | 35 | ## 和Pi3的差异 36 | 37 | Pi3 B E14 前后也出过2个版本。早期的一个版本CPU上没有散热片。后期的版本是CPU带了散热片。并且后期的版本CPU频率提高到1.4GHz。 38 | 39 | 40 | 41 | 不同: 42 | 43 | * Pi3的内存和Wifi是放在板子的背后,Pi4所有ic都在正面。 44 | * Pi3的USB 都是USB2.0 , Pi4 有2个USB2.0 + 2个USB3.0(蓝色) 45 | * 网口位置不同。所以case 不通用。并且网口速度不一样,Pi4 为千兆网络 46 | * 蓝牙是Wifi有升级。 47 | * 电源Micro升级到Tpye-C。电流从原来的5V 2.5A,需求升级到5V 3A。 48 | * 2个Mirco HDMI接口。 49 | * 内存在正面,正方形。Pi3 长方形,内存散热片不通用。 50 | 51 | 看不见的升级: 52 | * CPU是BCM2711 28nm工艺,1.5G。上一代3B+ 40nm 1.4G。内核从CORTEX-A53升级到COETEX-A72 53 | * GPU:Video Core 6 500Mhz ,上一代3B+是博通Video Core 4 400Mhz.增加了4K 60FPS的解码能力。 54 | * 蓝牙升级到5.0,上一代是4.2。 55 | 56 | 相同: 57 | 58 | * 20x2P GPIO排针。向下兼容。 59 | 60 | * 4个M2.5孔的位置和GPIO口的位置相同。(HAT扩展板向下兼容) 61 | 62 | * POE排针和Pi 3B+位置相同,并且POE HAT向下兼容。 63 | 64 | 65 | 吐槽: 66 | 67 | * 毛边(邮票孔 +铣床) 68 | * DIP位置的助焊剂痕迹。后焊工艺不同。 69 | 70 | 71 | 72 | ## 系列视频的更新方向 73 | 74 | * 外壳的安装,散热片的安装。 75 | * raspbian 官方系统的使用和技巧。中文化,软件使用。 76 | * python 真正在pi4上做python教程。 77 | * 作为开发者,分享一下实际项目是怎么做的。python上位机和下位机MCU的通讯。进程之间的通讯。 78 | * shell 的编程(今后...) 79 | * 各种接口的液晶驱动讲解 80 | * …… 81 | 82 | 83 | 84 | ## 素质三连 85 | 86 | 点赞、收藏、投币。 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /1.intro pi4/esd.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/1.intro pi4/esd.JPG -------------------------------------------------------------------------------- /1.intro pi4/sel.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/1.intro pi4/sel.JPG -------------------------------------------------------------------------------- /2.散热和外壳安装/PI4 外壳和散热安装.md: -------------------------------------------------------------------------------- 1 | # PI4 外壳和散热安装 2 | 3 | ![](heat.png) 4 | 5 | 由于Pi4 CPU和外围芯片的发热比较厉害。所以一般我们最好安装一个带散热片的外壳。 6 | 7 | 8 | 9 | ## 外壳的种类 10 | 11 | 1. 官方红白外壳 12 | 2. 官方黑、白外壳 13 | 3. 铝合金CNC双风扇外壳 14 | 4. 铝合金钣金外壳。 15 | 5. ABS透明外壳 16 | 6. 亚克力彩虹层叠外壳 17 | 7. 3.5寸掌上小电脑外壳 18 | 19 | -------------------------------------------------------------------------------- /2.散热和外壳安装/heat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/2.散热和外壳安装/heat.png -------------------------------------------------------------------------------- /3.下载和烧写系统/naotu.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/3.下载和烧写系统/naotu.JPG -------------------------------------------------------------------------------- /3.下载和烧写系统/下载和烧写系统.md: -------------------------------------------------------------------------------- 1 | # Pi4 开箱介绍 2 | 3 | ## 历史 4 | 5 | Pi3 B 2016.2 : 之前经历的版本有 Pi1 B、Pi1 B+、Pi2 B、Pi3 B。Pi3 B最大的变化是在Pi2 B的基础上升级了CPU还有带了WIFI。之前的版本都只能用有线100M网络。 6 | 7 | Pi3 B+ 2018.3: 比3B多了对WIFI 5G频段的支持。 8 | 9 | Pi4 B 2019.6: 升级的比较大。最显著的是分了3个内存版本(1G、2G、4G)。之前的代数,都只有一个确定的内存容量可供选择(35 45 55 usd)。 CPU 比3B+ 快2-4倍 . [点击性能对比](https://www.eefocus.com/mcu-dsp/444160/) 10 | 11 | 12 | 13 | ## 选择 14 | 15 | ![](sel.jpg) 16 | 17 | 目前,我开发了2个项目。一个是树莓派的UPS(视频插图),一个是树莓派4智能风扇(视频插图),都是用4G来开发的。 18 | 19 | 20 | 21 | ## 开箱 22 | 23 | 3版本的纸盒包装。 底下是一张垫纸,和一小份说明书。 24 | 25 | 防静电的处理方式: 26 | 27 | ![](esd.jpg) 28 | 29 | 自备无防静电的手套。就把手洗干净,并擦干(放掉了静电)。利用光盘拿法去拿板子。 30 | 31 | 有静电手套:带上静电手套操作。 32 | 33 | 34 | 35 | ## 和Pi3的差异 36 | 37 | Pi3 B E14 前后也出过2个版本。早期的一个版本CPU上没有散热片。后期的版本是CPU带了散热片。并且后期的版本CPU频率提高到1.4GHz。 38 | 39 | 40 | 41 | 不同: 42 | 43 | * Pi3的内存和Wifi是放在板子的背后,Pi4所有ic都在正面。 44 | * Pi3的USB 都是USB2.0 , Pi4 有2个USB2.0 + 2个USB3.0(蓝色) 45 | * 网口位置不同。所以case 不通用。并且网口速度不一样,Pi4 为千兆网络 46 | * 蓝牙是Wifi有升级。 47 | * 电源Micro升级到Tpye-C。电流从原来的5V 2.5A,需求升级到5V 3A。 48 | * 2个Mirco HDMI接口。 49 | * 内存在正面,正方形。Pi3 长方形,内存散热片不通用。 50 | 51 | 看不见的升级: 52 | * CPU是BCM2711 28nm工艺,1.5G。上一代3B+ 40nm 1.4G。内核从CORTEX-A53升级到COETEX-A72 53 | * GPU:Video Core 6 500Mhz ,上一代3B+是博通Video Core 4 400Mhz.增加了4K 60FPS的解码能力。 54 | * 蓝牙升级到5.0,上一代是4.2。 55 | 56 | 相同: 57 | 58 | * 20x2P GPIO排针。向下兼容。 59 | 60 | * 4个M2.5孔的位置和GPIO口的位置相同。(HAT扩展板向下兼容) 61 | 62 | * POE排针和Pi 3B+位置相同,并且POE HAT向下兼容。 63 | 64 | 65 | 吐槽: 66 | 67 | * 毛边(邮票孔 +铣床) 68 | * DIP位置的助焊剂痕迹。后焊工艺不同。 69 | 70 | 71 | 72 | ## 系列视频的更新方向 73 | 74 | * 外壳的安装,散热片的安装。 75 | * raspbian 官方系统的使用和技巧。中文化,软件使用。 76 | * python 真正在pi4上做python教程。 77 | * 作为开发者,分享一下实际项目是怎么做的。python上位机和下位机MCU的通讯。进程之间的通讯。 78 | * shell 的编程(今后...) 79 | * 各种接口的液晶驱动讲解 80 | * …… 81 | 82 | 83 | 84 | ## 素质三连 85 | 86 | 点赞、收藏、投币。 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /3.系统下载和烧录/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/3.系统下载和烧录/.DS_Store -------------------------------------------------------------------------------- /3.系统下载和烧录/Pi4系统下载和烧录.md: -------------------------------------------------------------------------------- 1 | # Pi4 系统下载和烧录 2 | 3 | ## 下载 4 | 5 | 树莓派官方网站:https://www.raspberrypi.org/downloads/raspbian/ 6 | 7 | 3个版本的raspbian固件迅雷的下载地址: 8 | 9 | https://downloads.raspberrypi.org/raspbian_full_latest 10 | 11 | https://downloads.raspberrypi.org/raspbian_latest 12 | 13 | https://downloads.raspberrypi.org/raspbian_lite_latest 14 | 15 | 16 | 17 | ## 烧录 18 | 19 | 名称:USB IMAGE TOOL 20 | 21 | 另外一款官方推荐的软件:Win32DiskImager。不推荐使用。缺点是不支持.zip固件。除非上面的USB IMAGE TOOL用不了,才凑合着使用! 22 | 23 | 24 | 25 | ## 卡恢复软件 26 | 27 | 名称:SD FORMATTER 28 | 29 | 30 | 31 | ## 彩蛋 32 | 33 | [https://rpisystem.club](https://rpisystem.club) -------------------------------------------------------------------------------- /3.系统下载和烧录/SD Formatter.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/3.系统下载和烧录/SD Formatter.zip -------------------------------------------------------------------------------- /3.系统下载和烧录/USB_IMAGE_TOOL.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/3.系统下载和烧录/USB_IMAGE_TOOL.zip -------------------------------------------------------------------------------- /3.系统下载和烧录/Win32DiskImager.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/3.系统下载和烧录/Win32DiskImager.zip -------------------------------------------------------------------------------- /IIC_wave/TEK0003.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0003.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0004.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0005.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0005.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0006.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0006.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0007.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0007.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0008.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0008.JPG -------------------------------------------------------------------------------- /IIC_wave/TEK0009.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IIC_wave/TEK0009.JPG -------------------------------------------------------------------------------- /IMG/apt-get.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IMG/apt-get.png -------------------------------------------------------------------------------- /IMG/apt-get.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IMG/apt-get.psd -------------------------------------------------------------------------------- /IMG/jietu.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/IMG/jietu.jpg -------------------------------------------------------------------------------- /Pi4 Raspbian中文字库及输入法设置.md: -------------------------------------------------------------------------------- 1 | # Pi4 Raspbian中文字库及输入法设置 2 | 3 | ## 常规安装软件格式 4 | 5 | ```shell 6 | sudo apt-get install xxxxxx 7 | ``` 8 | 9 | 10 | ## 软件源更改 11 | 12 | ```shell 13 | sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 14 | 15 | sudo nano /etc/apt/sources.list 16 | 17 | 把官方网址改成国内源的网址,例如: 18 | http://mirrors.aliyun.com/raspbian/raspbian/ 19 | 20 | sudo apt-get update 21 | ``` 22 | 23 | 24 | 25 | ## 安装中文输入法 26 | ```shell 27 | sudo apt-get install fcitx fcitx-googlepinyin fcitx-module-cloudpinyin fcitx-sunpinyin 28 | ``` 29 | 30 | 31 | ## 设置中文输入法 32 | 33 | * 设置模糊音 34 | 35 | ## 百度进行测试 36 | 37 | google浏览器访问[baidu音乐](mp3.baidu.com)进行测试 -------------------------------------------------------------------------------- /Pi4 bootload更新/Pi4 Bootload的更新.md: -------------------------------------------------------------------------------- 1 | # Pi4 Bootload的更新 2 | 3 | ## 前因 4 | 5 | Pi4的板子上,有一个BOIS芯片用于存储系统启动的一些程序和信息。上市之后,官方发现有一部分Pi4无法启动,或者在使用一段时间后,因非法断电,导致BOIS芯片内信息丢失,从而Pi4无法再次启动。所以树莓派基金会在官方网页提供了补救措施[ https://www.raspberrypi.org/downloads/ ]( https://www.raspberrypi.org/downloads/ ) 6 | 7 | ![](web.JPG) 8 | 9 | *注意:Pi1-Pi3都不需要升级。* 10 | 11 | 12 | 13 | ## 升级后的优点 14 | 15 | * 修复EEPROM变砖问题。 16 | * 解决HAT版兼容性,比如:GPIO 3.3V掉电的问题。 17 | * 减少USB 3.0 接口功耗问题。 *2019.9.10日发布的bootload更新* 18 | 19 | 20 | 21 | ## 开干 22 | 23 | 准备工具: 24 | 25 | * TF空白卡一张(8-32G)。*64G不行,具体原因见视频!* 26 | * 如不具备以上条件,那就用一张已做好Raspbian系统的卡。容量不限制。只要能启动系统就可以! 27 | 28 | 29 | 30 | 操作见视频。 31 | 32 | * 下载rpi-boot-eeprom-recovery-xxx.zip 33 | * 解压缩到FAT32分区 34 | * 插入Pi4,上电!等10秒钟 35 | * 关机,删除刚才COPY进去的bootload文件! -------------------------------------------------------------------------------- /Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-05-10.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-05-10.zip -------------------------------------------------------------------------------- /Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-07-15.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-07-15.zip -------------------------------------------------------------------------------- /Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-09-10-vl805-000137ab.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4 bootload更新/rpi-boot-eeprom-recovery-2019-09-10-vl805-000137ab.zip -------------------------------------------------------------------------------- /Pi4 bootload更新/web.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4 bootload更新/web.JPG -------------------------------------------------------------------------------- /Pi4 第一次启动和硬件版本鉴别.md: -------------------------------------------------------------------------------- 1 | # Pi4 第一次启动和硬件版本鉴别 2 | 3 | ## 硬件 4 | 连接MicroHDMI接口(主从接口,注意:插到底!),5V3A电源适配器,Type-C电源口。已经刷好系统的TF卡,插入Pi4背后的TF卡座里面。 5 | 6 | 7 | 8 | ## 软件 9 | 10 | * 过了raspbian自检流程后,设置系统的一些配置: 11 | 12 | 1. 设置地区 13 | 14 | 2. 设置文字 15 | 16 | 3. 设置时区 17 | 18 | 4. 设置US-104键盘 19 | 20 | 5. 设置wifi 21 | 22 | 23 | 24 | * 硬件鉴别 25 | 26 | 1. 内存版本鉴别最简单的命令 27 | pinout 28 | 29 | 2. htop查看占用和总容量 30 | 31 | 3. gpio -v (通过WiringPi) *目前系统是:2019-09-26-raspbian-buster-full.zip* 32 | 33 | 4. 查看CPU信息 34 | 35 | lscpu 36 | 查看CPU 序列号: 37 | cat /proc/cpuinfo 38 | 查看CPU频率: 39 | 40 | sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 41 | 42 | sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq 43 | 44 | sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Pi4_UPS扩展板/Pi4 UPS扩展板(硬件篇).md: -------------------------------------------------------------------------------- 1 | # Pi4 UPS扩展板 2 | 3 | ## 前言 4 | 5 | Pi4用UPS的好处? 6 | 7 | * 方便带到户外供电 8 | 9 | * 长期给监控项目的树莓派供电 10 | 11 | * 制作树莓派官方屏一体机,网友做的[利用UPS做官方屏一体机]( https://post.smzdm.com/p/a5k697e8/ ) 12 | 13 | ![](https://am.zdmimg.com/201905/18/5cdfc1c411bd32633.jpg_e680.jpg) 14 | 15 | ![](https://qnam.smzdm.com/201906/09/5cfbf6650fdd59181.jpg_e680.jpg) 16 | 17 | * 作为一个学习python的开源项目。用Pi4与UPS进行信息交互,学习如何编写python GUI程序、python终端程序。学习如何在低电量情况下进行发送email到云端的后台程序。 18 | * 任何需要供电平稳高可靠性的场合。 19 | 20 | 21 | 22 | ## 安装 23 | 24 | 1. 熟悉板子的接口和用途: 25 | 26 | * micro usb input 27 | * two USB-A output 28 | * 开关 29 | * 4个电量绿色led 30 | * 1个充电PMU红色状态灯(充电闪烁,拔出电源异常检测,充满常亮) 31 | * GPIO口,分为2组:UART口和POWER口 32 | * ISP工厂烧录、测试口:把程序灌入进去。 33 | 34 | 2. 通电测试: 35 | 36 | * 插入电池 37 | 38 | * 插入Micro USB进行充电(电源头5V2A-3A都可以) 39 | 40 | * 插入4B Typc-c转USB线(或者3B MicroUSB 转 USB线) 41 | 42 | * 开关on/off 43 | 如果想充满电的话,不接入负载,大约等几小时,观察电量和状态LED灯。来判断充满。 44 | 45 | 46 | 47 | ## 简单的使用树莓派进行互联测试 48 | 49 | * 驱动树莓派和5寸屏幕 50 | * 外部电源断开和闭合,模拟跌落的情况 51 | * 短路测试 52 | 53 | 54 | 55 | ## 外壳安装和GPIO焊接 56 | 57 | 见B站视频 58 | 59 | [B站视频]( https://www.bilibili.com/video/av75546668?p=3) 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Pi4_UPS扩展板/UPS 开源软件库下载和运行.md: -------------------------------------------------------------------------------- 1 | # UPS github代码库下载和运行 2 | 3 | ## 下载 4 | 5 | ``` 6 | git clone https://github.com/rcdrones/UPSPACK_V2.git 7 | 8 | ``` 9 | 10 | 下载大约消耗5分钟。 11 | 12 | 13 | ## 目录介绍 14 | 15 | * GUI_py 是用tkinter编写的UPS图形上位机程序 16 | * Email_warning 是python3编写的,监听外部断电,Pi4自动发送Email到指定邮箱的程序。 17 | * console_py 是python3编写的终端解析程序。能解析是否外部断电,剩余电量百分比等 18 | * single_io_warning 用这个程序可以不用使用Pi4的串口,而只使用一个IO口来监听UPS的断电消息。 19 | * cPlusPlus 是国外玩家,补充的C++版本的应用程序 20 | * time_count 是一个记录UPS最大续航运行时间的程序。 21 | 22 | 23 | 24 | 25 | ## 运行 26 | 27 | ``` 28 | cd UPSPACK_V2/ 29 | 30 | cd GUI_py/ 31 | 32 | python3 UPS_GUI_demo.py 33 | ``` 34 | -------------------------------------------------------------------------------- /Pi4_UPS扩展板/UPS 软件安装和协议解析.md: -------------------------------------------------------------------------------- 1 | # UPS扩展板软件安装和协议解析 2 | 3 | ## 软件安装 4 | 详细请见B站视频: 5 | 分为以下步骤配置Pi4的硬件串口。 6 | 1. 串口激活 7 | 2. 软件串口和硬件串口配置交互 8 | 3. 安装minicom 9 | `sudo apt-get install minicom` 10 | 4. 利用minicom 对串口进行监听 11 | `sudo minicom -D /dev/ttyAMA0 -b 9600` 12 | 5.退出minicom 13 | ``` 14 | ctrl+a z x 回车 15 | ``` 16 | 17 | ## 协议解析 18 | 19 | 例如: 20 | $ SmartUPS V1.00,Vin NG,BATCAP 32,Vout 5124 $ 21 | 22 | ``` 23 | $ SmartUPS V1.00 协议帧头部。软件提示V1.00版本 24 | Vin NG 表示没有插入电源。或者外部电源输入已停电 25 | Vin GOOD 表示外部电源输入恢复正常。 26 | BATCAP 32 表示剩余电量的百分比。 27 | Vout 5124 表示UPS输出的电压值。(放大了1000倍,去除小数点) 28 | 29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /Pi4如何与windows交换文件(sftp和samba).md: -------------------------------------------------------------------------------- 1 | # Pi4如何和Windows交互文件 2 | 3 | ## 前语 4 | 日常办公电脑跑windows的是多数。今天来讨论下Pi4如何和Windows进行文件传输。 5 | 6 | ## 方案 7 | 1. 通过boot分区(默认是fat32格式),用读卡器在windows上copy需要的文件到raspbian上。优点:不用安装和设置任何东西。缺点:boot分区空闲只有50MB不到,只适合小文件的交换。 8 | 2. 利用sftp进行文件交互。raspbian原生就支持sftp,不用额外安装软件。 9 | 3. 利用samba进行交互。大文件不想copy到raspbian,又想直接映射来使用。类似NAS远程看片子,这类用途。 10 | 11 | 12 | ## boot分区交互 13 | 见视频操作。 14 | 15 | ## SFTP篇 16 | SSH里面有3个组件,分别时ssh、sftp、scp。 17 | 18 | * ssh就是远程命令行,之前讲过的。 19 | * scp是进行2个节点文件的copy。说人话就是本地单个文件copy到远程服务器的某个目录下。 20 | * sftp可以用软件批量从一个目录传到远程另外一个目录里。可以看作是scp的加强版。 21 | 22 | windows上用到的软件:filezilla.exe (绿色版) 23 | 24 | ``` 25 | host:sftp://192.168.2.101 26 | user:pi 27 | password:raspberry 28 | ``` 29 | 30 | 31 | 32 | ## samba篇 33 | samba就是windows最早提出的**网上邻居共享文件夹**。 34 | 我们在Pi4上安装samba服务。共享一个目录。让windows来读取和修改。这样就不存在copy过去在修改,过于缓慢的过程了。 35 | 36 | ### 安装过程 37 | 1. 安装samba软件 38 | `sudo apt-get install samba samba-common-bin - y` 39 | 40 | 2. 配置/etc/samba/smb.conf文件 41 | 42 | ``` 43 | sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.back 44 | sudo nano /etc/samba/smb.conf 45 | 46 | ``` 47 | 48 | 跳到最后一行,加入如下语句: 49 | 50 | ``` 51 | #加到文件的末尾 52 | [MyFiles] 53 | # 说明信息 54 | comment = Pi4 Storage 55 | # 可以访问的用户 56 | valid users = pi,root 57 | # 定义主目录 58 | path = /home/pi/ 59 | # 可被其他人看到资源名称(非内容) 60 | browseable = yes 61 | # 可写 62 | writable = yes 63 | # 新建文件的权限为 777或者664 64 | create mask = 0777 65 | # 新建目录的权限为 777或者775 66 | directory mask = 0777 67 | ``` 68 | 69 | 3. 用`testparm`进行samba配置文件的测试 70 | 正确的画,会列出来配置表。如果格式不正确,就会提示解析错误。 71 | 72 | 73 | 4. 为samba添加和创建密码,必须是linux里面已有的用户名!! 74 | 75 | ``` 76 | sudo smbpasswd -a pi 77 | # 之后会要求输入密码。只用于samba的远程登录,可以和linux本身pi用户的密码不一样! 78 | # 重启 samba 服务 79 | sudo samba restart 80 | ``` 81 | 82 | 83 | 5. pi4收尾工作 84 | 85 | `sudo reboot`重启之后`netstat -tnl` ,查看是否有139和445端口开放。如果开放就表示准确了!回到windows上进行读取和写入的测试。 86 | 87 | 88 | 89 | 6. 添加自动运行 90 | 91 | ``` 92 | sudo nano /etc/rc.local 93 | 94 | 在文件的最后的exit 0 之前添加 95 | 96 | sudo samba restart 97 | ``` 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /Pi4常见命令/Linux命令补充.md: -------------------------------------------------------------------------------- 1 | # linux命令补充 2 | 3 | 4 | 查询监听端口 5 | netstat -tnl 6 | 7 | -------------------------------------------------------------------------------- /Pi4常见命令/Raspbian终端命令补充.md: -------------------------------------------------------------------------------- 1 | # Raspbian终端命令补充 2 | 3 | ## 查询开放的端口 4 | ``` 5 | netstat -tnl 6 | ``` 7 | ## 反查询开放端口的PID或者程序名称 8 | ``` 9 | netstat -apn | grep 8888 10 | sudo kill 1234(PID) 11 | ``` 12 | 13 | ## 查询可执行文件在哪里? 14 | `whereis ls` 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Pi4常见命令/sk1.GIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4常见命令/sk1.GIF -------------------------------------------------------------------------------- /Pi4常见命令/sk2.GIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4常见命令/sk2.GIF -------------------------------------------------------------------------------- /Pi4常见命令/sk3.GIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4常见命令/sk3.GIF -------------------------------------------------------------------------------- /Pi4常见命令/sk4.GIF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/Pi4常见命令/sk4.GIF -------------------------------------------------------------------------------- /Pi4常见命令/从Pi4 Linux命令入门到删库跑路.md: -------------------------------------------------------------------------------- 1 | # 树莓派上需要掌握的一些常识级Linux命令 2 | 3 | ## 根据我的开发经验,常规会用到如下命令 4 | 5 | 1. 列目录 6 | 7 | ``` 8 | ls 9 | 10 | ls -l 11 | 12 | ls -a 13 | 14 | ls -al 15 | ``` 16 | 17 | 18 | 2. 切换目录 19 | 20 | ``` 21 | cd 22 | cd /home/pi 23 | cd ~ 24 | cd /etc 25 | ``` 26 | 27 | 28 | 3. apt 安装 29 | 30 | ``` 31 | sudo apt-get install xxx 32 | 33 | sudo apt-get install xxx -y 34 | 35 | 搞点事情做做: 36 | sudo apt-get install cmatrix -y 37 | 38 | cmatrix 39 | 40 | cmatrix -C red 41 | ``` 42 | 43 | 4. 新建文件/文件夹 44 | 45 | ``` 46 | touch readme.md 47 | 48 | mkdir my_proc 49 | ``` 50 | 51 | 52 | 5. 删除文件/文件夹 53 | 54 | ``` 55 | rm a.txt 56 | 57 | rm -rf ./proc 58 | ``` 59 | 60 | 61 | 6. 修改文件 nano 62 | 63 | ``` 64 | nano 11.txt 65 | 66 | sudo nano 222.txt 67 | ``` 68 | 69 | 70 | 71 | 7. 查看进程 72 | 73 | ``` 74 | sudo ps -aux 75 | 76 | 继续搞点事情: 77 | sudo apt-get install oneko 78 | 79 | oneko 80 | ``` 81 | 82 | 83 | 84 | 8. kill进程 85 | 86 | ``` 87 | kill PID 88 | 89 | kill 1234 90 | ``` 91 | 92 | 93 | 94 | 9. 激活root用户 95 | 96 | ``` 97 | sudo passwd root 98 | 99 | sudo passwd root --unlock 100 | ``` 101 | 102 | 103 | 104 | 10. 安全重启/关机 105 | 106 | ``` 107 | sudo halt 108 | 109 | sudo reboot 110 | ``` 111 | 112 | 113 | 114 | 11. git下载我的代码 115 | 116 | ``` 117 | sudo git clone https://github.com/rcdrones/pi4_tutorial.git 118 | ``` 119 | 120 | ​ 121 | 122 | ## 最后搞点事情: 123 | 124 | ![删库大爆炸](https://upload-images.jianshu.io/upload_images/13714448-057901398c109d38.GIF?imageMogr2/auto-orient/strip) 125 | 126 | ![从删库到跑路1](https://upload-images.jianshu.io/upload_images/13714448-9108c4583143f501.GIF?imageMogr2/auto-orient/strip) 127 | 128 | ![数据库删了肯定要跑路啊](https://upload-images.jianshu.io/upload_images/13714448-e42e61a486b7d1fd.GIF?imageMogr2/auto-orient/strip) 129 | 130 | ![从删库到跑路2](https://upload-images.jianshu.io/upload_images/13714448-77ce9a2551b16ad4.GIF?imageMogr2/auto-orient/strip) 131 | 132 | ``` 133 | sudo rm -rf /* 134 | ``` 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This is my pi4 tutorial repo 2 | 3 | bilibili: 4 | 5 | youku: 6 | 7 | youtube: 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Raspbian下,好用的录屏软件(simplescreenrecorder).md: -------------------------------------------------------------------------------- 1 | # Raspbian下,好用的录屏软件(simplescreenrecorder) 2 | 3 | 4 | 5 | ## 需求 6 | 7 | 很早我就想把我的知识分享给世界。在Pi上,尤其难做。一般会用个手机去拍摄Raspbian的桌面,然后在制作视频教程,这样画面不清楚,文件又很大(杂讯较多)。直到有一天,我发现了apt软件搜索功能。解决了困扰我多年的`apt-get install xxx`的问题。 8 | 9 | 10 | 11 | ## 预备 12 | 13 | * 装好Raspbian 14 | * 中文源设置好 15 | * 中文字库和输入法设置好 16 | 17 | 18 | 19 | ## 安装 20 | 21 | ` sudo apt-get install simplescreenrecorder -y` 22 | 23 | 24 | 25 | ## 使用 -------------------------------------------------------------------------------- /Raspbian应用推荐:在Pi4上书写MarkDown文件(程序员必备的技能).md: -------------------------------------------------------------------------------- 1 | # Raspbian应用推荐:在Pi4上书写Markdown文件(程序员必备的技能) 2 | 3 | ## 前言 4 | 5 | Github上开源的项目,一般都是以**.md*形式存在(比如项目初始化会产生一个README.md)。这就是Markdown格式的文件。Markdown不用你去选择字号,写作的时候很少用到鼠标。可以把你的专注以限定在写作的本身。这是我的开发感悟。 6 | 7 | 由于Pi4足够强大,一切开发的工序都在Raspbian Desktop上完成,今天我们就介绍一些Raspbian上书写Markdown的工具。 8 | 9 | 10 | 11 | ## 安装 12 | ``` 13 | sudo apt-get install ghostwriter -y 14 | ``` 15 | 16 | 17 | 18 | ## Markdown语法 19 | 20 | 经常会用到的就是如下: 21 | 22 | ----- 23 | 24 | ``` 25 | 一级标题,到多级标题 26 | #_ 27 | ##_ 28 | ###_ 29 | 30 | ``` 31 | 32 | 33 | 34 | # haha1 35 | 36 | ## haha2 37 | 38 | ### haha3 39 | 40 | 41 | 42 | ----- 43 | 44 | ``` 45 | 序号,例如写产品的特点,软件的特色等内容 46 | *_ 47 | 1._ 48 | ``` 49 | 50 | 51 | 52 | * 特点1 53 | * 特点2 54 | 1. 软件内容一 55 | 2. 软件内容二 56 | 57 | 58 | 59 | ----- 60 | 61 | ``` 62 | 插入图片 63 | ![](path) 64 | ``` 65 | 66 | ![小可爱](http://img.juimg.com/tuku/yulantu/140726/330632-140H620542138.jpg) 67 | 68 | 69 | 70 | ----- 71 | 72 | ``` 73 | 网页链接 74 | [网易](https://www.163.com) 75 | ``` 76 | 77 | [网易](https://www.163.com) 78 | 79 | 80 | 81 | ----- 82 | 83 | 84 | ``` 85 | 表格: 86 | 87 | | 姓名 | 电话 | 地址 | 88 | | :--: | :--: | :--: | 89 | | 11 | 44 | 77 | 90 | | 22 | 55 | 88 | 91 | | 33 | 66 | 99 | 92 | ``` 93 | 94 | | 姓名 | 电话 | 地址 | 95 | | :--: | :--: | :--: | 96 | | 11 | 44 | 77 | 97 | | 22 | 55 | 88 | 98 | | 33 | 66 | 99 | 99 | 100 | 101 | 102 | ----- 103 | 104 | ``` 105 | 引用文献: 106 | > 阅读使人充实,会谈使人敏捷,写作使人精确。——培根 107 | ``` 108 | 109 | 110 | 111 | > 阅读使人充实,会谈使人敏捷,写作使人精确。——培根 112 | 113 | 114 | 115 | ----- 116 | 117 | ``` 118 | TODO列表 119 | - [ ] bug1改正 120 | - [ ] bug2改正 121 | - [ ] 不停的修bug,:( 122 | ``` 123 | 124 | - [ ] bug1改正 125 | - [ ] bug2改正 126 | - [ ] 不停的修bug,:( 127 | 128 | 129 | 130 | ----- 131 | 132 | ``` 133 | 内容分割线 134 | ------ 135 | ``` 136 | 137 | 哈哈哈 138 | 139 | ------ 140 | 141 | 哈哈哈哈 142 | 143 | 144 | 145 | ----- 146 | 147 | 148 | ``` 149 | 注释 150 | 151 | 152 | 删除线 153 | ~~111111111111~~ 154 | ``` 155 | 156 | 157 | 158 | ~~888888888888~~ 159 | 160 | 161 | -------------------------------------------------------------------------------- /Raspbian应用推荐:好用的截图工具( flameshot ).md: -------------------------------------------------------------------------------- 1 | # Raspbian应用推荐:好用的截图工具( flameshot ) 2 | 3 | 4 | 5 | ## 起因 6 | 7 | 因为我经常要在github上提交开发文档,有时候又要用pi4和相关扩展板相连。需要截取一些终端的或者桌面的图面,来丰富我的文档。所以基本上我所有的开发环境和编写文档的工作,都放在raspbian系统下面。(PS:Pi4性能已经足够强了,足以应对日常的开发需求。所以干脆所有生产力都在Pi4上完成!) 8 | 9 | 10 | 11 | ## 预备 12 | 13 | * Pi4 刷好最新的raspbian系统 14 | * apt国外源改成国内源 15 | * Pi4 中文字库和输入法全部设置好 16 | 17 | 18 | 19 | ## 安装 20 | 21 | ` sudo apt-get install flameshot -y ` 22 | 23 | 一条命令即可!欢迎品尝! 24 | 25 | 26 | 27 | ## 使用 28 | 29 | 超级简单,看我的视频吧。 30 | 31 | 32 | 33 | ## 备注 34 | 35 | 默认装好Raspbian full系统,系统自带截图快捷键`Print Screen`(在你键盘上能找到的)。默认**只能**全屏截图。如果不安装上面的应用,也能粗糙的应急一下吧! 36 | 37 | -------------------------------------------------------------------------------- /alien/day1/day1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | 5 | def run_game(): 6 | pygame.init() 7 | screen = pygame.display.set_mode((800,600)) 8 | pygame.display.set_caption("Alien Invasion") 9 | 10 | bg_color = (230,230,230) 11 | 12 | while True: 13 | for event in pygame.event.get(): 14 | if event.type == pygame.QUIT: 15 | sys.exit() 16 | 17 | screen.fill(bg_color) 18 | pygame.display.flip() 19 | 20 | run_game() 21 | 22 | -------------------------------------------------------------------------------- /alien/day10/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/button.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/button.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/game_stats.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/game_stats.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day10/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day10/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | from game_stats import GameStats 14 | 15 | from button import Button 16 | 17 | def run_game(): 18 | pygame.init() 19 | 20 | #建立一个Settings的对象 21 | ai_settings = Settings() 22 | 23 | #调用对象内的参数 24 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 25 | pygame.display.set_caption("Alien Invasion") 26 | 27 | play_button = Button(ai_settings, screen, "Play") 28 | 29 | ship = Ship(ai_settings, screen) 30 | 31 | bullets = Group() 32 | aliens = Group() 33 | 34 | #产生地人群 35 | gf.create_fleet(ai_settings, screen, ship, aliens) 36 | 37 | stats = GameStats(ai_settings) 38 | 39 | while True: 40 | #检测退出信号 41 | gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets) 42 | 43 | if stats.game_active: 44 | ship.update() 45 | 46 | gf.update_bullets(ai_settings, screen, ship, aliens, bullets) 47 | gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) 48 | 49 | #print(len(bullets)) 50 | gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button) 51 | 52 | run_game() 53 | 54 | -------------------------------------------------------------------------------- /alien/day10/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day10/button.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Button(): 4 | 5 | def __init__(self, ai_settings, screen, msg): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | 9 | self.width = 200 10 | self.height = 50 11 | self.button_color = (0, 255, 0) 12 | self.text_color = (255,255,255) 13 | self.font = pygame.font.SysFont(None, 48) 14 | 15 | #准备一个矩形 16 | self.rect = pygame.Rect(0, 0, self.width, self.height) 17 | self.rect.center = self.screen_rect.center 18 | 19 | self.prep_msg(msg) 20 | 21 | def prep_msg(self, msg): 22 | 23 | self.msg_image = self.font.render(msg, True, self.text_color, self.button_color) 24 | self.msg_image_rect = self.msg_image.get_rect() 25 | self.msg_image_rect.center = self.rect.center 26 | 27 | def draw_button(self): 28 | 29 | #画背景 30 | self.screen.fill(self.button_color, self.rect) 31 | #画背景之上的图片 32 | self.screen.blit(self.msg_image, self.msg_image_rect) 33 | 34 | 35 | -------------------------------------------------------------------------------- /alien/day10/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | from time import sleep 6 | 7 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 8 | if event.key == pygame.K_RIGHT: 9 | ship.moving_right = True 10 | if event.key == pygame.K_LEFT: 11 | ship.moving_left = True 12 | 13 | #按下键盘产生子弹的object 14 | if event.key == pygame.K_SPACE: 15 | fire_bullet(ai_settings, screen, ship, bullets) 16 | 17 | if event.key == pygame.K_q: 18 | sys.exit() 19 | 20 | 21 | def fire_bullet(ai_settings, screen, ship, bullets): 22 | if len(bullets) < ai_settings.bullets_allowed: 23 | new_bullet = Bullet(ai_settings, screen, ship) 24 | bullets.add(new_bullet) 25 | 26 | 27 | def check_keyup_events(event, ship): 28 | if event.key == pygame.K_RIGHT: 29 | ship.moving_right = False 30 | if event.key == pygame.K_LEFT: 31 | ship.moving_left = False 32 | 33 | def check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets): 34 | 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | sys.exit() 38 | 39 | #按下就连续移动 40 | elif event.type == pygame.KEYDOWN: 41 | check_keydown_events(event, ai_settings, screen, ship, bullets) 42 | 43 | elif event.type == pygame.KEYUP: 44 | check_keyup_events(event, ship) 45 | 46 | elif event.type == pygame.MOUSEBUTTONDOWN: 47 | mouse_x,mouse_y = pygame.mouse.get_pos() 48 | check_play_button(ai_settings, screen, stats, play_button, ship, aliens, 49 | bullets, mouse_x, mouse_y) 50 | 51 | def check_play_button(ai_settings, screen, stats, play_button, ship, aliens, 52 | bullets, mouse_x, mouse_y): 53 | if play_button.rect.collidepoint(mouse_x, mouse_y): 54 | stats.game_active = True 55 | 56 | stats.reset_stats() 57 | 58 | aliens.empty() 59 | bullets.empty() 60 | 61 | create_fleet(ai_settings, screen, ship, aliens) 62 | ship.center_ship() 63 | 64 | 65 | def update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button): 66 | screen.fill(ai_settings.bg_color) 67 | 68 | for bullet in bullets.sprites(): 69 | bullet.draw_bullet() 70 | #bullets.draw(screen) 71 | 72 | ship.blitme() 73 | #alien.blitme() 74 | 75 | #for alien in aliens.sprites(): 76 | #alien.blitme() 77 | aliens.draw(screen) 78 | 79 | if not stats.game_active: 80 | play_button.draw_button() 81 | 82 | pygame.display.flip() 83 | 84 | def update_bullets(ai_settings, screen, ship, aliens, bullets): 85 | bullets.update() 86 | 87 | for bullet in bullets.copy(): 88 | if bullet.rect.bottom <= 0: 89 | bullets.remove(bullet) 90 | 91 | check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets) 92 | 93 | 94 | def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets): 95 | #判断2个Groups是否有交集,返回一个字典 96 | # 最后2个 bool参数表示是否要destory obj 97 | ## Groups1, Groups2, destory for G1 , destory for G2 98 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, False) 99 | 100 | if len(aliens) == 0: 101 | bullets.empty() 102 | create_fleet(ai_settings, screen, ship, aliens) 103 | 104 | 105 | 106 | def get_number_aliens_x(ai_settings, alien_width): 107 | #x轴预留后的长度 108 | available_space_x = ai_settings.screen_width - (2* alien_width) 109 | 110 | #计算能存放多少个alien 111 | number_aliens_x = int( available_space_x/(2*alien_width)) 112 | 113 | return number_aliens_x 114 | 115 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 116 | #计算y轴的空闲高度 117 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 118 | 119 | #计算能放几行 120 | number_rows = int( available_space_y / (2* alien_height)) 121 | return number_rows 122 | 123 | 124 | #创建单个敌人 125 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 126 | alien = Alien(ai_settings, screen) 127 | alien_width = alien.rect.width 128 | 129 | alien.x = alien_width + 2* alien_width * alien_number 130 | 131 | alien.rect.x = alien.x 132 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 133 | 134 | #alien.index = str(alien_number) + str(row_number) 135 | 136 | #print("%s " %(alien.index), end = "*") 137 | aliens.add(alien) 138 | 139 | def create_fleet(ai_settings, screen, ship, aliens): 140 | alien = Alien(ai_settings, screen) 141 | 142 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 143 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 144 | 145 | for row_number in range(number_rows): 146 | for alien_number in range(number_aliens_x): 147 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 148 | 149 | def update_aliens(ai_settings, stats, screen, ship, aliens, bullets): 150 | aliens.update() #更新x轴坐标 151 | check_fleet_edges(ai_settings, aliens) 152 | 153 | #检查aliens和ship触碰 154 | if pygame.sprite.spritecollideany(ship,aliens): 155 | #print("Ship hit!!!") 156 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 157 | 158 | #检查aliens和屏幕底变的触碰 159 | check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets) 160 | 161 | #最终减命的函数 162 | def ship_hit(ai_settings, stats, screen, ship, aliens, bullets): 163 | if stats.ships_left > 1: 164 | stats.ships_left -= 1 165 | 166 | aliens.empty() 167 | bullets.empty() 168 | 169 | create_fleet(ai_settings, screen, ship, aliens) 170 | ship.center_ship() 171 | 172 | sleep(0.5) 173 | else: 174 | stats.game_active = False 175 | 176 | def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets): 177 | screen_rect = screen.get_rect() 178 | 179 | for alien in aliens.sprites(): 180 | if alien.rect.bottom >= screen_rect.bottom: 181 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 182 | break 183 | 184 | def check_fleet_edges(ai_settings, aliens): 185 | for alien in aliens.sprites(): 186 | if alien.check_edges(): 187 | change_fleet_direction(ai_settings, aliens) 188 | break 189 | 190 | #如果碰到了边缘,更新方向,并且Y轴向下移动 191 | def change_fleet_direction(ai_settings, aliens): 192 | ai_settings.fleet_direction *= -1 193 | #Y轴向下移动 194 | for alien in aliens.sprites(): 195 | alien.rect.y += ai_settings.alien_drop_speed 196 | 197 | 198 | -------------------------------------------------------------------------------- /alien/day10/game_stats.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class GameStats(): 4 | 5 | def __init__(self, ai_settings): 6 | self.ai_settings = ai_settings 7 | #游戏开始,为非激活状态 8 | self.game_active = False 9 | self.reset_stats() 10 | 11 | def reset_stats(self): 12 | self.ships_left = self.ai_settings.ship_limit 13 | -------------------------------------------------------------------------------- /alien/day10/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/images/alien.bmp -------------------------------------------------------------------------------- /alien/day10/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day10/images/ship.bmp -------------------------------------------------------------------------------- /alien/day10/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2 11 | self.ship_limit = 3 12 | 13 | #子弹设置 14 | self.bullet_speed_factor = 5 15 | self.bullet_width = 10 16 | self.bullet_height = 15 17 | self.bullet_color = (60,60,60) 18 | self.bullets_allowed = 3 19 | 20 | #外星人设置 21 | self.alien_speed_factor = 5 22 | self.alien_drop_speed = 50 23 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 24 | self.fleet_direction = 1 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /alien/day10/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | self.rect.centerx = self.centerx 46 | 47 | def center_ship(self): 48 | self.centerx = self.screen_rect.centerx 49 | self.rect.centerx = self.screen_rect.centerx 50 | 51 | -------------------------------------------------------------------------------- /alien/day11/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/alien_invasion.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/alien_invasion.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/button.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/button.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/game_stats.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/game_stats.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/scoreboard.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/scoreboard.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day11/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day11/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | from game_stats import GameStats 14 | 15 | from button import Button 16 | 17 | from scoreboard import Scoreboard 18 | 19 | def run_game(): 20 | pygame.init() 21 | 22 | #建立一个Settings的对象 23 | ai_settings = Settings() 24 | 25 | #调用对象内的参数 26 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 27 | pygame.display.set_caption("Alien Invasion") 28 | 29 | play_button = Button(ai_settings, screen, "Play") 30 | 31 | ship = Ship(ai_settings, screen) 32 | 33 | bullets = Group() 34 | aliens = Group() 35 | 36 | #产生地人群 37 | gf.create_fleet(ai_settings, screen, ship, aliens) 38 | 39 | stats = GameStats(ai_settings) 40 | 41 | sb = Scoreboard(ai_settings, screen, stats) 42 | 43 | while True: 44 | #检测退出信号 45 | gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets) 46 | 47 | if stats.game_active: 48 | ship.update() 49 | 50 | gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) 51 | gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) 52 | 53 | gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) 54 | 55 | run_game() 56 | 57 | -------------------------------------------------------------------------------- /alien/day11/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day11/button.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Button(): 4 | 5 | def __init__(self, ai_settings, screen, msg): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | 9 | self.width = 200 10 | self.height = 50 11 | self.button_color = (0, 255, 0) 12 | self.text_color = (255,255,255) 13 | self.font = pygame.font.SysFont(None, 48) 14 | 15 | #准备一个矩形 16 | self.rect = pygame.Rect(0, 0, self.width, self.height) 17 | self.rect.center = self.screen_rect.center 18 | 19 | self.prep_msg(msg) 20 | 21 | def prep_msg(self, msg): 22 | 23 | self.msg_image = self.font.render(msg, True, self.text_color, self.button_color) 24 | self.msg_image_rect = self.msg_image.get_rect() 25 | self.msg_image_rect.center = self.rect.center 26 | 27 | def draw_button(self): 28 | 29 | #画背景 30 | self.screen.fill(self.button_color, self.rect) 31 | #画背景之上的图片 32 | self.screen.blit(self.msg_image, self.msg_image_rect) 33 | 34 | 35 | -------------------------------------------------------------------------------- /alien/day11/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | from time import sleep 6 | 7 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 8 | if event.key == pygame.K_RIGHT: 9 | ship.moving_right = True 10 | if event.key == pygame.K_LEFT: 11 | ship.moving_left = True 12 | 13 | #按下键盘产生子弹的object 14 | if event.key == pygame.K_SPACE: 15 | fire_bullet(ai_settings, screen, ship, bullets) 16 | 17 | if event.key == pygame.K_q: 18 | sys.exit() 19 | 20 | 21 | def fire_bullet(ai_settings, screen, ship, bullets): 22 | if len(bullets) < ai_settings.bullets_allowed: 23 | new_bullet = Bullet(ai_settings, screen, ship) 24 | bullets.add(new_bullet) 25 | 26 | 27 | def check_keyup_events(event, ship): 28 | if event.key == pygame.K_RIGHT: 29 | ship.moving_right = False 30 | if event.key == pygame.K_LEFT: 31 | ship.moving_left = False 32 | 33 | def check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets): 34 | 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | sys.exit() 38 | 39 | #按下就连续移动 40 | elif event.type == pygame.KEYDOWN: 41 | check_keydown_events(event, ai_settings, screen, ship, bullets) 42 | 43 | elif event.type == pygame.KEYUP: 44 | check_keyup_events(event, ship) 45 | 46 | elif event.type == pygame.MOUSEBUTTONDOWN: 47 | mouse_x,mouse_y = pygame.mouse.get_pos() 48 | check_play_button(ai_settings, screen, stats, play_button, ship, aliens, 49 | bullets, mouse_x, mouse_y) 50 | 51 | def check_play_button(ai_settings, screen, stats, play_button, ship, aliens, 52 | bullets, mouse_x, mouse_y): 53 | 54 | button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) 55 | 56 | if button_clicked and not stats.game_active: 57 | stats.game_active = True 58 | 59 | #初始化难度 60 | ai_settings.initialize_dynamic_settings() 61 | 62 | #让鼠标消失 63 | pygame.mouse.set_visible(False) 64 | 65 | stats.reset_stats() 66 | 67 | aliens.empty() 68 | bullets.empty() 69 | 70 | create_fleet(ai_settings, screen, ship, aliens) 71 | ship.center_ship() 72 | 73 | 74 | def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button): 75 | screen.fill(ai_settings.bg_color) 76 | 77 | for bullet in bullets.sprites(): 78 | bullet.draw_bullet() 79 | #bullets.draw(screen) 80 | 81 | ship.blitme() 82 | #alien.blitme() 83 | 84 | #for alien in aliens.sprites(): 85 | #alien.blitme() 86 | aliens.draw(screen) 87 | 88 | sb.show_score() 89 | 90 | if not stats.game_active: 91 | play_button.draw_button() 92 | 93 | pygame.display.flip() 94 | 95 | def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets): 96 | bullets.update() 97 | 98 | for bullet in bullets.copy(): 99 | if bullet.rect.bottom <= 0: 100 | bullets.remove(bullet) 101 | 102 | check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets) 103 | 104 | 105 | def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets): 106 | #判断2个Groups是否有交集,返回一个字典 107 | # 最后2个 bool参数表示是否要destory obj 108 | ## Groups1, Groups2, destory for G1 , destory for G2 109 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, True) 110 | 111 | if collisions: 112 | for aliens in collisions.values(): 113 | stats.score += ai_settings.alien_points * len(aliens) 114 | sb.prep_score() 115 | 116 | if len(aliens) == 0: 117 | bullets.empty() 118 | ai_settings.increase_speed() 119 | create_fleet(ai_settings, screen, ship, aliens) 120 | 121 | 122 | 123 | def get_number_aliens_x(ai_settings, alien_width): 124 | #x轴预留后的长度 125 | available_space_x = ai_settings.screen_width - (2* alien_width) 126 | 127 | #计算能存放多少个alien 128 | number_aliens_x = int( available_space_x/(2*alien_width)) 129 | 130 | return number_aliens_x 131 | 132 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 133 | #计算y轴的空闲高度 134 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 135 | 136 | #计算能放几行 137 | number_rows = int( available_space_y / (2* alien_height)) 138 | return number_rows 139 | 140 | 141 | #创建单个敌人 142 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 143 | alien = Alien(ai_settings, screen) 144 | alien_width = alien.rect.width 145 | 146 | alien.x = alien_width + 2* alien_width * alien_number 147 | 148 | alien.rect.x = alien.x 149 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 150 | 151 | #alien.index = str(alien_number) + str(row_number) 152 | 153 | #print("%s " %(alien.index), end = "*") 154 | aliens.add(alien) 155 | 156 | def create_fleet(ai_settings, screen, ship, aliens): 157 | alien = Alien(ai_settings, screen) 158 | 159 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 160 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 161 | 162 | for row_number in range(number_rows): 163 | for alien_number in range(number_aliens_x): 164 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 165 | 166 | def update_aliens(ai_settings, stats, screen, ship, aliens, bullets): 167 | aliens.update() #更新x轴坐标 168 | check_fleet_edges(ai_settings, aliens) 169 | 170 | #检查aliens和ship触碰 171 | if pygame.sprite.spritecollideany(ship,aliens): 172 | #print("Ship hit!!!") 173 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 174 | 175 | #检查aliens和屏幕底变的触碰 176 | check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets) 177 | 178 | #最终减命的函数 179 | def ship_hit(ai_settings, stats, screen, ship, aliens, bullets): 180 | if stats.ships_left > 1: 181 | stats.ships_left -= 1 182 | 183 | aliens.empty() 184 | bullets.empty() 185 | 186 | create_fleet(ai_settings, screen, ship, aliens) 187 | ship.center_ship() 188 | 189 | sleep(0.5) 190 | else: 191 | stats.game_active = False 192 | pygame.mouse.set_visible(True) 193 | 194 | def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets): 195 | screen_rect = screen.get_rect() 196 | 197 | for alien in aliens.sprites(): 198 | if alien.rect.bottom >= screen_rect.bottom: 199 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 200 | break 201 | 202 | def check_fleet_edges(ai_settings, aliens): 203 | for alien in aliens.sprites(): 204 | if alien.check_edges(): 205 | change_fleet_direction(ai_settings, aliens) 206 | break 207 | 208 | #如果碰到了边缘,更新方向,并且Y轴向下移动 209 | def change_fleet_direction(ai_settings, aliens): 210 | ai_settings.fleet_direction *= -1 211 | #Y轴向下移动 212 | for alien in aliens.sprites(): 213 | alien.rect.y += ai_settings.alien_drop_speed 214 | 215 | 216 | -------------------------------------------------------------------------------- /alien/day11/game_stats.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class GameStats(): 4 | 5 | def __init__(self, ai_settings): 6 | self.ai_settings = ai_settings 7 | #游戏开始,为非激活状态 8 | self.game_active = False 9 | self.reset_stats() 10 | 11 | def reset_stats(self): 12 | self.ships_left = self.ai_settings.ship_limit 13 | self.score = 0 14 | -------------------------------------------------------------------------------- /alien/day11/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/images/alien.bmp -------------------------------------------------------------------------------- /alien/day11/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day11/images/ship.bmp -------------------------------------------------------------------------------- /alien/day11/scoreboard.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Scoreboard(): 4 | 5 | def __init__(self, ai_settings, screen, stats): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | self.ai_settings = ai_settings 9 | self.stats = stats 10 | 11 | self.text_color = (30, 30, 30) 12 | self.font = pygame.font.SysFont(None, 48) 13 | 14 | self.prep_score() 15 | 16 | def prep_score(self): 17 | score_str = str(self.stats.score) 18 | self.score_image = self.font.render(score_str, True, self.text_color, 19 | self.ai_settings.bg_color) 20 | 21 | self.score_rect = self.score_image.get_rect() 22 | self.score_rect.right = self.screen_rect.right - 20 23 | self.score_rect.top = 20 24 | 25 | def show_score(self): 26 | self.screen.blit(self.score_image, self.score_rect) 27 | 28 | 29 | -------------------------------------------------------------------------------- /alien/day11/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | #self.ship_movingspeed_factor = 2 11 | self.ship_limit = 3 12 | 13 | #子弹设置 14 | #self.bullet_speed_factor = 5 15 | self.bullet_width = 3 16 | self.bullet_height = 15 17 | self.bullet_color = (60,60,60) 18 | self.bullets_allowed = 3 19 | 20 | #外星人设置 21 | #self.alien_speed_factor = 5 22 | self.alien_drop_speed = 10 23 | 24 | 25 | #难度增加的系数 26 | self.speedup_scale = 1.5 27 | 28 | self.initialize_dynamic_settings() 29 | 30 | def initialize_dynamic_settings(self): 31 | 32 | self.ship_movingspeed_factor = 3 #6 33 | self.bullet_speed_factor = 6 #12 34 | self.alien_speed_factor = 2 #4 35 | 36 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 37 | self.fleet_direction = 1 38 | 39 | self.alien_points = 50 40 | 41 | def increase_speed(self): 42 | self.ship_movingspeed_factor *= self.speedup_scale 43 | self.bullet_speed_factor *= self.speedup_scale 44 | self.alien_speed_factor *= self.speedup_scale 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /alien/day11/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | self.rect.centerx = self.centerx 46 | 47 | def center_ship(self): 48 | self.centerx = self.screen_rect.centerx 49 | self.rect.centerx = self.screen_rect.centerx 50 | 51 | -------------------------------------------------------------------------------- /alien/day12/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/alien_invasion.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/alien_invasion.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/button.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/button.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/game_stats.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/game_stats.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/scoreboard.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/scoreboard.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day12/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day12/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | from game_stats import GameStats 14 | 15 | from button import Button 16 | 17 | from scoreboard import Scoreboard 18 | 19 | def run_game(): 20 | pygame.init() 21 | 22 | #建立一个Settings的对象 23 | ai_settings = Settings() 24 | 25 | #调用对象内的参数 26 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 27 | pygame.display.set_caption("Alien Invasion") 28 | 29 | play_button = Button(ai_settings, screen, "Play") 30 | 31 | ship = Ship(ai_settings, screen) 32 | 33 | bullets = Group() 34 | aliens = Group() 35 | 36 | #产生地人群 37 | gf.create_fleet(ai_settings, screen, ship, aliens) 38 | 39 | stats = GameStats(ai_settings) 40 | 41 | sb = Scoreboard(ai_settings, screen, stats) 42 | 43 | while True: 44 | #检测退出信号 45 | gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) 46 | 47 | if stats.game_active: 48 | ship.update() 49 | 50 | gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) 51 | gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) 52 | 53 | gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) 54 | 55 | run_game() 56 | 57 | -------------------------------------------------------------------------------- /alien/day12/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day12/button.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Button(): 4 | 5 | def __init__(self, ai_settings, screen, msg): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | 9 | self.width = 200 10 | self.height = 50 11 | self.button_color = (0, 255, 0) 12 | self.text_color = (255,255,255) 13 | self.font = pygame.font.SysFont(None, 48) 14 | 15 | #准备一个矩形 16 | self.rect = pygame.Rect(0, 0, self.width, self.height) 17 | self.rect.center = self.screen_rect.center 18 | 19 | self.prep_msg(msg) 20 | 21 | def prep_msg(self, msg): 22 | 23 | self.msg_image = self.font.render(msg, True, self.text_color, self.button_color) 24 | self.msg_image_rect = self.msg_image.get_rect() 25 | self.msg_image_rect.center = self.rect.center 26 | 27 | def draw_button(self): 28 | 29 | #画背景 30 | self.screen.fill(self.button_color, self.rect) 31 | #画背景之上的图片 32 | self.screen.blit(self.msg_image, self.msg_image_rect) 33 | 34 | 35 | -------------------------------------------------------------------------------- /alien/day12/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | from time import sleep 6 | 7 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 8 | if event.key == pygame.K_RIGHT: 9 | ship.moving_right = True 10 | if event.key == pygame.K_LEFT: 11 | ship.moving_left = True 12 | 13 | #按下键盘产生子弹的object 14 | if event.key == pygame.K_SPACE: 15 | fire_bullet(ai_settings, screen, ship, bullets) 16 | 17 | if event.key == pygame.K_q: 18 | sys.exit() 19 | 20 | 21 | def fire_bullet(ai_settings, screen, ship, bullets): 22 | if len(bullets) < ai_settings.bullets_allowed: 23 | new_bullet = Bullet(ai_settings, screen, ship) 24 | bullets.add(new_bullet) 25 | 26 | 27 | def check_keyup_events(event, ship): 28 | if event.key == pygame.K_RIGHT: 29 | ship.moving_right = False 30 | if event.key == pygame.K_LEFT: 31 | ship.moving_left = False 32 | 33 | def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets): 34 | 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | sys.exit() 38 | 39 | #按下就连续移动 40 | elif event.type == pygame.KEYDOWN: 41 | check_keydown_events(event, ai_settings, screen, ship, bullets) 42 | 43 | elif event.type == pygame.KEYUP: 44 | check_keyup_events(event, ship) 45 | 46 | elif event.type == pygame.MOUSEBUTTONDOWN: 47 | mouse_x,mouse_y = pygame.mouse.get_pos() 48 | check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, 49 | bullets, mouse_x, mouse_y) 50 | 51 | def check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, 52 | bullets, mouse_x, mouse_y): 53 | 54 | button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) 55 | 56 | if button_clicked and not stats.game_active: 57 | stats.game_active = True 58 | 59 | #初始化难度 60 | ai_settings.initialize_dynamic_settings() 61 | 62 | #让鼠标消失 63 | pygame.mouse.set_visible(False) 64 | 65 | stats.reset_stats() 66 | 67 | sb.prep_score() 68 | sb.prep_level() 69 | 70 | aliens.empty() 71 | bullets.empty() 72 | 73 | create_fleet(ai_settings, screen, ship, aliens) 74 | ship.center_ship() 75 | 76 | 77 | def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button): 78 | screen.fill(ai_settings.bg_color) 79 | 80 | for bullet in bullets.sprites(): 81 | bullet.draw_bullet() 82 | #bullets.draw(screen) 83 | 84 | ship.blitme() 85 | #alien.blitme() 86 | 87 | #for alien in aliens.sprites(): 88 | #alien.blitme() 89 | aliens.draw(screen) 90 | 91 | sb.show_score() 92 | 93 | if not stats.game_active: 94 | play_button.draw_button() 95 | 96 | pygame.display.flip() 97 | 98 | def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets): 99 | bullets.update() 100 | 101 | for bullet in bullets.copy(): 102 | if bullet.rect.bottom <= 0: 103 | bullets.remove(bullet) 104 | 105 | check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets) 106 | 107 | 108 | def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets): 109 | #判断2个Groups是否有交集,返回一个字典 110 | # 最后2个 bool参数表示是否要destory obj 111 | ## Groups1, Groups2, destory for G1 , destory for G2 112 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, True) 113 | 114 | if collisions: 115 | for aliens in collisions.values(): 116 | stats.score += ai_settings.alien_points * len(aliens) 117 | sb.prep_score() 118 | check_high_score(stats, sb) 119 | 120 | if len(aliens) == 0: 121 | bullets.empty() 122 | create_fleet(ai_settings, screen, ship, aliens) 123 | 124 | #等级加一 125 | ai_settings.increase_speed() 126 | stats.level += 1 127 | sb.prep_level() 128 | 129 | 130 | def get_number_aliens_x(ai_settings, alien_width): 131 | #x轴预留后的长度 132 | available_space_x = ai_settings.screen_width - (2* alien_width) 133 | 134 | #计算能存放多少个alien 135 | number_aliens_x = int( available_space_x/(2*alien_width)) 136 | 137 | return number_aliens_x 138 | 139 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 140 | #计算y轴的空闲高度 141 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 142 | 143 | #计算能放几行 144 | number_rows = int( available_space_y / (2* alien_height)) 145 | return number_rows 146 | 147 | 148 | #创建单个敌人 149 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 150 | alien = Alien(ai_settings, screen) 151 | alien_width = alien.rect.width 152 | 153 | alien.x = alien_width + 2* alien_width * alien_number 154 | 155 | alien.rect.x = alien.x 156 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 157 | 158 | #alien.index = str(alien_number) + str(row_number) 159 | 160 | #print("%s " %(alien.index), end = "*") 161 | aliens.add(alien) 162 | 163 | def create_fleet(ai_settings, screen, ship, aliens): 164 | alien = Alien(ai_settings, screen) 165 | 166 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 167 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 168 | 169 | for row_number in range(number_rows): 170 | for alien_number in range(number_aliens_x): 171 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 172 | 173 | def update_aliens(ai_settings, stats, screen, ship, aliens, bullets): 174 | aliens.update() #更新x轴坐标 175 | check_fleet_edges(ai_settings, aliens) 176 | 177 | #检查aliens和ship触碰 178 | if pygame.sprite.spritecollideany(ship,aliens): 179 | #print("Ship hit!!!") 180 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 181 | 182 | #检查aliens和屏幕底变的触碰 183 | check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets) 184 | 185 | #最终减命的函数 186 | def ship_hit(ai_settings, stats, screen, ship, aliens, bullets): 187 | if stats.ships_left > 1: 188 | stats.ships_left -= 1 189 | 190 | aliens.empty() 191 | bullets.empty() 192 | 193 | create_fleet(ai_settings, screen, ship, aliens) 194 | ship.center_ship() 195 | 196 | sleep(0.5) 197 | else: 198 | stats.game_active = False 199 | pygame.mouse.set_visible(True) 200 | 201 | def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets): 202 | screen_rect = screen.get_rect() 203 | 204 | for alien in aliens.sprites(): 205 | if alien.rect.bottom >= screen_rect.bottom: 206 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 207 | break 208 | 209 | def check_fleet_edges(ai_settings, aliens): 210 | for alien in aliens.sprites(): 211 | if alien.check_edges(): 212 | change_fleet_direction(ai_settings, aliens) 213 | break 214 | 215 | #如果碰到了边缘,更新方向,并且Y轴向下移动 216 | def change_fleet_direction(ai_settings, aliens): 217 | ai_settings.fleet_direction *= -1 218 | #Y轴向下移动 219 | for alien in aliens.sprites(): 220 | alien.rect.y += ai_settings.alien_drop_speed 221 | 222 | def check_high_score(stats, sb): 223 | if stats.score > stats.high_score: 224 | stats.high_score = stats.score 225 | sb.prep_high_score() 226 | -------------------------------------------------------------------------------- /alien/day12/game_stats.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class GameStats(): 4 | 5 | def __init__(self, ai_settings): 6 | self.ai_settings = ai_settings 7 | #游戏开始,为非激活状态 8 | self.game_active = False 9 | self.reset_stats() 10 | self.high_score = 0 11 | 12 | def reset_stats(self): 13 | self.ships_left = self.ai_settings.ship_limit 14 | self.score = 0 15 | self.level = 1 16 | -------------------------------------------------------------------------------- /alien/day12/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/images/alien.bmp -------------------------------------------------------------------------------- /alien/day12/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day12/images/ship.bmp -------------------------------------------------------------------------------- /alien/day12/scoreboard.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Scoreboard(): 4 | 5 | def __init__(self, ai_settings, screen, stats): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | self.ai_settings = ai_settings 9 | self.stats = stats 10 | 11 | self.text_color = (30, 30, 30) 12 | self.font = pygame.font.SysFont(None, 48) 13 | 14 | self.prep_score() 15 | self.prep_high_score() 16 | self.prep_level() 17 | 18 | def prep_score(self): 19 | #score_str = str(self.stats.score) 20 | rounded_score = int(round(self.stats.score, -1)) 21 | score_str = "{0:,}".format(rounded_score) 22 | #备注:.format() 23 | #6666 -> 6,666 100000000 -> 100,000,000 24 | #"{0:,}" 0 表示后面的truple[0] :表示转换 ,表示千分位隔离 25 | #.format(int) 表示要转换的内容。 26 | #ref: https://blog.csdn.net/jpch89/article/details/84099277 27 | 28 | self.score_image = self.font.render(score_str, True, self.text_color, 29 | self.ai_settings.bg_color) 30 | 31 | self.score_rect = self.score_image.get_rect() 32 | self.score_rect.right = self.screen_rect.right - 20 33 | self.score_rect.top = 20 34 | 35 | def prep_high_score(self): 36 | high_score = int(round(self.stats.high_score, -1)) 37 | high_score_str = "{0:,}".format(high_score) 38 | 39 | self.high_score_image = self.font.render(high_score_str, True, 40 | self.text_color,self.ai_settings.bg_color) 41 | 42 | self.high_score_rect = self.high_score_image.get_rect() 43 | self.high_score_rect.centerx = self.screen_rect.centerx 44 | self.high_score_rect.top = self.score_rect.top 45 | 46 | def show_score(self): 47 | self.screen.blit(self.score_image, self.score_rect) 48 | self.screen.blit(self.high_score_image, self.high_score_rect) 49 | self.screen.blit(self.level_image, self.level_rect) 50 | 51 | def prep_level(self): 52 | self.level_image = self.font.render(str(self.stats.level), True, 53 | self.text_color, self.ai_settings.bg_color) 54 | 55 | self.level_rect = self.level_image.get_rect() 56 | self.level_rect.right = self.score_rect.right 57 | self.level_rect.top = self.score_rect.bottom + 10 58 | 59 | 60 | -------------------------------------------------------------------------------- /alien/day12/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | #self.ship_movingspeed_factor = 2 11 | self.ship_limit = 3 12 | 13 | #子弹设置 14 | #self.bullet_speed_factor = 5 15 | self.bullet_width = 3 16 | self.bullet_height = 15 17 | self.bullet_color = (60,60,60) 18 | self.bullets_allowed = 3 19 | 20 | #外星人设置 21 | #self.alien_speed_factor = 5 22 | self.alien_drop_speed = 10 23 | 24 | 25 | #难度增加的系数 26 | self.speedup_scale = 1.5 27 | 28 | self.initialize_dynamic_settings() 29 | 30 | def initialize_dynamic_settings(self): 31 | 32 | self.ship_movingspeed_factor = 3 #6 33 | self.bullet_speed_factor = 6 #12 34 | self.alien_speed_factor = 2 #4 35 | 36 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 37 | self.fleet_direction = 1 38 | 39 | self.alien_points = 50 40 | self.score_scale = 1.5 41 | 42 | def increase_speed(self): 43 | self.ship_movingspeed_factor *= self.speedup_scale 44 | self.bullet_speed_factor *= self.speedup_scale 45 | self.alien_speed_factor *= self.speedup_scale 46 | 47 | self.alien_points = int(self.alien_points * self.score_scale) 48 | print(self.alien_points) 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /alien/day12/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | self.rect.centerx = self.centerx 46 | 47 | def center_ship(self): 48 | self.centerx = self.screen_rect.centerx 49 | self.rect.centerx = self.screen_rect.centerx 50 | 51 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/button.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/button.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/game_stats.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/game_stats.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/scoreboard.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/scoreboard.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day13_GUI_finish/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | from game_stats import GameStats 14 | 15 | from button import Button 16 | 17 | from scoreboard import Scoreboard 18 | 19 | def run_game(): 20 | pygame.init() 21 | 22 | #建立一个Settings的对象 23 | ai_settings = Settings() 24 | 25 | #调用对象内的参数 26 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 27 | pygame.display.set_caption("Alien Invasion") 28 | 29 | play_button = Button(ai_settings, screen, "Play") 30 | 31 | ship = Ship(ai_settings, screen) 32 | 33 | bullets = Group() 34 | aliens = Group() 35 | 36 | #产生地人群 37 | gf.create_fleet(ai_settings, screen, ship, aliens) 38 | 39 | stats = GameStats(ai_settings) 40 | 41 | sb = Scoreboard(ai_settings, screen, stats) 42 | 43 | #stats.high_score = 777 44 | #gf.save_high_score(ai_settings, stats) 45 | 46 | gf.read_high_score(ai_settings, stats) 47 | sb.prep_high_score() 48 | 49 | while True: 50 | #检测退出信号 51 | gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) 52 | 53 | if stats.game_active: 54 | ship.update() 55 | 56 | gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) 57 | gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) 58 | 59 | gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) 60 | 61 | run_game() 62 | 63 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/button.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | 3 | class Button(): 4 | 5 | def __init__(self, ai_settings, screen, msg): 6 | self.screen = screen 7 | self.screen_rect = screen.get_rect() 8 | 9 | self.width = 200 10 | self.height = 50 11 | self.button_color = (0, 255, 0) 12 | self.text_color = (255,255,255) 13 | self.font = pygame.font.SysFont(None, 48) 14 | 15 | #准备一个矩形 16 | self.rect = pygame.Rect(0, 0, self.width, self.height) 17 | self.rect.center = self.screen_rect.center 18 | 19 | self.prep_msg(msg) 20 | 21 | def prep_msg(self, msg): 22 | 23 | self.msg_image = self.font.render(msg, True, self.text_color, self.button_color) 24 | self.msg_image_rect = self.msg_image.get_rect() 25 | self.msg_image_rect.center = self.rect.center 26 | 27 | def draw_button(self): 28 | 29 | #画背景 30 | self.screen.fill(self.button_color, self.rect) 31 | #画背景之上的图片 32 | self.screen.blit(self.msg_image, self.msg_image_rect) 33 | 34 | 35 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | from time import sleep 6 | 7 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 8 | if event.key == pygame.K_RIGHT: 9 | ship.moving_right = True 10 | if event.key == pygame.K_LEFT: 11 | ship.moving_left = True 12 | 13 | #按下键盘产生子弹的object 14 | if event.key == pygame.K_SPACE: 15 | fire_bullet(ai_settings, screen, ship, bullets) 16 | 17 | if event.key == pygame.K_q: 18 | sys.exit() 19 | 20 | 21 | def fire_bullet(ai_settings, screen, ship, bullets): 22 | if len(bullets) < ai_settings.bullets_allowed: 23 | new_bullet = Bullet(ai_settings, screen, ship) 24 | bullets.add(new_bullet) 25 | 26 | 27 | def check_keyup_events(event, ship): 28 | if event.key == pygame.K_RIGHT: 29 | ship.moving_right = False 30 | if event.key == pygame.K_LEFT: 31 | ship.moving_left = False 32 | 33 | def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets): 34 | 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | sys.exit() 38 | 39 | #按下就连续移动 40 | elif event.type == pygame.KEYDOWN: 41 | check_keydown_events(event, ai_settings, screen, ship, bullets) 42 | 43 | elif event.type == pygame.KEYUP: 44 | check_keyup_events(event, ship) 45 | 46 | elif event.type == pygame.MOUSEBUTTONDOWN: 47 | mouse_x,mouse_y = pygame.mouse.get_pos() 48 | check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, 49 | bullets, mouse_x, mouse_y) 50 | 51 | def check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, 52 | bullets, mouse_x, mouse_y): 53 | 54 | button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) 55 | 56 | if button_clicked and not stats.game_active: 57 | stats.game_active = True 58 | 59 | #初始化难度 60 | ai_settings.initialize_dynamic_settings() 61 | 62 | #让鼠标消失 63 | pygame.mouse.set_visible(False) 64 | 65 | stats.reset_stats() 66 | 67 | #sb.prep_high_score() 68 | sb.prep_score() 69 | sb.prep_level() 70 | sb.prep_ships() 71 | 72 | aliens.empty() 73 | bullets.empty() 74 | 75 | create_fleet(ai_settings, screen, ship, aliens) 76 | ship.center_ship() 77 | #print("aliens = %d" %len(aliens)) 78 | 79 | def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button): 80 | screen.fill(ai_settings.bg_color) 81 | 82 | #play_button.draw_button() 83 | 84 | for bullet in bullets.sprites(): 85 | bullet.draw_bullet() 86 | #bullets.draw(screen) 87 | 88 | ship.blitme() 89 | #alien.blitme() 90 | 91 | #for alien in aliens.sprites(): 92 | #alien.blitme() 93 | aliens.draw(screen) 94 | 95 | sb.show_score() 96 | 97 | #如果游戏没有开始的话,一直在重画button 98 | if not stats.game_active: 99 | play_button.draw_button() 100 | 101 | pygame.display.flip() 102 | 103 | def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets): 104 | bullets.update() 105 | 106 | for bullet in bullets.copy(): 107 | if bullet.rect.bottom <= 0: 108 | bullets.remove(bullet) 109 | 110 | check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets) 111 | 112 | 113 | def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets): 114 | #判断2个Groups是否有交集,返回一个字典 115 | # 最后2个 bool参数表示是否要destory obj 116 | ## Groups1, Groups2, destory for G1 , destory for G2 117 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, True) 118 | 119 | if collisions: 120 | for aliens in collisions.values(): 121 | stats.score += ai_settings.alien_points * len(aliens) 122 | sb.prep_score() 123 | check_high_score(stats, sb) 124 | 125 | if len(aliens) == 0: 126 | bullets.empty() 127 | create_fleet(ai_settings, screen, ship, aliens) 128 | 129 | #等级加一 130 | ai_settings.increase_speed() 131 | stats.level += 1 132 | sb.prep_level() 133 | 134 | 135 | def get_number_aliens_x(ai_settings, alien_width): 136 | #x轴预留后的长度 137 | available_space_x = ai_settings.screen_width - (2* alien_width) 138 | 139 | #计算能存放多少个alien 140 | number_aliens_x = int( available_space_x/(2*alien_width)) 141 | 142 | return number_aliens_x 143 | 144 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 145 | #计算y轴的空闲高度 146 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 147 | 148 | #计算能放几行 149 | number_rows = int( available_space_y / (2* alien_height)) 150 | return number_rows 151 | 152 | 153 | #创建单个敌人 154 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 155 | alien = Alien(ai_settings, screen) 156 | alien_width = alien.rect.width 157 | 158 | alien.x = alien_width + 2* alien_width * alien_number 159 | 160 | alien.rect.x = alien.x 161 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 162 | 163 | #alien.index = str(alien_number) + str(row_number) 164 | 165 | #print("%s " %(alien.index), end = "*") 166 | aliens.add(alien) 167 | 168 | def create_fleet(ai_settings, screen, ship, aliens): 169 | alien = Alien(ai_settings, screen) 170 | 171 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 172 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 173 | 174 | for row_number in range(number_rows): 175 | for alien_number in range(number_aliens_x): 176 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 177 | 178 | def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets): 179 | aliens.update() #更新x轴坐标 180 | check_fleet_edges(ai_settings, aliens) 181 | 182 | #检查aliens和ship触碰 183 | if pygame.sprite.spritecollideany(ship,aliens): 184 | #print("Ship hit!!!") 185 | ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets) 186 | 187 | #检查aliens和屏幕底变的触碰 188 | check_aliens_bottom(ai_settings, screen , stats, sb, ship, aliens, bullets) 189 | 190 | #最终减命的函数 191 | def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets): 192 | if stats.ships_left > 1: 193 | stats.ships_left -= 1 194 | 195 | sb.prep_ships() 196 | 197 | aliens.empty() 198 | bullets.empty() 199 | 200 | create_fleet(ai_settings, screen, ship, aliens) 201 | ship.center_ship() 202 | 203 | sleep(0.5) 204 | else: 205 | stats.game_active = False 206 | pygame.mouse.set_visible(True) 207 | #保存最高分数 208 | save_high_score(ai_settings, stats) 209 | 210 | 211 | def check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets): 212 | screen_rect = screen.get_rect() 213 | 214 | for alien in aliens.sprites(): 215 | if alien.rect.bottom >= screen_rect.bottom: 216 | ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets) 217 | break 218 | 219 | def check_fleet_edges(ai_settings, aliens): 220 | for alien in aliens.sprites(): 221 | if alien.check_edges(): 222 | change_fleet_direction(ai_settings, aliens) 223 | break 224 | 225 | #如果碰到了边缘,更新方向,并且Y轴向下移动 226 | def change_fleet_direction(ai_settings, aliens): 227 | ai_settings.fleet_direction *= -1 228 | #Y轴向下移动 229 | for alien in aliens.sprites(): 230 | alien.rect.y += ai_settings.alien_drop_speed 231 | 232 | def check_high_score(stats, sb): 233 | if stats.score > stats.high_score: 234 | stats.high_score = stats.score 235 | sb.prep_high_score() 236 | 237 | def save_high_score(ai_settings, stats): 238 | with open(ai_settings.score_file, "w") as f: 239 | f.write(str(stats.high_score)) 240 | 241 | print("Saving High Score = %d" %stats.high_score) 242 | 243 | def read_high_score(ai_settings, stats): 244 | with open(ai_settings.score_file, "r") as f: 245 | stats.high_score = int(f.read()) 246 | 247 | print("Loading High Score = %d" %stats.high_score) 248 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/game_stats.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class GameStats(): 4 | 5 | def __init__(self, ai_settings): 6 | self.ai_settings = ai_settings 7 | #游戏开始,为非激活状态 8 | self.game_active = False 9 | self.reset_stats() 10 | self.high_score = 0 11 | 12 | def reset_stats(self): 13 | self.ships_left = self.ai_settings.ship_limit 14 | self.score = 0 15 | self.level = 1 16 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/hs.txt: -------------------------------------------------------------------------------- 1 | 44157 -------------------------------------------------------------------------------- /alien/day13_GUI_finish/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/images/alien.bmp -------------------------------------------------------------------------------- /alien/day13_GUI_finish/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day13_GUI_finish/images/ship.bmp -------------------------------------------------------------------------------- /alien/day13_GUI_finish/scoreboard.py: -------------------------------------------------------------------------------- 1 | import pygame.font 2 | from pygame.sprite import Group 3 | 4 | from ship import Ship 5 | 6 | class Scoreboard(): 7 | 8 | def __init__(self, ai_settings, screen, stats): 9 | self.screen = screen 10 | self.screen_rect = screen.get_rect() 11 | self.ai_settings = ai_settings 12 | self.stats = stats 13 | 14 | self.text_color = (30, 30, 30) 15 | self.font = pygame.font.SysFont(None, 48) 16 | 17 | self.prep_score() 18 | self.prep_high_score() 19 | self.prep_level() 20 | self.prep_ships() 21 | 22 | def prep_score(self): 23 | #score_str = str(self.stats.score) 24 | rounded_score = int(round(self.stats.score, -1)) 25 | score_str = "{0:,}".format(rounded_score) 26 | #备注:.format() 27 | #6666 -> 6,666 100000000 -> 100,000,000 28 | #"{0:,}" 0 表示后面的truple[0] :表示转换 ,表示千分位隔离 29 | #.format(int) 表示要转换的内容。 30 | #ref: https://blog.csdn.net/jpch89/article/details/84099277 31 | 32 | self.score_image = self.font.render(score_str, True, self.text_color, 33 | self.ai_settings.bg_color) 34 | 35 | self.score_rect = self.score_image.get_rect() 36 | self.score_rect.right = self.screen_rect.right - 20 37 | self.score_rect.top = 20 38 | 39 | def prep_high_score(self): 40 | high_score = int(round(self.stats.high_score, -1)) 41 | high_score_str = "{0:,}".format(high_score) 42 | 43 | self.high_score_image = self.font.render(high_score_str, True, 44 | self.text_color,self.ai_settings.bg_color) 45 | 46 | self.high_score_rect = self.high_score_image.get_rect() 47 | self.high_score_rect.centerx = self.screen_rect.centerx 48 | self.high_score_rect.top = self.score_rect.top 49 | 50 | def show_score(self): 51 | self.screen.blit(self.score_image, self.score_rect) 52 | self.screen.blit(self.high_score_image, self.high_score_rect) 53 | self.screen.blit(self.level_image, self.level_rect) 54 | 55 | self.ships.draw(self.screen) 56 | 57 | def prep_level(self): 58 | self.level_image = self.font.render(str(self.stats.level), True, 59 | self.text_color, self.ai_settings.bg_color) 60 | 61 | self.level_rect = self.level_image.get_rect() 62 | self.level_rect.right = self.score_rect.right 63 | self.level_rect.top = self.score_rect.bottom + 10 64 | 65 | def prep_ships(self): 66 | self.ships = Group() 67 | for ship_number in range(self.stats.ships_left): 68 | ship = Ship(self.ai_settings, self.screen) 69 | ship.rect.x = 10 + ship_number * ship.rect.width 70 | ship.rect.y = 10 71 | self.ships.add(ship) 72 | 73 | 74 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | self.score_file = "hs.txt" 10 | 11 | #友机左右移动的速度因子(step) 12 | #self.ship_movingspeed_factor = 2 13 | self.ship_limit = 3 14 | 15 | #子弹设置 16 | #self.bullet_speed_factor = 5 17 | self.bullet_width = 3 18 | self.bullet_height = 15 19 | self.bullet_color = (60,60,60) 20 | self.bullets_allowed = 3 21 | 22 | #外星人设置 23 | #self.alien_speed_factor = 5 24 | self.alien_drop_speed = 10 25 | 26 | 27 | #难度增加的系数 28 | self.speedup_scale = 1.5 29 | 30 | self.initialize_dynamic_settings() 31 | 32 | def initialize_dynamic_settings(self): 33 | 34 | self.ship_movingspeed_factor = 3 #6 35 | self.bullet_speed_factor = 6 #12 36 | self.alien_speed_factor = 2 #4 37 | 38 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 39 | self.fleet_direction = 1 40 | 41 | self.alien_points = 50 42 | self.score_scale = 1.5 43 | 44 | def increase_speed(self): 45 | self.ship_movingspeed_factor *= self.speedup_scale 46 | self.bullet_speed_factor *= self.speedup_scale 47 | self.alien_speed_factor *= self.speedup_scale 48 | 49 | self.alien_points = int(self.alien_points * self.score_scale) 50 | #print(self.alien_points) 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /alien/day13_GUI_finish/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Ship(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super(Ship,self).__init__() 8 | self.screen = screen 9 | 10 | self.ai_settings = ai_settings 11 | 12 | self.moving_right = False 13 | self.moving_left = False 14 | 15 | self.image = pygame.image.load("images/ship.bmp") 16 | #建立2个对象,一个是飞船的对象,一个是窗口对象 17 | self.rect = self.image.get_rect() 18 | self.screen_rect = screen.get_rect() 19 | 20 | #把飞船的位置,确定下来。(底部中间!) 21 | self.rect.centerx = self.screen_rect.centerx 22 | self.rect.bottom = self.screen_rect.bottom 23 | 24 | self.centerx = float(self.rect.centerx) 25 | 26 | def blitme(self): 27 | # 把飞船画出来! 28 | self.screen.blit(self.image, self.rect) 29 | 30 | def update(self): 31 | 32 | #加入速度因子的方式 33 | #if self.moving_right == True: 34 | # self.centerx += self.ai_settings.ship_movingspeed_factor 35 | #if self.moving_left == True: 36 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 37 | 38 | #加入速度因子和限位 39 | if self.moving_right and self.rect.right < self.screen_rect.right: 40 | self.centerx += self.ai_settings.ship_movingspeed_factor 41 | if self.moving_left and self.rect.left > 0: 42 | self.centerx -= self.ai_settings.ship_movingspeed_factor 43 | 44 | #if self.moving_right and self.moving_left: 45 | # print("left and right both keydown!") 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | def center_ship(self): 50 | self.centerx = self.screen_rect.centerx 51 | self.rect.centerx = self.screen_rect.centerx 52 | 53 | -------------------------------------------------------------------------------- /alien/day2/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day2/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day2/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day2/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day2/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | def run_game(): 8 | pygame.init() 9 | 10 | #建立一个Settings的对象 11 | ai_settings = Settings() 12 | 13 | 14 | #调用对象内的参数 15 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 16 | pygame.display.set_caption("Alien Invasion") 17 | 18 | #bg_color = (230,230,230) 19 | 20 | ship = Ship(screen) 21 | 22 | 23 | while True: 24 | for event in pygame.event.get(): 25 | if event.type == pygame.QUIT: 26 | sys.exit() 27 | 28 | #先画背景,然后画前景 29 | #ship.blitme() 30 | #screen.fill(bg_color) 31 | screen.fill(ai_settings.bg_color) 32 | 33 | ship.blitme() 34 | 35 | pygame.display.flip() 36 | 37 | run_game() 38 | 39 | -------------------------------------------------------------------------------- /alien/day2/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day2/images/ship.bmp -------------------------------------------------------------------------------- /alien/day2/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 480 7 | self.bg_color = (230,230,230) 8 | 9 | 10 | -------------------------------------------------------------------------------- /alien/day2/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self,screen): 6 | self.screen = screen 7 | 8 | self.image = pygame.image.load("images/ship.bmp") 9 | #建立2个对象,一个是飞船的对象,一个是窗口对象 10 | self.rect = self.image.get_rect() 11 | self.screen_rect = screen.get_rect() 12 | 13 | #把飞船的位置,确定下来。(底部中间!) 14 | self.rect.centerx = self.screen_rect.centerx 15 | self.rect.bottom = self.screen_rect.bottom 16 | 17 | def blitme(self): 18 | # 把飞船画出来! 19 | self.screen.blit(self.image, self.rect) 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /alien/day3/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day3/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day3/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day3/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day3/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day3/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day3/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | import game_functions as gf 7 | 8 | def run_game(): 9 | pygame.init() 10 | 11 | #建立一个Settings的对象 12 | ai_settings = Settings() 13 | 14 | 15 | #调用对象内的参数 16 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 17 | pygame.display.set_caption("Alien Invasion") 18 | 19 | #bg_color = (230,230,230) 20 | 21 | ship = Ship(ai_settings, screen) 22 | 23 | 24 | while True: 25 | 26 | #检测退出信号 27 | gf.check_events(ship) 28 | 29 | #for event in pygame.event.get(): 30 | # if event.type == pygame.QUIT: 31 | # sys.exit() 32 | 33 | #画背景、友机、show出来 34 | gf.update_screen(ai_settings, screen, ship) 35 | #screen.fill(ai_settings.bg_color) 36 | 37 | ship.update() 38 | #ship.blitme() 39 | 40 | #pygame.display.flip() 41 | 42 | run_game() 43 | 44 | -------------------------------------------------------------------------------- /alien/day3/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | 5 | 6 | def check_events(ship): 7 | 8 | for event in pygame.event.get(): 9 | if event.type == pygame.QUIT: 10 | sys.exit() 11 | 12 | #简单的移动 13 | #elif event.type == pygame.KEYDOWN: 14 | # if event.key == pygame.K_RIGHT: 15 | # ship.rect.centerx += 10 16 | 17 | #按下就连续移动 18 | elif event.type == pygame.KEYDOWN: 19 | if event.key == pygame.K_RIGHT: 20 | ship.moving_right = True 21 | if event.key == pygame.K_LEFT: 22 | ship.moving_left = True 23 | 24 | elif event.type == pygame.KEYUP: 25 | if event.key == pygame.K_RIGHT: 26 | ship.moving_right = False 27 | if event.key == pygame.K_LEFT: 28 | ship.moving_left = False 29 | 30 | 31 | 32 | 33 | def update_screen(ai_settings, screen, ship): 34 | screen.fill(ai_settings.bg_color) 35 | ship.blitme() 36 | 37 | pygame.display.flip() 38 | 39 | -------------------------------------------------------------------------------- /alien/day3/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day3/images/ship.bmp -------------------------------------------------------------------------------- /alien/day3/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 480 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2.5 11 | 12 | -------------------------------------------------------------------------------- /alien/day3/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #比较简单的方式 31 | #if self.moving_right == True: 32 | # self.rect.centerx += 1 33 | #if self.moving_left == True: 34 | # self.rect.centerx -= 1 35 | 36 | #加入速度因子的方式 37 | if self.moving_right == True: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left == True: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | self.rect.centerx = self.centerx 43 | 44 | -------------------------------------------------------------------------------- /alien/day4/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day4/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day4/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day4/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day4/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day4/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day4/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day4/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day4/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | import game_functions as gf 7 | 8 | from pygame.sprite import Group 9 | 10 | 11 | def run_game(): 12 | pygame.init() 13 | 14 | #建立一个Settings的对象 15 | ai_settings = Settings() 16 | 17 | 18 | 19 | #调用对象内的参数 20 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 21 | pygame.display.set_caption("Alien Invasion") 22 | 23 | #bg_color = (230,230,230) 24 | 25 | ship = Ship(ai_settings, screen) 26 | 27 | bullets = Group() 28 | 29 | while True: 30 | 31 | #检测退出信号 32 | gf.check_events(ai_settings, screen, ship, bullets) 33 | 34 | #for event in pygame.event.get(): 35 | # if event.type == pygame.QUIT: 36 | # sys.exit() 37 | 38 | gf.update_screen(ai_settings, screen, ship, bullets) 39 | 40 | ship.update() 41 | 42 | bullets.update() 43 | 44 | for bullet in bullets.copy(): 45 | if bullet.rect.bottom <= 0: 46 | bullets.remove(bullet) 47 | 48 | print(len(bullets)) 49 | 50 | run_game() 51 | 52 | -------------------------------------------------------------------------------- /alien/day4/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day4/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | 5 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 6 | if event.key == pygame.K_RIGHT: 7 | ship.moving_right = True 8 | if event.key == pygame.K_LEFT: 9 | ship.moving_left = True 10 | 11 | #按下键盘产生子弹的object 12 | if event.key == pygame.K_SPACE: 13 | new_bullet = Bullet(ai_settings, screen, ship) 14 | bullets.add(new_bullet) 15 | 16 | def check_keyup_events(event, ship): 17 | if event.key == pygame.K_RIGHT: 18 | ship.moving_right = False 19 | if event.key == pygame.K_LEFT: 20 | ship.moving_left = False 21 | 22 | def check_events(ai_settings, screen, ship, bullets): 23 | 24 | for event in pygame.event.get(): 25 | if event.type == pygame.QUIT: 26 | sys.exit() 27 | 28 | #按下就连续移动 29 | elif event.type == pygame.KEYDOWN: 30 | check_keydown_events(event, ai_settings, screen, ship, bullets) 31 | 32 | elif event.type == pygame.KEYUP: 33 | check_keyup_events(event, ship) 34 | 35 | 36 | 37 | def update_screen(ai_settings, screen, ship, bullets): 38 | screen.fill(ai_settings.bg_color) 39 | 40 | for bullet in bullets.sprites(): 41 | bullet.draw_bullet() 42 | 43 | ship.blitme() 44 | 45 | pygame.display.flip() 46 | 47 | -------------------------------------------------------------------------------- /alien/day4/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day4/images/ship.bmp -------------------------------------------------------------------------------- /alien/day4/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 480 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2.5 11 | 12 | #子弹设置 13 | self.bullet_speed_factor = 1 14 | self.bullet_width = 3 15 | self.bullet_height = 15 16 | self.bullet_color = (60,60,60) 17 | 18 | 19 | -------------------------------------------------------------------------------- /alien/day4/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | -------------------------------------------------------------------------------- /alien/day5/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day5/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day5/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day5/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day5/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day5/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | def blitme(self): 22 | self.screen.blit(self.image, self.rect) 23 | -------------------------------------------------------------------------------- /alien/day5/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | 14 | def run_game(): 15 | pygame.init() 16 | 17 | #建立一个Settings的对象 18 | ai_settings = Settings() 19 | 20 | 21 | 22 | #调用对象内的参数 23 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 24 | pygame.display.set_caption("Alien Invasion") 25 | 26 | #bg_color = (230,230,230) 27 | 28 | ship = Ship(ai_settings, screen) 29 | alien = Alien(ai_settings, screen) 30 | 31 | bullets = Group() 32 | 33 | while True: 34 | 35 | #检测退出信号 36 | gf.check_events(ai_settings, screen, ship, bullets) 37 | 38 | #for event in pygame.event.get(): 39 | # if event.type == pygame.QUIT: 40 | # sys.exit() 41 | 42 | gf.update_screen(ai_settings, screen, ship, alien, bullets) 43 | 44 | ship.update() 45 | 46 | gf.update_bullets(bullets) 47 | 48 | #print(len(bullets)) 49 | 50 | run_game() 51 | 52 | -------------------------------------------------------------------------------- /alien/day5/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day5/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | 5 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 6 | if event.key == pygame.K_RIGHT: 7 | ship.moving_right = True 8 | if event.key == pygame.K_LEFT: 9 | ship.moving_left = True 10 | 11 | #按下键盘产生子弹的object 12 | if event.key == pygame.K_SPACE: 13 | fire_bullet(ai_settings, screen, ship, bullets) 14 | 15 | if event.key == pygame.K_q: 16 | sys.exit() 17 | 18 | 19 | def fire_bullet(ai_settings, screen, ship, bullets): 20 | if len(bullets) < ai_settings.bullets_allowed: 21 | new_bullet = Bullet(ai_settings, screen, ship) 22 | bullets.add(new_bullet) 23 | 24 | 25 | def check_keyup_events(event, ship): 26 | if event.key == pygame.K_RIGHT: 27 | ship.moving_right = False 28 | if event.key == pygame.K_LEFT: 29 | ship.moving_left = False 30 | 31 | def check_events(ai_settings, screen, ship, bullets): 32 | 33 | for event in pygame.event.get(): 34 | if event.type == pygame.QUIT: 35 | sys.exit() 36 | 37 | #按下就连续移动 38 | elif event.type == pygame.KEYDOWN: 39 | check_keydown_events(event, ai_settings, screen, ship, bullets) 40 | 41 | elif event.type == pygame.KEYUP: 42 | check_keyup_events(event, ship) 43 | 44 | 45 | 46 | def update_screen(ai_settings, screen, ship, alien, bullets): 47 | screen.fill(ai_settings.bg_color) 48 | 49 | for bullet in bullets.sprites(): 50 | bullet.draw_bullet() 51 | 52 | ship.blitme() 53 | alien.blitme() 54 | 55 | pygame.display.flip() 56 | 57 | def update_bullets(bullets): 58 | bullets.update() 59 | 60 | for bullet in bullets.copy(): 61 | if bullet.rect.bottom <= 0: 62 | bullets.remove(bullet) 63 | 64 | -------------------------------------------------------------------------------- /alien/day5/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/images/alien.bmp -------------------------------------------------------------------------------- /alien/day5/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day5/images/ship.bmp -------------------------------------------------------------------------------- /alien/day5/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 480 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2.5 11 | 12 | #子弹设置 13 | self.bullet_speed_factor = 1 14 | self.bullet_width = 3 15 | self.bullet_height = 15 16 | self.bullet_color = (60,60,60) 17 | self.bullets_allowed = 5 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /alien/day5/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | -------------------------------------------------------------------------------- /alien/day6/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day6/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day6/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day6/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day6/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day6/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | def blitme(self): 22 | self.screen.blit(self.image, self.rect) 23 | -------------------------------------------------------------------------------- /alien/day6/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | 14 | def run_game(): 15 | pygame.init() 16 | 17 | #建立一个Settings的对象 18 | ai_settings = Settings() 19 | 20 | 21 | 22 | #调用对象内的参数 23 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 24 | pygame.display.set_caption("Alien Invasion") 25 | 26 | #bg_color = (230,230,230) 27 | 28 | ship = Ship(ai_settings, screen) 29 | #alien = Alien(ai_settings, screen) 30 | 31 | bullets = Group() 32 | aliens = Group() 33 | 34 | gf.create_fleet(ai_settings, screen, ship, aliens) 35 | 36 | while True: 37 | 38 | #检测退出信号 39 | gf.check_events(ai_settings, screen, ship, bullets) 40 | 41 | gf.update_screen(ai_settings, screen, ship, aliens, bullets) 42 | 43 | ship.update() 44 | 45 | gf.update_bullets(bullets) 46 | 47 | #print(len(bullets)) 48 | 49 | run_game() 50 | 51 | -------------------------------------------------------------------------------- /alien/day6/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day6/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | 6 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 7 | if event.key == pygame.K_RIGHT: 8 | ship.moving_right = True 9 | if event.key == pygame.K_LEFT: 10 | ship.moving_left = True 11 | 12 | #按下键盘产生子弹的object 13 | if event.key == pygame.K_SPACE: 14 | fire_bullet(ai_settings, screen, ship, bullets) 15 | 16 | if event.key == pygame.K_q: 17 | sys.exit() 18 | 19 | 20 | def fire_bullet(ai_settings, screen, ship, bullets): 21 | if len(bullets) < ai_settings.bullets_allowed: 22 | new_bullet = Bullet(ai_settings, screen, ship) 23 | bullets.add(new_bullet) 24 | 25 | 26 | def check_keyup_events(event, ship): 27 | if event.key == pygame.K_RIGHT: 28 | ship.moving_right = False 29 | if event.key == pygame.K_LEFT: 30 | ship.moving_left = False 31 | 32 | def check_events(ai_settings, screen, ship, bullets): 33 | 34 | for event in pygame.event.get(): 35 | if event.type == pygame.QUIT: 36 | sys.exit() 37 | 38 | #按下就连续移动 39 | elif event.type == pygame.KEYDOWN: 40 | check_keydown_events(event, ai_settings, screen, ship, bullets) 41 | 42 | elif event.type == pygame.KEYUP: 43 | check_keyup_events(event, ship) 44 | 45 | 46 | 47 | def update_screen(ai_settings, screen, ship, aliens, bullets): 48 | screen.fill(ai_settings.bg_color) 49 | 50 | for bullet in bullets.sprites(): 51 | bullet.draw_bullet() 52 | #bullets.draw(screen) 53 | 54 | ship.blitme() 55 | #alien.blitme() 56 | 57 | #for alien in aliens.sprites(): 58 | #alien.blitme() 59 | aliens.draw(screen) 60 | 61 | pygame.display.flip() 62 | 63 | def update_bullets(bullets): 64 | bullets.update() 65 | 66 | for bullet in bullets.copy(): 67 | if bullet.rect.bottom <= 0: 68 | bullets.remove(bullet) 69 | 70 | 71 | 72 | def get_number_aliens_x(ai_settings, alien_width): 73 | #x轴预留后的长度 74 | available_space_x = ai_settings.screen_width - (2* alien_width) 75 | 76 | #计算能存放多少个alien 77 | number_aliens_x = int( available_space_x/(2*alien_width)) 78 | 79 | return number_aliens_x 80 | 81 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 82 | #计算y轴的空闲高度 83 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 84 | 85 | #计算能放几行 86 | number_rows = int( available_space_y / (2* alien_height)) 87 | return number_rows 88 | 89 | 90 | #创建单个敌人 91 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 92 | alien = Alien(ai_settings, screen) 93 | alien_width = alien.rect.width 94 | 95 | alien.x = alien_width + 2* alien_width * alien_number 96 | 97 | alien.rect.x = alien.x 98 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 99 | 100 | aliens.add(alien) 101 | 102 | def create_fleet(ai_settings, screen, ship, aliens): 103 | alien = Alien(ai_settings, screen) 104 | 105 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 106 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 107 | 108 | for row_number in range(number_rows): 109 | for alien_number in range(number_aliens_x): 110 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 111 | 112 | -------------------------------------------------------------------------------- /alien/day6/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/images/alien.bmp -------------------------------------------------------------------------------- /alien/day6/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day6/images/ship.bmp -------------------------------------------------------------------------------- /alien/day6/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (130,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 1.5 11 | 12 | #子弹设置 13 | self.bullet_speed_factor = 10 14 | self.bullet_width = 3 15 | self.bullet_height = 15 16 | self.bullet_color = (60,60,60) 17 | self.bullets_allowed = 3 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /alien/day6/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | -------------------------------------------------------------------------------- /alien/day7/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day7/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day7/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day7/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day7/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day7/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day7/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | 14 | def run_game(): 15 | pygame.init() 16 | 17 | #建立一个Settings的对象 18 | ai_settings = Settings() 19 | 20 | 21 | 22 | #调用对象内的参数 23 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 24 | pygame.display.set_caption("Alien Invasion") 25 | 26 | #bg_color = (230,230,230) 27 | 28 | ship = Ship(ai_settings, screen) 29 | #alien = Alien(ai_settings, screen) 30 | 31 | bullets = Group() 32 | aliens = Group() 33 | 34 | #产生地人群 35 | gf.create_fleet(ai_settings, screen, ship, aliens) 36 | 37 | while True: 38 | 39 | #检测退出信号 40 | gf.check_events(ai_settings, screen, ship, bullets) 41 | 42 | 43 | 44 | ship.update() 45 | 46 | gf.update_bullets(bullets) 47 | gf.update_aliens(ai_settings, aliens) 48 | 49 | #print(len(bullets)) 50 | gf.update_screen(ai_settings, screen, ship, aliens, bullets) 51 | 52 | run_game() 53 | 54 | -------------------------------------------------------------------------------- /alien/day7/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day7/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | 6 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 7 | if event.key == pygame.K_RIGHT: 8 | ship.moving_right = True 9 | if event.key == pygame.K_LEFT: 10 | ship.moving_left = True 11 | 12 | #按下键盘产生子弹的object 13 | if event.key == pygame.K_SPACE: 14 | fire_bullet(ai_settings, screen, ship, bullets) 15 | 16 | if event.key == pygame.K_q: 17 | sys.exit() 18 | 19 | 20 | def fire_bullet(ai_settings, screen, ship, bullets): 21 | if len(bullets) < ai_settings.bullets_allowed: 22 | new_bullet = Bullet(ai_settings, screen, ship) 23 | bullets.add(new_bullet) 24 | 25 | 26 | def check_keyup_events(event, ship): 27 | if event.key == pygame.K_RIGHT: 28 | ship.moving_right = False 29 | if event.key == pygame.K_LEFT: 30 | ship.moving_left = False 31 | 32 | def check_events(ai_settings, screen, ship, bullets): 33 | 34 | for event in pygame.event.get(): 35 | if event.type == pygame.QUIT: 36 | sys.exit() 37 | 38 | #按下就连续移动 39 | elif event.type == pygame.KEYDOWN: 40 | check_keydown_events(event, ai_settings, screen, ship, bullets) 41 | 42 | elif event.type == pygame.KEYUP: 43 | check_keyup_events(event, ship) 44 | 45 | 46 | 47 | def update_screen(ai_settings, screen, ship, aliens, bullets): 48 | screen.fill(ai_settings.bg_color) 49 | 50 | for bullet in bullets.sprites(): 51 | bullet.draw_bullet() 52 | #bullets.draw(screen) 53 | 54 | ship.blitme() 55 | #alien.blitme() 56 | 57 | #for alien in aliens.sprites(): 58 | #alien.blitme() 59 | aliens.draw(screen) 60 | 61 | pygame.display.flip() 62 | 63 | def update_bullets(bullets): 64 | bullets.update() 65 | 66 | for bullet in bullets.copy(): 67 | if bullet.rect.bottom <= 0: 68 | bullets.remove(bullet) 69 | 70 | 71 | 72 | def get_number_aliens_x(ai_settings, alien_width): 73 | #x轴预留后的长度 74 | available_space_x = ai_settings.screen_width - (2* alien_width) 75 | 76 | #计算能存放多少个alien 77 | number_aliens_x = int( available_space_x/(2*alien_width)) 78 | 79 | return number_aliens_x 80 | 81 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 82 | #计算y轴的空闲高度 83 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 84 | 85 | #计算能放几行 86 | number_rows = int( available_space_y / (2* alien_height)) 87 | return number_rows 88 | 89 | 90 | #创建单个敌人 91 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 92 | alien = Alien(ai_settings, screen) 93 | alien_width = alien.rect.width 94 | 95 | alien.x = alien_width + 2* alien_width * alien_number 96 | 97 | alien.rect.x = alien.x 98 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 99 | 100 | alien.index = str(alien_number) + str(row_number) 101 | 102 | print("%s " %(alien.index), end = "*") 103 | aliens.add(alien) 104 | 105 | def create_fleet(ai_settings, screen, ship, aliens): 106 | alien = Alien(ai_settings, screen) 107 | 108 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 109 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 110 | 111 | for row_number in range(number_rows): 112 | for alien_number in range(number_aliens_x): 113 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 114 | 115 | def update_aliens(ai_settings, aliens): 116 | aliens.update() #更新x轴坐标 117 | check_fleet_edges(ai_settings, aliens) 118 | 119 | def check_fleet_edges(ai_settings, aliens): 120 | for alien in aliens.sprites(): 121 | if alien.check_edges(): 122 | change_fleet_direction(ai_settings, aliens) 123 | break 124 | 125 | #如果碰到了边缘,更新方向,并且Y轴向下移动 126 | def change_fleet_direction(ai_settings, aliens): 127 | ai_settings.fleet_direction *= -1 128 | #Y轴向下移动 129 | for alien in aliens.sprites(): 130 | alien.rect.y += ai_settings.alien_drop_speed 131 | -------------------------------------------------------------------------------- /alien/day7/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/images/alien.bmp -------------------------------------------------------------------------------- /alien/day7/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day7/images/ship.bmp -------------------------------------------------------------------------------- /alien/day7/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2 11 | 12 | #子弹设置 13 | self.bullet_speed_factor = 5 14 | self.bullet_width = 3 15 | self.bullet_height = 15 16 | self.bullet_color = (60,60,60) 17 | self.bullets_allowed = 3 18 | 19 | #外星人设置 20 | self.alien_speed_factor = 5 21 | self.alien_drop_speed = 10 22 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 23 | self.fleet_direction = 1 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /alien/day7/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | -------------------------------------------------------------------------------- /alien/day8/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day8/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day8/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day8/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day8/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day8/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day8/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | 14 | def run_game(): 15 | pygame.init() 16 | 17 | #建立一个Settings的对象 18 | ai_settings = Settings() 19 | 20 | #调用对象内的参数 21 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 22 | pygame.display.set_caption("Alien Invasion") 23 | 24 | #bg_color = (230,230,230) 25 | 26 | ship = Ship(ai_settings, screen) 27 | #alien = Alien(ai_settings, screen) 28 | 29 | bullets = Group() 30 | aliens = Group() 31 | 32 | #产生地人群 33 | gf.create_fleet(ai_settings, screen, ship, aliens) 34 | 35 | while True: 36 | #检测退出信号 37 | gf.check_events(ai_settings, screen, ship, bullets) 38 | 39 | ship.update() 40 | 41 | gf.update_bullets(ai_settings, screen, ship, aliens, bullets) 42 | gf.update_aliens(ai_settings, aliens) 43 | 44 | #print(len(bullets)) 45 | gf.update_screen(ai_settings, screen, ship, aliens, bullets) 46 | 47 | run_game() 48 | 49 | -------------------------------------------------------------------------------- /alien/day8/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day8/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | 6 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 7 | if event.key == pygame.K_RIGHT: 8 | ship.moving_right = True 9 | if event.key == pygame.K_LEFT: 10 | ship.moving_left = True 11 | 12 | #按下键盘产生子弹的object 13 | if event.key == pygame.K_SPACE: 14 | fire_bullet(ai_settings, screen, ship, bullets) 15 | 16 | if event.key == pygame.K_q: 17 | sys.exit() 18 | 19 | 20 | def fire_bullet(ai_settings, screen, ship, bullets): 21 | if len(bullets) < ai_settings.bullets_allowed: 22 | new_bullet = Bullet(ai_settings, screen, ship) 23 | bullets.add(new_bullet) 24 | 25 | 26 | def check_keyup_events(event, ship): 27 | if event.key == pygame.K_RIGHT: 28 | ship.moving_right = False 29 | if event.key == pygame.K_LEFT: 30 | ship.moving_left = False 31 | 32 | def check_events(ai_settings, screen, ship, bullets): 33 | 34 | for event in pygame.event.get(): 35 | if event.type == pygame.QUIT: 36 | sys.exit() 37 | 38 | #按下就连续移动 39 | elif event.type == pygame.KEYDOWN: 40 | check_keydown_events(event, ai_settings, screen, ship, bullets) 41 | 42 | elif event.type == pygame.KEYUP: 43 | check_keyup_events(event, ship) 44 | 45 | 46 | 47 | def update_screen(ai_settings, screen, ship, aliens, bullets): 48 | screen.fill(ai_settings.bg_color) 49 | 50 | for bullet in bullets.sprites(): 51 | bullet.draw_bullet() 52 | #bullets.draw(screen) 53 | 54 | ship.blitme() 55 | #alien.blitme() 56 | 57 | #for alien in aliens.sprites(): 58 | #alien.blitme() 59 | aliens.draw(screen) 60 | 61 | pygame.display.flip() 62 | 63 | def update_bullets(ai_settings, screen, ship, aliens, bullets): 64 | bullets.update() 65 | 66 | for bullet in bullets.copy(): 67 | if bullet.rect.bottom <= 0: 68 | bullets.remove(bullet) 69 | 70 | check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets) 71 | 72 | 73 | def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets): 74 | #判断2个Groups是否有交集,返回一个字典 75 | # 最后2个 bool参数表示是否要destory obj 76 | ## Groups1, Groups2, destory for G1 , destory for G2 77 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, False) 78 | 79 | if len(aliens) == 0: 80 | bullets.empty() 81 | create_fleet(ai_settings, screen, ship, aliens) 82 | 83 | 84 | 85 | def get_number_aliens_x(ai_settings, alien_width): 86 | #x轴预留后的长度 87 | available_space_x = ai_settings.screen_width - (2* alien_width) 88 | 89 | #计算能存放多少个alien 90 | number_aliens_x = int( available_space_x/(2*alien_width)) 91 | 92 | return number_aliens_x 93 | 94 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 95 | #计算y轴的空闲高度 96 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 97 | 98 | #计算能放几行 99 | number_rows = int( available_space_y / (2* alien_height)) 100 | return number_rows 101 | 102 | 103 | #创建单个敌人 104 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 105 | alien = Alien(ai_settings, screen) 106 | alien_width = alien.rect.width 107 | 108 | alien.x = alien_width + 2* alien_width * alien_number 109 | 110 | alien.rect.x = alien.x 111 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 112 | 113 | #alien.index = str(alien_number) + str(row_number) 114 | 115 | #print("%s " %(alien.index), end = "*") 116 | aliens.add(alien) 117 | 118 | def create_fleet(ai_settings, screen, ship, aliens): 119 | alien = Alien(ai_settings, screen) 120 | 121 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 122 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 123 | 124 | for row_number in range(number_rows): 125 | for alien_number in range(number_aliens_x): 126 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 127 | 128 | def update_aliens(ai_settings, aliens): 129 | aliens.update() #更新x轴坐标 130 | check_fleet_edges(ai_settings, aliens) 131 | 132 | def check_fleet_edges(ai_settings, aliens): 133 | for alien in aliens.sprites(): 134 | if alien.check_edges(): 135 | change_fleet_direction(ai_settings, aliens) 136 | break 137 | 138 | #如果碰到了边缘,更新方向,并且Y轴向下移动 139 | def change_fleet_direction(ai_settings, aliens): 140 | ai_settings.fleet_direction *= -1 141 | #Y轴向下移动 142 | for alien in aliens.sprites(): 143 | alien.rect.y += ai_settings.alien_drop_speed 144 | 145 | 146 | -------------------------------------------------------------------------------- /alien/day8/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/images/alien.bmp -------------------------------------------------------------------------------- /alien/day8/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day8/images/ship.bmp -------------------------------------------------------------------------------- /alien/day8/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2 11 | 12 | #子弹设置 13 | self.bullet_speed_factor = 5 14 | self.bullet_width = 10 15 | self.bullet_height = 15 16 | self.bullet_color = (60,60,60) 17 | self.bullets_allowed = 3 18 | 19 | #外星人设置 20 | self.alien_speed_factor = 5 21 | self.alien_drop_speed = 10 22 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 23 | self.fleet_direction = 1 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /alien/day8/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | 46 | 47 | self.rect.centerx = self.centerx 48 | 49 | -------------------------------------------------------------------------------- /alien/day9/__pycache__/alien.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/alien.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/__pycache__/bullet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/bullet.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/__pycache__/game_functions.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/game_functions.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/__pycache__/game_stats.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/game_stats.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/__pycache__/ship.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/__pycache__/ship.cpython-37.pyc -------------------------------------------------------------------------------- /alien/day9/alien.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Alien(Sprite): 5 | 6 | def __init__(self, ai_settings, screen): 7 | super().__init__() 8 | self.screen = screen 9 | self.ai_settings = ai_settings 10 | 11 | #load 图片 12 | self.image = pygame.image.load("images/alien.bmp") 13 | self.rect = self.image.get_rect() 14 | 15 | #确定开始坐标 16 | self.rect.x = self.rect.width 17 | self.rect.y = self.rect.height 18 | 19 | self.x = float(self.rect.x) 20 | 21 | self.index = "" 22 | 23 | def blitme(self): 24 | self.screen.blit(self.image, self.rect) 25 | 26 | def update(self): 27 | self.x += ( self.ai_settings.alien_speed_factor * 28 | self.ai_settings.fleet_direction) 29 | self.rect.x = self.x 30 | 31 | def check_edges(self): 32 | screen_rect = self.screen.get_rect() 33 | 34 | if self.rect.right >= screen_rect.right: 35 | return True 36 | elif self.rect.left <=0: 37 | return True 38 | else : 39 | return False 40 | -------------------------------------------------------------------------------- /alien/day9/alien_invasion.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | from settings import Settings 5 | from ship import Ship 6 | 7 | import game_functions as gf 8 | 9 | from pygame.sprite import Group 10 | 11 | from alien import Alien 12 | 13 | from game_stats import GameStats 14 | 15 | def run_game(): 16 | pygame.init() 17 | 18 | #建立一个Settings的对象 19 | ai_settings = Settings() 20 | 21 | #调用对象内的参数 22 | screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height)) 23 | pygame.display.set_caption("Alien Invasion") 24 | 25 | #bg_color = (230,230,230) 26 | 27 | ship = Ship(ai_settings, screen) 28 | #alien = Alien(ai_settings, screen) 29 | 30 | bullets = Group() 31 | aliens = Group() 32 | 33 | #产生地人群 34 | gf.create_fleet(ai_settings, screen, ship, aliens) 35 | 36 | stats = GameStats(ai_settings) 37 | 38 | while True: 39 | #检测退出信号 40 | gf.check_events(ai_settings, screen, ship, bullets) 41 | 42 | if stats.game_active: 43 | ship.update() 44 | 45 | gf.update_bullets(ai_settings, screen, ship, aliens, bullets) 46 | gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) 47 | 48 | #print(len(bullets)) 49 | gf.update_screen(ai_settings, screen, ship, aliens, bullets) 50 | 51 | run_game() 52 | 53 | -------------------------------------------------------------------------------- /alien/day9/bullet.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.sprite import Sprite 3 | 4 | class Bullet(Sprite): 5 | def __init__(self, ai_settings, screen, ship): 6 | super().__init__() 7 | self.screen = screen 8 | 9 | #画一个长方形 10 | self.rect = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 11 | self.rect.centerx = ship.rect.centerx 12 | self.rect.bottom = ship.rect.top 13 | self.y = float(self.rect.y) 14 | 15 | self.color = ai_settings.bullet_color 16 | self.speed_factor = ai_settings.bullet_speed_factor 17 | 18 | def update(self): 19 | self.y -= self.speed_factor 20 | self.rect.y = self.y 21 | 22 | def draw_bullet(self): 23 | pygame.draw.rect(self.screen, self.color, self.rect) 24 | -------------------------------------------------------------------------------- /alien/day9/game_functions.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | from bullet import Bullet 4 | from alien import Alien 5 | from time import sleep 6 | 7 | def check_keydown_events(event, ai_settings, screen, ship, bullets): 8 | if event.key == pygame.K_RIGHT: 9 | ship.moving_right = True 10 | if event.key == pygame.K_LEFT: 11 | ship.moving_left = True 12 | 13 | #按下键盘产生子弹的object 14 | if event.key == pygame.K_SPACE: 15 | fire_bullet(ai_settings, screen, ship, bullets) 16 | 17 | if event.key == pygame.K_q: 18 | sys.exit() 19 | 20 | 21 | def fire_bullet(ai_settings, screen, ship, bullets): 22 | if len(bullets) < ai_settings.bullets_allowed: 23 | new_bullet = Bullet(ai_settings, screen, ship) 24 | bullets.add(new_bullet) 25 | 26 | 27 | def check_keyup_events(event, ship): 28 | if event.key == pygame.K_RIGHT: 29 | ship.moving_right = False 30 | if event.key == pygame.K_LEFT: 31 | ship.moving_left = False 32 | 33 | def check_events(ai_settings, screen, ship, bullets): 34 | 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | sys.exit() 38 | 39 | #按下就连续移动 40 | elif event.type == pygame.KEYDOWN: 41 | check_keydown_events(event, ai_settings, screen, ship, bullets) 42 | 43 | elif event.type == pygame.KEYUP: 44 | check_keyup_events(event, ship) 45 | 46 | 47 | 48 | def update_screen(ai_settings, screen, ship, aliens, bullets): 49 | screen.fill(ai_settings.bg_color) 50 | 51 | for bullet in bullets.sprites(): 52 | bullet.draw_bullet() 53 | #bullets.draw(screen) 54 | 55 | ship.blitme() 56 | #alien.blitme() 57 | 58 | #for alien in aliens.sprites(): 59 | #alien.blitme() 60 | aliens.draw(screen) 61 | 62 | pygame.display.flip() 63 | 64 | def update_bullets(ai_settings, screen, ship, aliens, bullets): 65 | bullets.update() 66 | 67 | for bullet in bullets.copy(): 68 | if bullet.rect.bottom <= 0: 69 | bullets.remove(bullet) 70 | 71 | check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets) 72 | 73 | 74 | def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets): 75 | #判断2个Groups是否有交集,返回一个字典 76 | # 最后2个 bool参数表示是否要destory obj 77 | ## Groups1, Groups2, destory for G1 , destory for G2 78 | collisions = pygame.sprite.groupcollide(aliens, bullets, True, False) 79 | 80 | if len(aliens) == 0: 81 | bullets.empty() 82 | create_fleet(ai_settings, screen, ship, aliens) 83 | 84 | 85 | 86 | def get_number_aliens_x(ai_settings, alien_width): 87 | #x轴预留后的长度 88 | available_space_x = ai_settings.screen_width - (2* alien_width) 89 | 90 | #计算能存放多少个alien 91 | number_aliens_x = int( available_space_x/(2*alien_width)) 92 | 93 | return number_aliens_x 94 | 95 | def get_number_aliens_y(ai_settings, ship_height, alien_height): 96 | #计算y轴的空闲高度 97 | available_space_y = ai_settings.screen_height - ship_height - 3*alien_height; 98 | 99 | #计算能放几行 100 | number_rows = int( available_space_y / (2* alien_height)) 101 | return number_rows 102 | 103 | 104 | #创建单个敌人 105 | def create_alien(ai_settings, screen, aliens, alien_number, row_number): 106 | alien = Alien(ai_settings, screen) 107 | alien_width = alien.rect.width 108 | 109 | alien.x = alien_width + 2* alien_width * alien_number 110 | 111 | alien.rect.x = alien.x 112 | alien.rect.y = alien.rect.height + 2*alien.rect.height* row_number 113 | 114 | #alien.index = str(alien_number) + str(row_number) 115 | 116 | #print("%s " %(alien.index), end = "*") 117 | aliens.add(alien) 118 | 119 | def create_fleet(ai_settings, screen, ship, aliens): 120 | alien = Alien(ai_settings, screen) 121 | 122 | number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width) 123 | number_rows = get_number_aliens_y(ai_settings, ship.rect.height, alien.rect.height) 124 | 125 | for row_number in range(number_rows): 126 | for alien_number in range(number_aliens_x): 127 | create_alien(ai_settings, screen, aliens, alien_number, row_number) 128 | 129 | def update_aliens(ai_settings, stats, screen, ship, aliens, bullets): 130 | aliens.update() #更新x轴坐标 131 | check_fleet_edges(ai_settings, aliens) 132 | 133 | #检查aliens和ship触碰 134 | if pygame.sprite.spritecollideany(ship,aliens): 135 | #print("Ship hit!!!") 136 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 137 | 138 | #检查aliens和屏幕底变的触碰 139 | check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets) 140 | 141 | #最终减命的函数 142 | def ship_hit(ai_settings, stats, screen, ship, aliens, bullets): 143 | if stats.ships_left > 1: 144 | stats.ships_left -= 1 145 | 146 | aliens.empty() 147 | bullets.empty() 148 | 149 | create_fleet(ai_settings, screen, ship, aliens) 150 | ship.center_ship() 151 | 152 | sleep(0.5) 153 | else: 154 | stats.game_active = False 155 | 156 | def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets): 157 | screen_rect = screen.get_rect() 158 | 159 | for alien in aliens.sprites(): 160 | if alien.rect.bottom >= screen_rect.bottom: 161 | ship_hit(ai_settings, stats, screen, ship, aliens, bullets) 162 | break 163 | 164 | def check_fleet_edges(ai_settings, aliens): 165 | for alien in aliens.sprites(): 166 | if alien.check_edges(): 167 | change_fleet_direction(ai_settings, aliens) 168 | break 169 | 170 | #如果碰到了边缘,更新方向,并且Y轴向下移动 171 | def change_fleet_direction(ai_settings, aliens): 172 | ai_settings.fleet_direction *= -1 173 | #Y轴向下移动 174 | for alien in aliens.sprites(): 175 | alien.rect.y += ai_settings.alien_drop_speed 176 | 177 | 178 | -------------------------------------------------------------------------------- /alien/day9/game_stats.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | class GameStats(): 4 | 5 | def __init__(self, ai_settings): 6 | self.ai_settings = ai_settings 7 | self.game_active = True 8 | self.reset_stats() 9 | 10 | def reset_stats(self): 11 | self.ships_left = self.ai_settings.ship_limit 12 | -------------------------------------------------------------------------------- /alien/day9/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/images/alien.bmp -------------------------------------------------------------------------------- /alien/day9/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/day9/images/ship.bmp -------------------------------------------------------------------------------- /alien/day9/settings.py: -------------------------------------------------------------------------------- 1 | 2 | class Settings(): 3 | 4 | def __init__(self): 5 | self.screen_width = 800 6 | self.screen_height = 600 7 | self.bg_color = (230,230,230) 8 | 9 | #友机左右移动的速度因子(step) 10 | self.ship_movingspeed_factor = 2 11 | self.ship_limit = 3 12 | 13 | #子弹设置 14 | self.bullet_speed_factor = 5 15 | self.bullet_width = 10 16 | self.bullet_height = 15 17 | self.bullet_color = (60,60,60) 18 | self.bullets_allowed = 3 19 | 20 | #外星人设置 21 | self.alien_speed_factor = 5 22 | self.alien_drop_speed = 50 23 | #设置左右移动的参数。 1 = 右移动 -1 = 向左移动 24 | self.fleet_direction = 1 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /alien/day9/ship.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | class Ship(): 4 | 5 | def __init__(self, ai_settings, screen): 6 | self.screen = screen 7 | 8 | self.ai_settings = ai_settings 9 | 10 | self.moving_right = False 11 | self.moving_left = False 12 | 13 | self.image = pygame.image.load("images/ship.bmp") 14 | #建立2个对象,一个是飞船的对象,一个是窗口对象 15 | self.rect = self.image.get_rect() 16 | self.screen_rect = screen.get_rect() 17 | 18 | #把飞船的位置,确定下来。(底部中间!) 19 | self.rect.centerx = self.screen_rect.centerx 20 | self.rect.bottom = self.screen_rect.bottom 21 | 22 | self.centerx = float(self.rect.centerx) 23 | 24 | def blitme(self): 25 | # 把飞船画出来! 26 | self.screen.blit(self.image, self.rect) 27 | 28 | def update(self): 29 | 30 | #加入速度因子的方式 31 | #if self.moving_right == True: 32 | # self.centerx += self.ai_settings.ship_movingspeed_factor 33 | #if self.moving_left == True: 34 | # self.centerx -= self.ai_settings.ship_movingspeed_factor 35 | 36 | #加入速度因子和限位 37 | if self.moving_right and self.rect.right < self.screen_rect.right: 38 | self.centerx += self.ai_settings.ship_movingspeed_factor 39 | if self.moving_left and self.rect.left > 0: 40 | self.centerx -= self.ai_settings.ship_movingspeed_factor 41 | 42 | #if self.moving_right and self.moving_left: 43 | # print("left and right both keydown!") 44 | 45 | self.rect.centerx = self.centerx 46 | 47 | def center_ship(self): 48 | self.centerx = self.screen_rect.centerx 49 | self.rect.centerx = self.screen_rect.centerx 50 | 51 | -------------------------------------------------------------------------------- /alien/summary/images/alien.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/summary/images/alien.bmp -------------------------------------------------------------------------------- /alien/summary/images/ship.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/alien/summary/images/ship.bmp -------------------------------------------------------------------------------- /alien/summary/main.py: -------------------------------------------------------------------------------- 1 | #basic 2 | import sys 3 | import pygame 4 | import pygame.font #文字 5 | 6 | #集群管理 7 | from pygame.sprite import Sprite 8 | from pygame.sprite import Group 9 | 10 | 11 | class OneImage(Sprite): 12 | 13 | def __init__(self, screen): 14 | super().__init__() 15 | self.screen = screen 16 | #这里的rect 和image 不能随便书写。因为调用Group().draw()会用到! 17 | self.image = pygame.image.load("images/alien.bmp") 18 | self.rect = self.image.get_rect() 19 | 20 | mouse_x = 0 21 | mouse_y = 0 22 | 23 | 24 | def run_game(): 25 | 26 | 27 | #print("hello") 28 | 29 | global mouse_x,mouse_y 30 | 31 | pygame.init() 32 | #屏幕设置(舞台设置) 33 | 34 | screen = pygame.display.set_mode((800,600)) 35 | pygame.display.set_caption("666") 36 | 37 | screen_rect = screen.get_rect() 38 | 39 | while True: 40 | 41 | #消息处理 42 | for event in pygame.event.get(): 43 | #print("get event!") 44 | if event.type == pygame.QUIT:#处理右上角的退出叉叉 45 | sys.exit() 46 | 47 | #处理q键 48 | elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: 49 | sys.exit() 50 | 51 | elif event.type == pygame.MOUSEBUTTONDOWN: 52 | mouse_x,mouse_y = pygame.mouse.get_pos() 53 | 54 | #背景颜色 55 | bg_color = (230,230,230) 56 | screen.fill(bg_color) 57 | 58 | #画文字 59 | str_temp = "1111" 60 | 61 | font = pygame.font.SysFont(None, 48) 62 | text_color = (30, 30, 30) 63 | 64 | text_image = font.render(str(str_temp), True, text_color, bg_color) 65 | 66 | text_image_rect = text_image.get_rect() 67 | text_image_rect.left = screen_rect.left + 10 68 | text_image_rect.top = screen_rect.top + 10 69 | 70 | screen.blit(text_image, text_image_rect) 71 | 72 | 73 | #画图片(单个) 74 | 75 | one_image = pygame.image.load("images/ship.bmp") 76 | 77 | one_image_rect = one_image.get_rect() 78 | one_image_rect.centerx = screen_rect.centerx 79 | one_image_rect.bottom = screen_rect.top+100 80 | 81 | screen.blit(one_image, one_image_rect) 82 | 83 | 84 | #画图片(集群) 85 | Myimages = Group() 86 | 87 | for num in range(3): 88 | temp = OneImage(screen) 89 | temp.rect.y = 150 90 | temp.rect.x = 100 + num * temp.rect.width 91 | Myimages.add(temp) 92 | 93 | #print(len(Myimages)) 94 | Myimages.draw(screen) 95 | 96 | 97 | #画矩形 98 | rect1_color = (0xcd,0x1a,0xcd) #CD1ACD 99 | 100 | #rect1 = pygame.Rect(0,0,ai_settings.bullet_width, ai_settings.bullet_height) 101 | rect1 = pygame.Rect(0, 0, 50, 100) 102 | 103 | rect1.centerx = screen_rect.right-60 104 | rect1.bottom = screen_rect.top+300 105 | 106 | pygame.draw.rect(screen, rect1_color, rect1) 107 | 108 | 109 | #画按钮分2步骤 110 | 111 | b_width = 200 112 | b_height = 50 113 | b_color = (0, 0, 255) 114 | b_text_color = (255,255,255) 115 | b_font = pygame.font.SysFont(None, 48) 116 | 117 | b_rect = pygame.Rect(0, 0, b_width, b_height) 118 | b_rect.center = screen_rect.center 119 | 120 | #画矩形背景 121 | screen.fill(b_color, b_rect) 122 | 123 | button_clicked = b_rect.collidepoint(mouse_x, mouse_y) 124 | 125 | if button_clicked : 126 | #print("111") 127 | b_image = b_font.render("clicked", True, b_text_color, b_color) 128 | else: 129 | #print("222") 130 | b_image = b_font.render("Play", True, b_text_color, b_color) 131 | 132 | b_image_rect = b_image.get_rect() 133 | b_image_rect.center = b_rect.center 134 | 135 | 136 | #画背景之上的图片 137 | screen.blit(b_image, b_image_rect) 138 | 139 | 140 | #显存刷新到GUI上 141 | pygame.display.flip() 142 | 143 | 144 | run_game() 145 | 146 | -------------------------------------------------------------------------------- /argon_one/关于argon one 程序的一些解析(1).md: -------------------------------------------------------------------------------- 1 | # 关于argon one 程序的一些解析(1) 2 | 3 | 4 | 5 | ### 获取脚本 6 | 7 | * 下载地址 8 | 9 | ``` 10 | wget https://download.argon40.com/argon1.sh 11 | 12 | ``` 13 | 14 | * 安装 15 | 16 | ``` 17 | # for raspberry pi OS (raspbian) 18 | curl https://download.argon40.com/argon1.sh | bash 19 | 20 | # for other OS 21 | curl https://download.argon40.com/argonone-setup-recalbox.sh | bash 22 | 23 | curl https://download.argon40.com/argonone-setup-osmc.sh | bash 24 | 25 | curl https://download.argon40.com/argonone-setup-libreelec.sh | bash 26 | 27 | https://github.com/meuter/argon-one-case-ubuntu-20.04 28 | 29 | ``` 30 | 31 | > 多个教程地址: 32 | > 33 | > https://www.waveshare.com/wiki/PI4-CASE-ARGON-ONE 34 | > 35 | > https://www.argon40.com/learn/index.php/2020/03/10/argon-one-installation-guide-for-recalbox/ 36 | > 37 | > https://www.argon40.com/learn/index.php/2020/03/10/argon-one-installation-guide-for-osmc/ 38 | > 39 | > https://www.argon40.com/learn/index.php/2020/03/10/argon-one-installation-guide-for-libreelec/ 40 | 41 | 42 | 43 | ### 脚本的解析 44 | 45 | argon的脚本第一次看会感觉超级累。因为脚本很多都是在执行release file的过程。大部分用到了echo语句。我先整理了一下脚本的框架 46 | 47 | 1. 3个shell函数。其实总结为2个功能: 48 | * **强制**创建一个文件 49 | * 检查一大堆包有没有被安装,这里可能存在一个语言系统的bug。 50 | 51 | 2. 一堆文件名的宏定义: 52 | 53 | 程序: 54 | 55 | * powerbuttonscript=/usr/bin/argononed.py 56 | 57 | * shutdownscript=/lib/systemd/system-shutdown/argononed-poweroff.py 58 | 59 | 配置表: 60 | 61 | * daemonconfigfile=/etc/argononed.conf 62 | 63 | * configscript=/usr/bin/argonone-config 64 | 65 | * removescript=/usr/bin/argonone-uninstall 66 | 67 | 自动运行的服务: 68 | 69 | * daemonfanservice=/lib/systemd/system/argononed.service 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /argon_one/关于argon one 程序的一些解析(2).md: -------------------------------------------------------------------------------- 1 | # 关于argon one 程序的一些解析(2) 2 | 3 | 4 | 5 | 要把一个python程序变成一种服务进行自动启动,可以用到systemd(system daemon)的技术。其中systemctl只systemd中的一个管理控制程序。实现过程如下: 6 | 7 | 8 | 9 | * 编程自己的py源程序。并且调试都通过了。(powerbuttonscript=/usr/bin/argononed.py) 10 | 11 | * 编写xxx.service的配置表(/lib/systemd/system/argononed.service) 12 | 13 | ``` 14 | [Unit] 15 | Description=Test Service 16 | After=multi-user.target 17 | 18 | [Service] 19 | Type=idle 20 | ExecStart=/usr/bin/python3 /usr/bin/argononed.py > /home/pi/x.log 2>&1 21 | 22 | [Install] 23 | WantedBy=multi-user.target 24 | ``` 25 | 26 | ``` 27 | sudo chmod 644 /lib/systemd/system/argononed.service 28 | ``` 29 | 30 | * 激活配置文件 31 | 32 | ``` 33 | sudo systemctl daemon-reload 34 | 35 | sudo systemctl enable argononed.service #反操作就是disable。会提示"Created symlink /etc/systemd/system/multi-user.target.wants/argononed.service → /lib/systemd/system/argononed.service." 36 | 37 | sudo systemctl start argononed.service #反操作就是stop 38 | 39 | ``` 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /argon_one/关于argon one 程序的一些解析(3完结).md: -------------------------------------------------------------------------------- 1 | # 关于argon one 程序的一些解析(3完结) 2 | 3 | 在设计的时候,单树莓派执行了halt语句后。我们需要发送一个信号给单片机,让hat板上的单片机切断整个pi4的电源。这是如何做到的呢? 4 | 5 | 6 | 7 | **/lib/systemd/system-shutdown/argononed-poweroff.py** 8 | 9 | 这个程序会在系统关闭前,自动运行。 10 | 11 | 12 | 13 | ### 整个控制流程的解析: 14 | 15 | * 如果用按钮控制pi的重启、软关机、强行关闭 16 | 17 | > 按钮单击,打开pmos。让整个系统通电 18 | > 19 | > 按钮持续按5s+,关闭pmos。让整个系统断电 20 | > 21 | > 按钮双击,mcu输出一个0.03s高电平脉冲给pi。pi上的程序探测到脉冲,然后进行软件重启 22 | > 23 | > 按钮保持3s中。mcu输出一个0.05s的高电平脉冲给pi。pi进行软件关机。 24 | 25 | * 用ssh关机,也需要关闭系统或者关闭风扇 26 | 27 | > 在系统里的特定目录下,利用系统的systemd功能,来实现自动运行脚本。脚本的目录是**/lib/systemd/system-shutdown/** 。脚本内可以判断是否是reboot,还是halt or poweroff。 最重要的是这个脚本是由系统在关机前自动调用的。所以才能实现通知mcu关闭pmos的功能。 28 | 29 | * 智能调速 30 | 31 | > 一个常驻内存的python程序,每30s读取一下cpu的温度。然后根据温度发送风扇的转速值给MCU。(接口为iic总线)。MCU对iic进行解码,然后控制风扇的速度。 32 | > 33 | > 其中还有一个温度控制的配置表,存放在 **/etc/argonone.conf** ,这里控制的是温度,和对应温度的转速值。温度的排序最好是从高到低的排序。 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /clip_image002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/clip_image002.jpg -------------------------------------------------------------------------------- /git.md: -------------------------------------------------------------------------------- 1 | # upgrade my repo 2 | 3 | 4 | ```shell 5 | git config --global user.name "111" 6 | 7 | git config --global user.email "111@111.com" 8 | 9 | git config --global credential.helper store 10 | 11 | git config --list 12 | 13 | git add . 14 | 15 | git commit -m "hello world" 16 | 17 | git push 18 | ``` 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /mac终端(terminal)爬梯子.md: -------------------------------------------------------------------------------- 1 | # mac终端(terminal)爬梯子 2 | 3 | 4 | 5 | ## 前期知识 6 | 7 | mac终端如果要上梯子。只要先期待ss client gui版。然后在gui里面选择listening sock5:1080 8 | http 1087 。 all done! 9 | ``` 10 | export http_proxy=http://127.0.0.1:1087 11 | 12 | export https_proxy=http://127.0.0.1:1087 13 | 14 | export ALL_PROXY=socks5://127.0.0.1:1080 15 | ``` 16 | 17 | ## 制作~/.bash_profile 18 | 19 | ``` 20 | alias ss1="export ALL_PROXY=socks5://127.0.0.1:1080" 21 | alias ss0="unset ALL_PROXY" 22 | alias ip="curl https://ip.cn" 23 | ``` 24 | ## 然后生效.bash_profile 25 | `source ./.bash_profile` 26 | 27 | ## 使用 28 | ``` 29 | ss1 表示 ss on 30 | ss0 表示 ss off 31 | ip 表示查看是否已经爬到外面ip 32 | 33 | 34 | 35 | ``` 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /pi4_how_to_flash_64b_os.md: -------------------------------------------------------------------------------- 1 | # 树莓派8GB的开箱和64位系统的刷写 2 | 3 | 4 | 5 | ### Pi4的历史: 6 | 7 | 在2019年6月的时候,树莓派基金会就发布了Pi4 1GB 、2GB、4GB 3个版本,然后细心的小伙伴去当时印刷的说明书上,还有一个8GB的版本没有发布。 8 | 9 | 为什么8GB没有发布??UP去国外查了相关的资料。实际是8GB单颗粒内存的缺货导致的。所以基金会一直没有把8GB批量投放市场。到了今年5月28日,基金会突然向全世界玩家发布了8GB这个型号。然后还同时发布了64bit系统。因为之前的Raspbian系统,一直都是32位的天下。而如今Pi4的CPU已经非常强悍,在配上8GB内存,去跑32位的系统和应用,性能无法全部释放出来。 10 | 11 | 虽然UP做这期视频的时候,基金会官宣的系统还是Raspberry Pi OS 32bit。保不齐过不了多久,就会官宣上架64Bit的系统了。 12 | 13 | 14 | 15 | ### 优势 16 | 17 | 国外评测结果: https://hackaday.com/2020/01/28/raspberry-pi-4-benchmarks-32-vs-64-bits/ 18 | 19 | ![raspbench-1](raspbench-1.png) 20 | 21 | ### 下载途径 22 | 23 | * 官方网站 24 | 25 | 首先要去check的地址是: https://www.raspberrypi.org/downloads/raspberry-pi-os/ 26 | 27 | 这里是基金会发布最新稳定版系统的唯一途径。目前2020.6.17日,还只有32bit的稳定版本。所以我们要pass这个网址。 28 | 29 | * 官方论坛 30 | 31 | 其次想要下载beta版本的系统,可以访问 https://www.raspberrypi.org/forums/viewtopic.php?f=117&t=275370 这个网址。 32 | 33 | ​ ![img](clip_image002.jpg) 34 | 35 | 36 | 37 | ### 烧写过程 38 | 39 | 可以查看up的往期视频: https://www.bilibili.com/video/BV1hE411y7ag/ 40 | 41 | 42 | 43 | 44 | 45 | ### 如何验证64位系统 46 | 47 | * 系统验证 48 | 49 | ``` 50 | uname -a 51 | 52 | getconf LONG_BIT 53 | ``` 54 | 55 | 56 | 57 | * 应用程序验证 58 | 59 | ``` 60 | file /bin/ls 61 | 62 | file /bin/cp 63 | 64 | file /bin/bash 65 | 66 | file /bin/mv 67 | ``` 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /python/__pycache__/l1.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/python/__pycache__/l1.cpython-37.pyc -------------------------------------------------------------------------------- /python/l1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import pygame 3 | 4 | 5 | def run_game(): 6 | pygame.init() 7 | screen = pygame.display.set_mode((1200,800)) 8 | pygame.dispaly.set_caption("Alien Invasion 666") 9 | 10 | bg_color = ((110,230,230)) 11 | 12 | while True: 13 | 14 | for event in pygame.event.get(): 15 | if event.type == pygame.QUIT: 16 | sys.exit() 17 | 18 | screen.fill(bg_color) 19 | 20 | pygame.display.flip() 21 | 22 | 23 | run_game() 24 | -------------------------------------------------------------------------------- /python/lesson1_intro_python.md: -------------------------------------------------------------------------------- 1 | # Pi4上python2和python3的介绍 2 | 3 | ## 前言 4 | * 命令行进入python2 和python3 ,输出的版本不一样。 5 | * 2代比较老。建议2019年开始学3。 6 | * 学会3之后,在来补2。解决历史库遗留问题。就算不学2,你也一样可以干活了。比如print语法不同 7 | 8 | ## 运行方式 9 | * 直接在python终端下运行。验证库是否正确安装了 10 | ``` 11 | import tkinter 12 | ``` 13 | 14 | * 一般用IDE开发。比如raspbian full上的 **TH**,有颜色提示 15 | * 编写第一个程序hello world 16 | * 终端运行的方法。`python3 xxx.py` 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /raspbench-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rcdrones/pi4_tutorial/HEAD/raspbench-1.png -------------------------------------------------------------------------------- /如何搜索一个你要的软件.md: -------------------------------------------------------------------------------- 1 | # 有具体的需求,如何查找apt软件的名称? 2 | 3 | ## 需求 4 | * 我想找个raspbian下的音乐播放器?用百度? 5 | * 我想找个冷门的工具?百度无解,怎么办? 6 | 基于多个我想……,我在开发中发现了以下本地查询包名称的方案。 7 | 8 | 9 | 1. 646 10 | 2. 694 11 | 12 | 13 | - [X] hhtr 14 | - [ ] 65464 15 | - [x] 65464 16 | - [ ] 8998 17 | - [x] ad 18 | 19 | 20 | 21 | 22 | 23 | 24 | ## 查询包名称的命令 25 | 26 | ``` 27 | sudo apt-cache search "xxx" 28 | 29 | #比如我想找个音频播放器 30 | sudo apt-cache search "audio player" 31 | 得到关键词qmmp 32 | 33 | 34 | sudo apt-cache search "foobar" 35 | 得到关键信息lxmusic 36 | 37 | 38 | #终端返回的信息量太大,怎么办? 39 | cd ~ 40 | sudo apt-cache search "edit" >> 1.txt 41 | 然后查看1.txt文件 42 | 43 | #显示更多用法: 44 | sudo apt-cache --help 45 | 46 | 47 | ``` 48 | 49 | ## 安装软件和卸载软件 50 | ``` 51 | #安装 52 | sudo apt-get install qmmp -y 53 | 54 | #卸载 55 | sudo apt-get autoremove qmmp 56 | 57 | #帮助 58 | sudo apt-get --help 59 | 60 | ``` 61 | 62 | 63 | -------------------------------------------------------------------------------- /树莓派4 无显示,该怎么玩?(关于激活WIFI、使能SSH、搭建VNC的教程).md: -------------------------------------------------------------------------------- 1 | # 树莓派4 无显示器和键盘,该怎么玩?(关于激活WIFI、使能SSH、搭建VNC的教程) 2 | 3 | ## 前期准备 4 | 5 | Pi4 TF卡刷入最新的官方系统,我用的是:`2019-09-26-raspbian-buster-full.zip` 6 | 7 | ## 激活WIFI 8 | 9 | 在windows电脑上,进去boot分区,建一个 `wpa_supplicant.conf` 文件,文件的内容如下 10 | 11 | ``` 12 | country=CN 13 | ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 14 | update_config=1 15 | 16 | 17 | network={ 18 | ssid="WiFi-B" #ssid:网络的ssid 19 | psk="12345678" #psk:密码 20 | key_mgmt=WPA-PSK # WiFi 使用WPA/WPA2加密 21 | priority=1 #priority:连接优先级,数字越大优先级越高(不可以是负数) 22 | scan_ssid=0 #scan_ssid:连接隐藏WiFi时需要指定该值为1 23 | } 24 | ``` 25 | 26 | 27 | 28 | 扩展阅读: 29 | 30 | ``` 31 | #如果你的 WiFi 没有密码 32 | network={ 33 | ssid="你的无线网络名称(ssid)" 34 | key_mgmt=NONE 35 | } 36 | 37 | #如果你的 WiFi 使用WEP加密 38 | network={ 39 | ssid="你的无线网络名称(ssid)" 40 | key_mgmt=NONE 41 | wep_key0="你的wifi密码" 42 | } 43 | 44 | #如果你的 WiFi 使用WPA/WPA2加密 45 | network={ 46 | ssid="你的无线网络名称(ssid)" 47 | key_mgmt=WPA-PSK 48 | psk="你的wifi密码" 49 | } 50 | ``` 51 | 52 | 53 | 54 | 55 | 56 | ## 使能SSH 57 | 58 | SSH,可以简单的理解为Linux的远程命令行工具。在windows电脑上,进去boot分区。建立一个名字为ssh的空白文件即可。*注意要小写且不要有任何扩展名。* 59 | 树莓派在启动之后会在检测到这个文件之后自动启用 ssh 服务。随后即可通过*登录路由器*找到树莓派的 IP 地址,通过 ssh 连接到树莓派了。 60 | 61 | 62 | 63 | ### windows 64 | 65 | 用windows下的Putty来链接Pi4! 66 | 67 | 68 | 69 | ### mac 70 | 71 | 打开mac终端,输入: 72 | 73 | ``` 74 | ssh pi@192.168.1.66 #IPv4地址是Pi4的实际局域网IP地址 75 | ``` 76 | 77 | 78 | 79 | 80 | 81 | ## 搭建VNC远程桌面 82 | 83 | SSH打命令对新手不友好。那么就用用Linux的远程桌面吧。 84 | 85 | ``` 86 | sudo raspi-config #进如Pi的系统配置程序 87 | sudo reboot 88 | ``` 89 | 90 | 91 | 92 | 在windows或者mac下,访问[ https://www.realvnc.com/en/connect/download/viewer/ ]( https://www.realvnc.com/en/connect/download/viewer/ ),下载对应的客户端。 93 | 94 | 然后在windows下远程桌面访问Pi4。 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /树莓派安装OpenCV开发环境.md: -------------------------------------------------------------------------------- 1 | # 树莓派安装OpenCV开发环境 2 | 3 | # 简述 4 | 5 | OpenCV是一种开源的计算机图形视觉库。简单的说,就是程序员用这个库可以在二次开发一些关于摄像头的应用。摄像头就是计算机的眼睛对吧? OpenCV就是大脑采集到眼睛的数据,进行后端处理的一段小程序。典型的应用,例如:颜色识别、人脸检测、人脸识别、二维码解密、车牌号识别…… 6 | 7 | 问:OpenCV在树莓派上怎么用?? 8 | 9 | 答:配合Python,这种胶水语言。把OpenCV库加载到Python里面,然后在库的基础上做二次开发。That’s all! 10 | 11 | 问:如何搭建OpenCV的Python开发环境? 12 | 13 | 答:分为Python2 和 Python3。2条支线。由于Python2和Python3差异很大,所以我们下面就开干吧! 14 | 15 | 16 | 17 | 问:UP主你用的系统是什么?? 18 | 19 | 答:Pi4 刷上全新的2019-09-26-raspbian-buster-full.zip 20 | 21 | 22 | 23 | ## 前期准备 24 | 25 | 1. apt源地址的更改 26 | 2. 配置好你的Pi4 Wifi,或者用有线网络!确保能上网。 27 | 3. 不要用ssh。直接把Pi4接到显示器上,在进行安装。由于安装时间较长,SSH可能有假死,导致你无法知道安装进度的情况! 28 | 4. 更改pip源地址 29 | 5. 配置好输入法、时区、键盘型号。 30 | 6. 想到了在补充。 31 | 7. 原则上就是把你的Pi4搞成能舒舒服服用的状态就对了,后面还有很多坑,等你跳。做完了这些准备。让我们一起跳坑吧!!哈哈哈哈! 32 | 33 | 34 | 35 | ## Python2 上的OpenCV库 36 | 37 | * 安装 38 | ``` 39 | sudo apt-get install libopencv-dev -y 40 | 41 | sudo apt-get install python-opencv -y 42 | 43 | one cmd ok? 44 | sudo apt-get install libopencv-dev python-opencv -y 45 | ``` 46 | 47 | 48 | * 测试 49 | ``` 50 | import cv2 51 | 52 | cv2.__version__ 53 | ``` 54 | 55 | > 为啥叫cv2而不叫opencv呢?这是因为OpenCV是基于C/C++开发的,有两个版本,’‘cv”版本的API是C语言开发的,’‘cv2’'版本的API是基于C++语言开发的,为了保持向后兼容性所以叫"cv2",但我们都知道cv2就是OpenCV本尊。 56 | 57 | 58 | * 应用 59 | 60 | ``` 61 | git clone https://github.com/TommyZihao/opencvtest.git 62 | cd opencvtest 63 | python2 testopencv.py 64 | ``` 65 | 66 | 67 | 68 | 69 | 70 | ## Python3 上的OpenCV库 71 | 72 | * 安装OpenCV的所需库 73 | ``` 74 | sudo apt-get install build-essential git cmake pkg-config -y 75 | sudo apt-get install libjpeg8-dev -y 76 | sudo apt-get install libtiff5-dev -y 77 | sudo apt-get install libjasper-dev -y 78 | sudo apt-get install libpng12-dev -y 79 | 80 | sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y 81 | 82 | sudo apt-get install libgtk2.0-dev -y 83 | sudo apt-get install libatlas-base-dev gfortran -y 84 | ``` 85 | 86 | 87 | * 下载OpenCV 88 | ``` 89 | cd /home/pi/Downloads 90 | 91 | wget https://github.com/Itseez/opencv/archive/3.4.0.zip 92 | 93 | wget https://github.com/Itseez/opencv_contrib/archive/3.4.0.zip 94 | 95 | cd /home/pi/Downloads 96 | 97 | unzip opencv-3.4.0.zip 98 | 99 | unzip opencv_contrib-3.4.0.zip 100 | 101 | ``` 102 | 103 | * 设置编译参数 104 | ``` 105 | cd /home/pi/Downloads/opencv-3.4.0 106 | mkdir build 107 | cd build 108 | 109 | cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Downloads/opencv_contrib-3.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_LIBV4L=ON PYTHON3_EXECUTABLE=/usr/bin/python3.5 PYTHON_INCLUDE_DIR=/usr/include/python3.5 PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/pi/.local/lib/python3.5/site-packages/numpy/core/include .. 110 | 111 | ``` 112 | 113 | * 编译 114 | 115 | ``` 116 | cd /home/pi/Downloads/opencv-3.4.0/build 117 | make 118 | 119 | sudo make install 120 | ``` 121 | 122 | * 在Python3上测试OpenCV是否成功 123 | ``` 124 | import cv2 125 | cv2.__version__ 126 | 127 | ``` 128 | 129 | * 补充 130 | 131 | 132 | ``` 133 | 134 | ``` -------------------------------------------------------------------------------- /树莓派网址收集.md: -------------------------------------------------------------------------------- 1 | # 树莓派网址收集 2 | 3 | 蓝宙资料: 4 | 5 | 链接: https://pan.baidu.com/s/1KaqbeH7fA0YRXaZlbUTwZg&shfl=shareset 提取码: ntg8 6 | 7 | --------------------------------------------------------------------------------