From 51fdda69f704289f7e6a33d359926eb6ec209008 Mon Sep 17 00:00:00 2001 From: sairate Date: Sat, 7 Jun 2025 14:13:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B4=AA=E5=90=83?= =?UTF-8?q?=E8=9B=87=E6=B8=B8=E6=88=8F-=20=E6=96=B0=E5=A2=9E=E8=B4=AA?= =?UTF-8?q?=E5=90=83=E8=9B=87=E6=B8=B8=E6=88=8F=E7=9A=84=20Python=20?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20-=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E5=AE=89=E8=A3=85=20freegames=20=E5=BA=93=E7=9A=84=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E5=88=B0=20README.md=20-=20=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=20.idea=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BB=A5=E8=AE=BE=E7=BD=AE=20IDE=20=E9=A1=B9=E7=9B=AE=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=20-=20=E9=85=8D=E7=BD=AE=20.gitignore=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BB=A5=E5=BF=BD=E7=95=A5=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84=20IDE=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 + .idea/freegames.iml | 10 +++ .idea/inspectionProfiles/Project_Default.xml | 14 ++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/material_theme_project_new.xml | 10 +++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ README.md | 3 + snack.py | 82 +++++++++++++++++++ 10 files changed, 149 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/freegames.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/material_theme_project_new.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 snack.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/freegames.iml b/.idea/freegames.iml new file mode 100644 index 0000000..4c26dad --- /dev/null +++ b/.idea/freegames.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..25bde2c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 0000000..8c3702f --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..92ae53e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c9befd2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..139aeda --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +```bash +pip install -i https://mirrors.aliyun.com/pypi/simple/ freegames +``` \ No newline at end of file diff --git a/snack.py b/snack.py new file mode 100644 index 0000000..e2d5c47 --- /dev/null +++ b/snack.py @@ -0,0 +1,82 @@ +"""Snake,经典街机游戏。 + +练习: +1. 如何让蛇变快或变慢? +2. 如何让蛇可以从一边穿越到另一边? +3. 如何让食物移动? +4. 修改为点击鼠标控制蛇移动。 +""" + +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): + """改变蛇的移动方向。""" + aim.x = x + aim.y = y + + +def inside(head): + """判断蛇头是否在边界内。""" + return -200 < head.x < 190 and -200 < head.y < 190 + + +def move(): + """让蛇向前移动一个单位。""" + 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('蛇长:', 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) # 每100毫秒调用一次 move,实现动画 + + +# 初始化窗口 +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()