Python 零代码的22个小游戏集合 freegames_没安装python能写的游戏代码有哪些(2)

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

of the game. For example, to play the “snake” game run::
    
      $ python3 -m freegames.snake
    
    Games can be modified by copying their source code. The copy command will
    create a Python file in your local directory which you can edit. For example,
    to copy and play the “snake” game run::
    
      $ python3 -m freegames copy snake
      $ python3 snake.py
    
    Python includes a built-in text editor named IDLE which can also execute Python
    code. To launch the editor and make changes to the “snake” game run::
    
      $ python3 -m idlelib.idle snake.py

  • 游戏列表:
D:\>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

二、游戏

执行方法 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库的代码

"""Maze, move from one side to another.

Excercises

1. Keep score by counting taps.
2. Make the maze harder.
3. Generate the same maze twice.
"""

from random import random
from turtle import *

from freegames import line


def draw():
    """Draw maze."""
    color('black')
    width(5)

    for x in range(-200, 200, 40):
        for y in range(-200, 200, 40):
            if random() > 0.5:
                line(x, y, x + 40, y + 40)
            else:
                line(x, y + 40, x + 40, y)

    update()


def tap(x, y):
    """Draw line and dot for screen tap."""
    if abs(x) > 198 or abs(y) > 198:
        up()
    else:
        down()

    width(2)
    color('red')
    goto(x, y)
    dot(4)


setup(420, 420, 370, 0)
hideturtle()
tracer(False)
draw()
onscreenclick(tap)
done()

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

"""Snake, classic arcade game.

Exercises

1. How do you make the snake faster or slower?
2. How can you make the snake go around the edges?
3. How would you move the food?
4. Change the snake to respond to mouse clicks.
"""

from random import randrange
from turtle import *

from freegames import square, vector

food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)


def change(x, y):
    """Change snake direction."""
    aim.x = x
    aim.y = y


def inside(head):
    """Return True if head inside boundaries."""
    return -200 < head.x < 190 and -200 < head.y < 190


def move():
    """Move snake forward one segment."""
    head = snake[-1].copy()
    head.move(aim)

    if not inside(head) or head in snake:
        square(head.x, head.y, 9, 'red')
        update()
        return

    snake.append(head)

    if head == food:
        print('Snake:', len(snake))
        food.x = randrange(-15, 15) * 10
        food.y = randrange(-15, 15) * 10
    else:
        snake.pop(0)

    clear()

    for body in snake:
        square(body.x, body.y, 9, 'black')

    square(food.x, food.y, 9, 'green')
    update()
    ontimer(move, 100)


setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()

四、内置类和函数

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

from freegames import square, vector

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

import freegames
freegames.__all__
[‘floor’, ‘line’, ‘path’, ‘square’, ‘vector’]

使用简介

