Code example for writing snake games in Python

Source: Internet
Author: User
Tags switch case
This article describes how to write code for a Python snake game, which has some reference value, interested friends can refer to this article for details on the compilation of Python snake games, which has some reference value. interested friends can refer

I recently learned Python and want to do something to train my hands. the command line greedy snake is generally a C practice program, but I can't find anything else at the moment. I want to practice simple syntax first.

Because Python is very troublesome to listen to the keyboard, it does not have kbhit () in C language, so this greedy snake won't act on its own. The running effect is as follows:

Requirements:# Represents the border, * represents the food, o represents the snake's body, O represents the snake's head, and wsad is used to move

Python version: 3.6.1

System environment: Win10

Class:

Board: board, that is, the game area

Snake: a snake that records the status of a snake by recording each point in the body.

Game

I still wanted a food class, but only one coordinate is required for food and a new one. so I simply used list to save the coordinates and put the new food in the game, which is not too difficult logically.

Source code:

# Write By Guobao #2017/4 // 7 # greedy snake # Use # for boundary, * for food, o for body and header # python 3.6.1import copyimport randomimport osimport msvcrt # the board class, used to put everythingclass board: _ points = [] def _ init _ (self): self. _ points. clear () for I in range (22): line = [] if I = 0 or I = 21: for j in range (22): line. append ('#') else: line. append ('#') for j in range (20): line. append ('') line. append ('#') self. _ point S. append (line) def getPoint (self, location): return self. _ points [location [0] [location [1] def clear (self): self. _ points. clear () for I in range (22): line = [] if I = 0 or I = 21: for j in range (22): line. append ('#') else: line. append ('#') for j in range (20): line. append ('') line. append ('#') self. _ points. append (line) def put_snake (self, snail ke_locations): # clear the board self. clear () # put t He snake points for x in snail ke_locations: self. _ points [x [0] [x [1] = 'O' # the head x = snail ke_locations [len (snail ke_locations)-1] self. _ points [x [0] [x [1] = 'O' def put_food (self, food_location): self. _ points [food_location [0] [food_location [1] = '*' def show (self): OS. system ("cls") for I in range (22): for j in range (22): print (self. _ points [I] [j], end = '') print () # the snake classclass snake :_ _ Points = [] def _ init _ (self): for I in range (1, 6): self. _ points. append ([1, I]) def getPoints (self): return self. _ points # move to the next position # give the next head def move (self, next_head): self. _ points. pop (0) self. _ points. append (next_head) # eat the food # give the next head def eat (self, next_head): self. _ points. append (next_head) # calc the next state # and return the direction de F next_head (self, direction = 'default'): # need to change the value, so copy it head = copy. deepcopy (self. _ points [len (self. _ points)-1]) # calc the "default" ction if direction = 'default': neck = self. _ points [len (self. _ points)-2] if neck [0]> head [0]: ction = 'Up' elif neck [0] 

Notes:

1. Python does not have a Switch case statement and can be implemented using dirt.

2. Python = is a copy, copy reference, deep copy requires the use of the copy deepcopy () function to achieve

3. use self to access member variables even in member functions, which is very different from C ++ and JAVA.

The above is the details of the code example for the snake game written in Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.