python freegames配合turtle 实现一些简单游戏

github参考地址

真·入门级 Python 游戏集合库。贪吃蛇、迷宫、Pong、猜字等,运行简单(有些不需要安装依赖)、代码少且易读。用游戏开启的你 Python 项目之旅,玩完再学源码

下载模块 主要的

pip install freegames
pip install turtle

第一个 蚂蚁爬爬

"""Ant, simple animation demo.
蚂蚁随机爬

"""

from random import *
from turtle import *
from freegames import vector

ant = vector(0, 0)
aim = vector(2, 0)


def wrap(value):
    """Wrap value around -200 and 200."""
    if value > 200:
        return 0
    elif value < -200:
        return 200
    else:
        return value  


def draw():
    """Move ant and draw screen."""
    color('blue')  # 设置颜色
    ant.move(aim)
    ant.x = wrap(ant.x)
    ant.y = wrap(ant.y)
    aim.move(random() - 0.5)
    aim.rotate(random() * 10 - 5)

    # clear()   清屏 不请会留下轨迹
    goto(ant.x, ant.y)
    dot(4)
    ontimer(draw, 10)


setup(420, 420, 370, 0)  # 设置屏幕大小
hideturtle()  # turtle  隐藏海龟
tracer(False)  # 为了加快作图的,你可以在里面添加参数,比如:turtle.tracer(False)图形将一次性画好
up()   # 开始
draw()
done()  # 停止绘画  但不关闭窗口

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值