CLASSES
    collections.abc.Sequence(collections.abc.Reversible, collections.abc.Collection)
        freegames.utils.vector
    
    class vector(collections.abc.Sequence)
     |  vector(x, y)
     |  
     |  Two-dimensional vector.
     |  
     |  Vectors can be modified in-place.
     |  
     |  >>> v = vector(0, 1)
     |  >>> v.move(1)
     |  >>> v
     |  vector(1, 2)
     |  >>> v.rotate(90)
     |  >>> v
     |  vector(-2.0, 1.0)
     |  
     |  Method resolution order:
     |      vector
     |      collections.abc.Sequence
     |      collections.abc.Reversible
     |      collections.abc.Collection
     |      collections.abc.Sized
     |      collections.abc.Iterable
     |      collections.abc.Container
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  __abs__(self)
     |      v.__abs__() -> abs(v)
     |      
     |      >>> v = vector(3, 4)
     |      >>> abs(v)
     |      5.0
     |  
     |  __add__(self, other)
     |      v.__add__(w) -> v + w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v + w
     |      vector(4, 6)
     |      >>> v + 1
     |      vector(2, 3)
     |      >>> 2.0 + v
     |      vector(3.0, 4.0)
     |  
     |  __eq__(self, other)
     |      v.__eq__(w) -> v == w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(1, 2)
     |      >>> v == w
     |      True
     |  
     |  __getitem__(self, index)
     |      v.__getitem__(v, i) -> v[i]
     |      
     |      >>> v = vector(3, 4)
     |      >>> v[0]
     |      3
     |      >>> v[1]
     |      4
     |      >>> v[2]
     |      Traceback (most recent call last):
     |          ...
     |      IndexError
     |  
     |  __hash__(self)
     |      v.__hash__() -> hash(v)
     |      
     |      >>> v = vector(1, 2)
     |      >>> h = hash(v)
     |      >>> v.x = 2
     |      Traceback (most recent call last):
     |          ...
     |      ValueError: cannot set x after hashing
     |  
     |  __iadd__(self, other)
     |      v.__iadd__(w) -> v += w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v += w
     |      >>> v
     |      vector(4, 6)
     |      >>> v += 1
     |      >>> v
     |      vector(5, 7)
     |  
     |  __imul__(self, other)
     |      v.__imul__(w) -> v *= w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v *= w
     |      >>> v
     |      vector(3, 8)
     |      >>> v *= 2
     |      >>> v
     |      vector(6, 16)
     |  
     |  __init__(self, x, y)
     |      Initialize vector with coordinates: x, y.
     |      
     |      >>> v = vector(1, 2)
     |      >>> v.x
     |      1
     |      >>> v.y
     |      2
     |  
     |  __isub__(self, other)
     |      v.__isub__(w) -> v -= w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v -= w
     |      >>> v
     |      vector(-2, -2)
     |      >>> v -= 1
     |      >>> v
     |      vector(-3, -3)
     |  
     |  __itruediv__(self, other)
     |      v.__itruediv__(w) -> v /= w
     |      
     |      >>> v = vector(2, 4)
     |      >>> w = vector(4, 8)
     |      >>> v /= w
     |      >>> v
     |      vector(0.5, 0.5)
     |      >>> v /= 2
     |      >>> v
     |      vector(0.25, 0.25)
     |  
     |  __len__(self)
     |      v.__len__() -> len(v)
     |      
     |      >>> v = vector(1, 2)
     |      >>> len(v)
     |      2
     |  
     |  __mul__(self, other)
     |      v.__mul__(w) -> v * w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v * w
     |      vector(3, 8)
     |      >>> v * 2
     |      vector(2, 4)
     |      >>> 3.0 * v
     |      vector(3.0, 6.0)
     |  
     |  __ne__(self, other)
     |      v.__ne__(w) -> v != w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v != w
     |      True
     |  
     |  __neg__(self)
     |      v.__neg__() -> -v
     |      
     |      >>> v = vector(1, 2)
     |      >>> -v
     |      vector(-1, -2)
     |  
     |  __radd__ = __add__(self, other)
     |  
     |  __repr__(self)
     |      v.__repr__() -> repr(v)
     |      
     |      >>> v = vector(1, 2)
     |      >>> repr(v)
     |      'vector(1, 2)'
     |  
     |  __rmul__ = __mul__(self, other)
     |  
     |  __sub__(self, other)
     |      v.__sub__(w) -> v - w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v - w
     |      vector(-2, -2)
     |      >>> v - 1
     |      vector(0, 1)
     |  
     |  __truediv__(self, other)
     |      v.__truediv__(w) -> v / w
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> w / v
     |      vector(3.0, 2.0)
     |      >>> v / 2
     |      vector(0.5, 1.0)
     |  
     |  copy(self)
     |      Return copy of vector.
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = v.copy()
     |      >>> v is w
     |      False
     |  
     |  move(self, other)
     |      Move vector by other (in-place).
     |      
     |      >>> v = vector(1, 2)
     |      >>> w = vector(3, 4)
     |      >>> v.move(w)


### 一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。



![](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)



### 二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。



![](https://img-blog.csdnimg.cn/img_convert/8c4513c1a906b72cbf93031e6781512b.png)



### 三、入门学习视频



我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。



![](https://img-blog.csdnimg.cn/afc935d834c5452090670f48eda180e0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA56iL5bqP5aqb56eD56eD,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python Freegames是一个基于Python的开源游戏库,其中包含了很多经典的小游戏,如贪吃蛇、弹球等等。如果想要复制整个代码库,可以按照以下步骤进行操作: 1. 首先,打开你所使用的Python编辑器或IDE(例如PyCharm、Spyder等),确保Python已经正确安装。 2. 在你选择的编辑器中,新建一个Python文件,取一个合适的名字,比如"freegames_code.py"。 3. 访问Python Freegames的官方网站或代码库,通常是在GitHub上进行托管。找到"freegames"这个库的页面,点击进入。 4. 在页面上找到一个绿色的按钮,一般着"Code"或"Clone",点击该按钮,然后选择"Download ZIP"将整个代码库以ZIP的形式下载到你的计算机中。 5. 找到你下载的ZIP文件,并解压缩到一个合适的文件夹中。 6. 打开解压缩后的文件夹,找到你想要复制的具体游戏Python代码文件,比如"pacman.py"。 7. 打开你之前新建的Python文件,在其中粘贴复制的代码。可以选择多个游戏代码进行复制粘贴,得到一个包含多个游戏代码文件。 8. 保存你的Python文件,并运行它。你可以通过运行该文件来体验Python Freegames中的游戏,比如贪吃蛇。 总结:通过以上步骤,你可以从Python Freegames的代码库中复制你想要的游戏代码,并进行使用和修改。注意,为了确保顺利运行游戏代码,可能需要安装一些依赖库,如Pygame。在使用代码前,可以阅读一下代码文件中的注释部分,了解具体的使用说明和注意事项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值