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>
大概看了下,如果不调用包吧。你至少知道怎么搞gui。python主要的Gui是pyqt,tkinter,GKT,wxpython。 第一、第五个重点参考吧。你同时再知道一个随机数的生成方式。至于调用包写的话,推荐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}]
不懂python,水一下,做游戏界面为什么用python

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
