Py学习  »  Python

一行 Python 代码,畅玩童年经典游戏

AirPython • 2 年前 • 313 次点击  



大家好,我是安果!

最近在 Github 上看到一个有趣的 Python 项目free-python-games,star 数超过 2.3K,它包含了 22 个简单的小游戏,所有游戏都是使用 Python 和 Turtle 模块实现的,Windows、Mac OS 或 Linux 皆可运行,我们先来看看有哪些游戏!

Snake贪吃蛇——经典街机游戏

Pacmax吃豆人——经典街机游戏

Cannon加农炮——抛射运动。类似愤怒的小鸡~

Maze迷宫小游戏

Pong碰球——经典街机游戏

满满的童年回忆,其他的游戏就等待大家自己去尝试吧!

安装

只需一行代码即可完成安装。

pip install freegames

使用方式

freegames 可直接通过命令行运行。

查看帮助

python -m freegames --help
'''
usage: freegames [-h] {list,copy,show} ...

Free Python Games

positional arguments:
  {list,copy,show}  sub-command help
    list            list games
    copy            copy game source code
    show            show game source code

optional arguments:
  -h, --help        show this help message and exit

Copyright 2022 Grant Jenks
'''

列出所有游戏

python -m freegames list
'''
ant、bagels、bounce、cannon、connect、
crypto、fidget、flappy、guess、life、
madlibs、maze、memory、minesweeper、pacman、
paint、pong、simonsays、snake、tictactoe、
tiles、tron
'''

运行游戏
所有的游戏都可以通过名称直接运行。

python -m freegames.paint

这里演示的是一个绘图小游戏,它会以两次鼠标点击的位置为端点,用线将其连接起来。

将代码copy到本地
如果想修改游戏的源代码。可以通过copy命令,它会在当前目录中创建一个py文件。

python -m freegames copy paint

运行修改后的文件
1.直接运行

python paint.py

2.通过内置的IDLE文本编辑器,它也可以执行Python代码。

python -m idlelib.idle paint.py

详细的介绍请访问官方文档,介绍得非常详细,对源码感兴趣的可以自己尝试修改,添加自己的风格

项目地址:

https://github.com/grantjenks/free-python-games

END


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/146467
 
313 次点击