0


Python 零代码的22个小游戏集合 freegames

freegames

一、简介

  • 简介:零代码的22个小游戏集合
  • 作者:Grant Jenks
  • 版本:2.4.0
  • 安装:
  1. D:\>pip install freegames -i https://pypi.tuna.tsinghua.edu.cn/simple/
  2. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
  3. Collecting freegames
  4. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/62/f5/643ebe95085f1fea2
  5. d8e4597259d8c56a920df1ed10dcfb65d7b80caff4f/freegames-2.4.0-py3-none-any.whl (10
  6. 8 kB)
  7. ------------------------------------ 109.0/109.0 kB 528.1 kB/s eta 0:00:00
  8. Installing collected packages: freegames
  9. Successfully installed freegames-2.4.0
  • 简要说明:

DESCRIPTION
Free Python Games is an Apache2 licensed collection of free Python games
intended for education and fun. The games are written in simple Python code and
designed for experimentation and changes. Simplified versions of several
classic arcade games are included.

  1. Python is one of the top-five most popular programming languages in the world
  2. and available for free from www.python.org. Python includes an extensive
  3. Standard Library distributed with your installation. The Standard Library has a
  4. module called Turtle which is a popular way to introduce programming to
  5. kids. Turtle was part of the original Logo programming language developed by
  6. Wally Feurzig and Seymour Papert in 1966. All of the games in Free Python Games
  7. are implemented using Python and its Turtle module.
  8. Starting in 2012, Free Python Games began as an after school program to teach
  9. programming to inner-city youth. The goal was to have fun as much as it was to
  10. learn. Since then the games have been improved and used in a variety of
  11. settings ranging from classrooms to summer day-camps.
  12. The games run anywhere Python can be installed which includes desktop computers
  13. running Windows, Mac OS, or Linux and older or low-power hardware such as the
  14. Raspberry Pi. Kids across the United States in grades 6th-12th have enjoyed
  15. learning about topics such as encryption and projectile motion through games.
  16. Each game is entirely independent from the others and includes comments along
  17. with a list of exercises to work through with students. Creativity and
  18. flexibility is important. There is no right or wrong way to implement a new
  19. feature or behavior! You never know which games students will engage with best.
  20. Free Python Games supports a command-line interface (CLI). Help for the CLI is
  21. available using::
  22. $ python3 -m freegames --help
  23. The CLI supports three commands: list, copy, and show. For a list of all games
  24. run::
  25. $ python3 -m freegames list
  26. Any of the listed games may be played by executing the Python module from the
  27. command-line. To reference the Python module, combine "freegames" with the name
  28. of the game. For example, to play the "snake" game run::
  29. $ python3 -m freegames.snake
  30. Games can be modified by copying their source code. The copy command will
  31. create a Python file in your local directory which you can edit. For example,
  32. to copy and play the "snake" game run::
  33. $ python3 -m freegames copy snake
  34. $ python3 snake.py
  35. Python includes a built-in text editor named IDLE which can also execute Python
  36. code. To launch the editor and make changes to the "snake" game run::
  37. $ python3 -m idlelib.idle snake.py
  • 游戏列表:
  1. D:\>python -m freegames list
  2. ant
  3. bagels
  4. bounce
  5. cannon
  6. connect
  7. crypto
  8. fidget
  9. flappy
  10. guess
  11. life
  12. madlibs
  13. maze
  14. memory
  15. minesweeper
  16. pacman
  17. paint
  18. pong
  19. simonsays
  20. snake
  21. tictactoe
  22. tiles
  23. tron

二、游戏

执行方法 freegames.游戏名

python -m freegames.life

python -m freegames.pacman

python -m freegames.cannon

python -m freegames.pong

python -m freegames.tiles

python -m freegames.maze

三、代码学习

所谓“零代码”实际上只是作者帮你写好来,拿来就用或者参考学习而已。

执行: python -m freegames copy maze,就能拷贝出源码来

(Windows系统)执行后,在当前用户的文件夹下保存有源文件: maze.py

源代码:很明显游戏是基于turtle库的代码

  1. """Maze, move from one side to another.
  2. Excercises
  3. 1. Keep score by counting taps.
  4. 2. Make the maze harder.
  5. 3. Generate the same maze twice.
  6. """
  7. from random import random
  8. from turtle import *
  9. from freegames import line
  10. def draw():
  11. """Draw maze."""
  12. color('black')
  13. width(5)
  14. for x in range(-200, 200, 40):
  15. for y in range(-200, 200, 40):
  16. if random() > 0.5:
  17. line(x, y, x + 40, y + 40)
  18. else:
  19. line(x, y + 40, x + 40, y)
  20. update()
  21. def tap(x, y):
  22. """Draw line and dot for screen tap."""
  23. if abs(x) > 198 or abs(y) > 198:
  24. up()
  25. else:
  26. down()
  27. width(2)
  28. color('red')
  29. goto(x, y)
  30. dot(4)
  31. setup(420, 420, 370, 0)
  32. hideturtle()
  33. tracer(False)
  34. draw()
  35. onscreenclick(tap)
  36. done()

