怎样从零开始用 Python 写一个贪吃蛇?
PS: 感觉好像被当成伸手党了 ,题主我一开始也是有个想法的,只是感觉运行效率太渣,来学习学习各位的方法。
还有现在忙着高考,也没多少时间实践这个贪吃蛇程序了。 但还是感谢各位提出的思路。
==--------以下为原问题--------==
首先说明“从零开始”不是指零基础
首先说明“从零开始”不是指零基础
首先说明“从零开始”不是指零基础
是指从头写起。
1. 使用现有模块的话有什么模块推荐?
2. 不使用现有模块的话怎么自己从底层开始写?需要具备哪些知识?
回复内容:
主要分为3个部分:- 用一个线程控制游戏的逻辑(如何移动蛇、何时会输等)
- 用GUI库把当前局面输出到屏幕上
- 想办法把用户的输入传递到控制线程上
第一部分:贪吃蛇游戏的逻辑首先,如何存储游戏的局面呢?

<span class="n">gx</span><span class="p">,</span> <span class="n">gy</span> <span class="o">=</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">15</span> <span class="c">#棋盘大小</span> <span class="n">EMPTY</span><span class="p">,</span> <span class="n">SNAKE</span><span class="p">,</span> <span class="n">FRUIT</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span> <span class="c">#网格状态</span> <span class="n">g</span> <span class="o">=</span> <span class="p">[[</span><span class="n">EMPTY</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">gx</span><span class="p">)]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">gy</span><span class="p">)]</span> <span class="c">#棋盘</span>
至于调用包写的话,推荐pygame,random够了。一百行左右的代码量吧。 pygame
100来行就可以了

源码:
codetimer/Snake 贪吃蛇只需要有如下基本的 IO 功能就可以写了:
- 在指定坐标写/画一个块
- 不阻塞不回显地读键盘
Console 下面能写,GUI 也可以。甚至 Console 需要的功能还少些。
所以最简单就是用标准库 curses 写个 Console 的贪吃蛇。curses 入门可以看官方的 How to:
Curses Programming with Python
如果用 Windows,可能需要下载安装非官方的 curses 包:http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses
而如果用 GUI,效果会好些,概念也要多些。我想 pygame 是很应景的东西,用起来比较方便,网站上有教程。
注意 IO 是操作系统提供的功能,由库包装一下系统功能来提供上面两个 IO 操作就已经算是从零开始了。如果你觉得用 curses 还不够“从零开始”,那么你就得针对不同操作系统来写了。例如对于 Windows,就是调用诸如 SetConsoleCursorPosition、WriteConsole 等 API 完成基本的输出,调用 SetConsoleMode、ReadConsole 之类函数处理输入。用 C 写贪吃蛇倒常有人这么干,Python 没见过这样的。 一、前言
看了提问者在另一个回答的评论

真的觉得提问者应该试试pygame。“工欲善其事,必先利其器。”个人认为,python与其他编程语言相比,亮点在于它有丰富的第三方库。比如用python做爬虫,原生库urllib2和第三方库requests相比,哪个效率更高?
二、推荐
pygame顾名思义,python和game相结合,专注于2D游戏开发。
pygame很容易入门。网上有一些不错的教程,在此推荐我的入门教程:
-Daydayblog-第1页
博客提供了16个章节:

没错,博客作者提供了如何使用pygame开发贪吃蛇的教程,作者程序运行界面如下:

三、贪吃蛇
贪吃蛇主要包括以下几个方面:
- 物体表示:蛇和苹果如何存储和显示、蛇如何移动和吃苹果变长;
- 字体显示:提示玩家该如何操作,显示玩家的分数;
- 人机交互:使用键盘的方向键来控制蛇的移动;
- 矩形绘图:绘制各种基本的图形来组成图像。
使用pygame开发贪吃蛇,您只需要学会:
- pygame简介及安装
- surface对象
- 窗口显示
- 绘制几何图形
- event事件
- 人机交互
蛇可通过python字典存储,
snake=[{'x': startX , 'y': startY}, {'x': startX - 1 * cellSize, 'y': startY}, {'x': startX - 2 * cellSize, 'y': startY}, {'x': startX - 3 * cellSize, 'y': startY}]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PS serial numbers cs5 permanent free 2020 include: 1. 1330-1384-7388-4265-2355-8589 (Chinese); 2. 1330-1409-7892-5799-0412-7680 (Chinese); 3. 1330-1616-1993 -8375-9492-6951 (Chinese); 4. 1330-1971-2669-5043-0398-7801 (Chinese), etc.

Solution to error 160 when ps installation cannot write registry value: 1. Check whether there is 2345 software on the computer, and if so, uninstall the software; 2. Press "Win+R" and enter "Regedit" to open the system registry, and then Find "Photoshop.exe" and delete the Photoshop item.

How to fix the PS interface font that is too small: 1. Open PS, click the "Edit" button on the top menu bar of PS to expand the editing menu; 2. Click the "Preferences" button in the expanded editing menu, and then click "Interface. .." button; 3. In the interface settings, set the user interface font size to "Large", set the UI scaling to "200%" and save the settings, then restart PS to take effect.

Steps to delete the selected area in PS: 1. Open the picture you want to edit; 2. Use the appropriate tool to create a selection; 3. You can use a variety of methods to delete the content in the selection, use the "Delete" key, use the "Healing Brush Tool" , use "Content-Aware Fill", use the "Stamp Tool", etc.; 4. Use tools to repair any obvious traces or defects to make the picture look more natural; 5. After completing editing, click "File" > in the menu bar "Save" to save the editing results.

1. Open the software and import a piece of material, as shown in the picture below. 2. Then ctrl+shift+u to remove color. 3. Then press ctrl+J to copy the layer. 4. Then reverse ctrl+I, and then set the layer blending mode to Color Dodge. 5. Click Filter--Others--Minimum. 6. In the pop-up dialog box, set the radius to 2 and click OK. 7. Finally, you can see the line draft extracted.

In the PS copy layer shortcut keys, we can know that if you want to copy a layer when using PS, you can use the shortcut key [Ctrl+J] for quick copying. This introduction to the shortcut keys for copying layers can tell you the specific operation method. The following is the detailed content, so take a look. PS copy layer shortcut key answer: [Ctrl+J] Specific method: 1. Open the image in PS and select the layer that needs to be copied. 2. Press [Ctrl+J] on the keyboard at the same time to complete the copy of the layer. Other copying methods: 1. After opening the image, press and hold the layer and move the [New Layer] icon downwards. 2. After moving to the icon, let go. 3. The layer copy is completed.

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

1. Ctrl + N: Create a new document. 2. Ctrl + O: Open a file. 3. Ctrl + S: Save the current file. 4. Ctrl + Shift + S: Save as. 5. Ctrl + W: Close the current document. 6. Ctrl + Q: Exit Photoshop. 7. Ctrl + Z: Undo. 8. Ctrl + Y: Redo. 9. Ctrl + X: Cut the selected content. 10. Ctrl + C: Copy the selected content.
