PS: Feel as if to reach the party, the title of the Lord I have an idea at the beginning, just feel the running efficiency is too slag, to learn the methods of everyone.
There are now busy with the college entrance examination, there is not much time to practice the snake program. But thank you for all the ideas you have put forward.
= =--------The following is the original problem--------= =
First, "zero-based" does not mean 0 basis
First, "zero-based" does not mean 0 basis
First, "zero-based" does not mean 0 basis
It means writing from scratch.
1. What modules are recommended for use with existing modules?
2. How do I start writing from the bottom without using an existing module? What do you need to know?
Reply content:
Mainly divided into 3 parts:
- Use a thread to control the logic of the game (how to move snakes, when to lose, etc.)
- Use the GUI library to output the current situation to the screen
- Try to pass the user's input to the control thread
the
first part:
the
logic of Snake game
First of all, how to store the game situation?
The whole face of a snake is a gy*gx The whole face of a snake is a gy*gx
GridOf
Chess Board, where each grid may have
the body of the snake (yellow in the picture)、
food (pictured in green)、
Nothing at all (blue in the picture)Three different states. This is how it is described in Python:
GX, Gy = the, the #棋盘大小EMPTY, SNAKE, FRUIT = 1,2,3 #网格状态g = [[EMPTY for x inch Range(GX)] for y inch Range(Gy)] #棋盘
Probably looked under, if not to call the package. At least you know how to do the GUI. The main GUI of Python is Pyqt,tkinter,gkt,wxpython. First, the fifth point of reference it. You'll also know how to generate a random number.
As for the call package write, recommended pygame,random enough. 100 lines or so of the amount of code it. Pygame
100来, it's fine.
Source:
Codetimer/snake
The snake only needs to have the following basic IO function to write:
- Write/Draw a block in the specified coordinates
- Do not block and read the keyboard back explicitly
It can be written under the Console, as well as the GUI. Even the Console needs less functionality.
So the simplest is to use standard library curses write a Console of the snake. Curses getting started can look at the official how to:
Curses Programming with Python
If you are using Windows, you may need to download and install the unofficial curses package:
http://www. Lfd.uci.edu/~gohlke/pyt honlibs/#curses
And if you use a GUI, the effect will be better, the concept will be more. I want to pygame.
is a very occasional thing, it is more convenient to use, the site has a tutorial.
Note that IO is a function provided by the operating system, and it is zero-based that the library wraps the system functions to provide the above two IO operations. If you think that using curses is not enough to "start from scratch", then you have to write for different operating systems. For example, Windows calls the API, such as SetConsoleCursorPosition, Writeconsole, to complete the basic output, calling functions such as Setconsolemode, Readconsole, and so on to handle the input. Write a snake in C often people do so, Python has not seen this.
First, preface
Read the comments of the questioner in another answer
Really think the questioner should try Pygame. "工欲善其事, its prerequisite. "Personally, Python is a bright spot in comparison to other programming languages because of its rich third-party libraries," he said. For example, using Python as a crawler, the original library urllib2 and third-party library requests compared to which more efficient?
Second, recommended
Pygame, as the name implies, is a combination of Python and game, focusing on 2D game development.
Pygame is easy to get started. There are some good tutorials on the web that recommend my introductory tutorial:
-daydayblog-1th Page
The blog provides 16 chapters:
Yes, the blog author provides a tutorial on how to use Pygame to develop a snake, and the author program runs the following interface:
third, the greedy snake
The snake mainly includes the following aspects:
- Objects show how snakes and apples store and display, how snakes move and eat apples to grow longer;
- Font display: Prompts the player how to operate, showing the player's score;
- Human-Computer Interaction: Use the keyboard's arrow keys to control the movement of snakes;
- Rectangle drawing: Draws a variety of basic graphics to compose the image.
To develop a snake with pygame, you only need to learn:
- Pygame Introduction and Installation
- Surface Object
- Window display
- Drawing geometry
- Event Events
- Interactive
Snakes can be stored through a Python dictionary,
snake=[{'x': startX , 'y': startY}, {'x': startX - 1 * cellSize, 'y': startY}, {'x': startX - 2 * cellSize, 'y': startY}, {'x': startX - 3 * cellSize, 'y': startY}]
Do not know python, water, do the game interface why use Python