再来看一个稍微复杂点的“贪吃蛇”代码:

  1. """Snake, classic arcade game.
  2. Exercises
  3. 1. How do you make the snake faster or slower?
  4. 2. How can you make the snake go around the edges?
  5. 3. How would you move the food?
  6. 4. Change the snake to respond to mouse clicks.
  7. """
  8. from random import randrange
  9. from turtle import *
  10. from freegames import square, vector
  11. food = vector(0, 0)
  12. snake = [vector(10, 0)]
  13. aim = vector(0, -10)
  14. def change(x, y):
  15. """Change snake direction."""
  16. aim.x = x
  17. aim.y = y
  18. def inside(head):
  19. """Return True if head inside boundaries."""
  20. return -200 < head.x < 190 and -200 < head.y < 190
  21. def move():
  22. """Move snake forward one segment."""
  23. head = snake[-1].copy()
  24. head.move(aim)
  25. if not inside(head) or head in snake:
  26. square(head.x, head.y, 9, 'red')
  27. update()
  28. return
  29. snake.append(head)
  30. if head == food:
  31. print('Snake:', len(snake))
  32. food.x = randrange(-15, 15) * 10
  33. food.y = randrange(-15, 15) * 10
  34. else:
  35. snake.pop(0)
  36. clear()
  37. for body in snake:
  38. square(body.x, body.y, 9, 'black')
  39. square(food.x, food.y, 9, 'green')
  40. update()
  41. ontimer(move, 100)
  42. setup(420, 420, 370, 0)
  43. hideturtle()
  44. tracer(False)
  45. listen()
  46. onkey(lambda: change(10, 0), 'Right')
  47. onkey(lambda: change(-10, 0), 'Left')
  48. onkey(lambda: change(0, 10), 'Up')
  49. onkey(lambda: change(0, -10), 'Down')
  50. move()
  51. done()

四、内置类和函数

snake游戏中使用了内置的类vector及函数square

from freegames import square, vector

除了这2个库里还有其它3个:

import freegames
freegames.all
['floor', 'line', 'path', 'square', 'vector']

使用简介

  1. CLASSES
  2. collections.abc.Sequence(collections.abc.Reversible, collections.abc.Collection)
  3. freegames.utils.vector
  4. class vector(collections.abc.Sequence)
  5. | vector(x, y)
  6. |
  7. | Two-dimensional vector.
  8. |
  9. | Vectors can be modified in-place.
  10. |
  11. | >>> v = vector(0, 1)
  12. | >>> v.move(1)
  13. | >>> v
  14. | vector(1, 2)
  15. | >>> v.rotate(90)
  16. | >>> v
  17. | vector(-2.0, 1.0)
  18. |
  19. | Method resolution order:
  20. | vector
  21. | collections.abc.Sequence
  22. | collections.abc.Reversible
  23. | collections.abc.Collection
  24. | collections.abc.Sized
  25. | collections.abc.Iterable
  26. | collections.abc.Container
  27. | builtins.object
  28. |
  29. | Methods defined here:
  30. |
  31. | __abs__(self)
  32. | v.__abs__() -> abs(v)
  33. |
  34. | >>> v = vector(3, 4)
  35. | >>> abs(v)
  36. | 5.0
  37. |
  38. | __add__(self, other)
  39. | v.__add__(w) -> v + w
  40. |
  41. | >>> v = vector(1, 2)
  42. | >>> w = vector(3, 4)
  43. | >>> v + w
  44. | vector(4, 6)
  45. | >>> v + 1
  46. | vector(2, 3)
  47. | >>> 2.0 + v
  48. | vector(3.0, 4.0)
  49. |
  50. | __eq__(self, other)
  51. | v.__eq__(w) -> v == w
  52. |
  53. | >>> v = vector(1, 2)
  54. | >>> w = vector(1, 2)
  55. | >>> v == w
  56. | True
  57. |
  58. | __getitem__(self, index)
  59. | v.__getitem__(v, i) -> v[i]
  60. |
  61. | >>> v = vector(3, 4)
  62. | >>> v[0]
  63. | 3
  64. | >>> v[1]
  65. | 4
  66. | >>> v[2]
  67. | Traceback (most recent call last):
  68. | ...
  69. | IndexError
  70. |
  71. | __hash__(self)
  72. | v.__hash__() -> hash(v)
  73. |
  74. | >>> v = vector(1, 2)
  75. | >>> h = hash(v)
  76. | >>> v.x = 2
  77. | Traceback (most recent call last):
  78. | ...
  79. | ValueError: cannot set x after hashing
  80. |
  81. | __iadd__(self, other)
  82. | v.__iadd__(w) -> v += w
  83. |
  84. | >>> v = vector(1, 2)
  85. | >>> w = vector(3, 4)
  86. | >>> v += w
  87. | >>> v
  88. | vector(4, 6)
  89. | >>> v += 1
  90. | >>> v
  91. | vector(5, 7)
  92. |
  93. | __imul__(self, other)
  94. | v.__imul__(w) -> v *= w
  95. |
  96. | >>> v = vector(1, 2)
  97. | >>> w = vector(3, 4)
  98. | >>> v *= w
  99. | >>> v
  100. | vector(3, 8)
  101. | >>> v *= 2
  102. | >>> v
  103. | vector(6, 16)
  104. |
  105. | __init__(self, x, y)
  106. | Initialize vector with coordinates: x, y.
  107. |
  108. | >>> v = vector(1, 2)
  109. | >>> v.x
  110. | 1
  111. | >>> v.y
  112. | 2
  113. |
  114. | __isub__(self, other)
  115. | v.__isub__(w) -> v -= w
  116. |
  117. | >>> v = vector(1, 2)
  118. | >>> w = vector(3, 4)
  119. | >>> v -= w
  120. | >>> v
  121. | vector(-2, -2)
  122. | >>> v -= 1
  123. | >>> v
  124. | vector(-3, -3)
  125. |
  126. | __itruediv__(self, other)
  127. | v.__itruediv__(w) -> v /= w
  128. |
  129. | >>> v = vector(2, 4)
  130. | >>> w = vector(4, 8)
  131. | >>> v /= w
  132. | >>> v
  133. | vector(0.5, 0.5)
  134. | >>> v /= 2
  135. | >>> v
  136. | vector(0.25, 0.25)
  137. |
  138. | __len__(self)
  139. | v.__len__() -> len(v)
  140. |
  141. | >>> v = vector(1, 2)
  142. | >>> len(v)
  143. | 2
  144. |
  145. | __mul__(self, other)
  146. | v.__mul__(w) -> v * w
  147. |
  148. | >>> v = vector(1, 2)
  149. | >>> w = vector(3, 4)
  150. | >>> v * w
  151. | vector(3, 8)
  152. | >>> v * 2
  153. | vector(2, 4)
  154. | >>> 3.0 * v
  155. | vector(3.0, 6.0)
  156. |
  157. | __ne__(self, other)
  158. | v.__ne__(w) -> v != w
  159. |
  160. | >>> v = vector(1, 2)
  161. | >>> w = vector(3, 4)
  162. | >>> v != w
  163. | True
  164. |
  165. | __neg__(self)
  166. | v.__neg__() -> -v
  167. |
  168. | >>> v = vector(1, 2)
  169. | >>> -v
  170. | vector(-1, -2)
  171. |
  172. | __radd__ = __add__(self, other)
  173. |
  174. | __repr__(self)
  175. | v.__repr__() -> repr(v)
  176. |
  177. | >>> v = vector(1, 2)
  178. | >>> repr(v)
  179. | 'vector(1, 2)'
  180. |
  181. | __rmul__ = __mul__(self, other)
  182. |
  183. | __sub__(self, other)
  184. | v.__sub__(w) -> v - w
  185. |
  186. | >>> v = vector(1, 2)
  187. | >>> w = vector(3, 4)
  188. | >>> v - w
  189. | vector(-2, -2)
  190. | >>> v - 1
  191. | vector(0, 1)
  192. |
  193. | __truediv__(self, other)
  194. | v.__truediv__(w) -> v / w
  195. |
  196. | >>> v = vector(1, 2)
  197. | >>> w = vector(3, 4)
  198. | >>> w / v
  199. | vector(3.0, 2.0)
  200. | >>> v / 2
  201. | vector(0.5, 1.0)
  202. |
  203. | copy(self)
  204. | Return copy of vector.
  205. |
  206. | >>> v = vector(1, 2)
  207. | >>> w = v.copy()
  208. | >>> v is w
  209. | False
  210. |
  211. | move(self, other)
  212. | Move vector by other (in-place).
  213. |
  214. | >>> v = vector(1, 2)
  215. | >>> w = vector(3, 4)
  216. | >>> v.move(w)
  217. | >>> v
  218. | vector(4, 6)
  219. | >>> v.move(3)
  220. | >>> v
  221. | vector(7, 9)
  222. |
  223. | rotate(self, angle)
  224. | Rotate vector counter-clockwise by angle (in-place).
  225. |
  226. | >>> v = vector(1, 2)
  227. | >>> v.rotate(90)
  228. | >>> v == vector(-2, 1)
  229. | True
  230. |
  231. | scale(self, other)
  232. | Scale vector by other (in-place).
  233. |
  234. | >>> v = vector(1, 2)
  235. | >>> w = vector(3, 4)
  236. | >>> v.scale(w)
  237. | >>> v
  238. | vector(3, 8)
  239. | >>> v.scale(0.5)
  240. | >>> v
  241. | vector(1.5, 4.0)
  242. |
  243. | ----------------------------------------------------------------------
  244. | Data descriptors defined here:
  245. |
  246. | x
  247. | X-axis component of vector.
  248. |
  249. | >>> v = vector(1, 2)
  250. | >>> v.x
  251. | 1
  252. | >>> v.x = 3
  253. | >>> v.x
  254. | 3
  255. |
  256. | y
  257. | Y-axis component of vector.
  258. |
  259. | >>> v = vector(1, 2)
  260. | >>> v.y
  261. | 2
  262. | >>> v.y = 5
  263. | >>> v.y
  264. | 5
  265. |
  266. | ----------------------------------------------------------------------
  267. | Data and other attributes defined here:
  268. |
  269. | PRECISION = 6
  270. |
  271. | __abstractmethods__ = frozenset()
  272. |
  273. | ----------------------------------------------------------------------
  274. | Methods inherited from collections.abc.Sequence:
  275. |
  276. | __contains__(self, value)
  277. |
  278. | __iter__(self)
  279. |
  280. | __reversed__(self)
  281. |
  282. | count(self, value)
  283. | S.count(value) -> integer -- return number of occurrences of value
  284. |
  285. | index(self, value, start=0, stop=None)
  286. | S.index(value, [start, [stop]]) -> integer -- return first index of value.
  287. | Raises ValueError if the value is not present.
  288. |
  289. | Supporting start and stop arguments is optional, but
  290. | recommended.
  291. |
  292. | ----------------------------------------------------------------------
  293. | Class methods inherited from collections.abc.Reversible:
  294. |
  295. | __subclasshook__(C) from abc.ABCMeta
  296. | Abstract classes can override this to customize issubclass().
  297. |
  298. | This is invoked early on by abc.ABCMeta.__subclasscheck__().
  299. | It should return True, False or NotImplemented. If it returns
  300. | NotImplemented, the normal algorithm is used. Otherwise, it
  301. | overrides the normal algorithm (and the outcome is cached).
  302. FUNCTIONS
  303. floor(value, size, offset=200)
  304. Floor of `value` given `size` and `offset`.
  305. The floor function is best understood with a diagram of the number line::
  306. -200 -100 0 100 200
  307. <--|--x--|-----|--y--|--z--|-->
  308. The number line shown has offset 200 denoted by the left-hand tick mark at
  309. -200 and size 100 denoted by the tick marks at -100, 0, 100, and 200. The
  310. floor of a value is the left-hand tick mark of the range where it lies. So
  311. for the points show above: ``floor(x)`` is -200, ``floor(y)`` is 0, and
  312. ``floor(z)`` is 100.
  313. >>> floor(10, 100)
  314. 0.0
  315. >>> floor(120, 100)
  316. 100.0
  317. >>> floor(-10, 100)
  318. -100.0
  319. >>> floor(-150, 100)
  320. -200.0
  321. >>> floor(50, 167)
  322. -33.0
  323. line(a, b, x, y)
  324. Draw line from `(a, b)` to `(x, y)`.
  325. path(filename)
  326. Return full path to `filename` in freegames module.
  327. square(x, y, size, name)
  328. Draw square at `(x, y)` with side length `size` and fill color `name`.
  329. The square is oriented so the bottom left corner is at (x, y).

另外还有20段代码,你可以用命令自己copy出来一一学习。总体来说,代码难度不是很高,重要的是要自己动手模仿编出新的游戏来!

标签: python 开发语言

本文转载自: https://blog.csdn.net/boysoft2002/article/details/126062084
版权归原作者 Hann Yang 所有, 如有侵权,请联系我们删除。

“Python 零代码的22个小游戏集合 freegames”的评论:

还没有评论