Python Using Turtle Drawing to Realize Snake Game

  • 2021-11-13 02:09:20
  • OfStack

In the implementation of Snake Game, first need to use python built-in turtle module to create game form and keyboard key monitoring work, and then use freegames module for Snake drawing work.

1. turtle module

The turtle module is built-in to python, so you need to import it using import before using it. The code is as follows:


import turtle

(1) The game form can be created using the setup () method in the turtle module. The syntax format is as follows:


turtle.setup(width=_CFG["width"],height=_CFG["height"],startx=_CFG["leftright"],starty=_CFG["topbottom"])

The parameters are described as follows:

width: Represents the width of the form, in pixels if it is an integer, or a fraction of the screen if it is floating; The default value is 50% of the screen. height: Represents the height of the form, in pixels if it is an integer, or a fraction of the screen if it is floating; The default value is 75% of the screen. startx: Indicates that the form is displayed in the horizontal position of the screen. If it is positive, it starts from the left edge of the screen in pixels, if it is negative, it starts from the right edge, and if it is not, it centers the window horizontally. starty: Indicates that the form is displayed in the vertical position of the screen, starting in pixels at the top edge of the screen if positive, starting at the bottom edge if negative, and centering the window vertically if not.

The key code for creating a game form is as follows:


import turtle      #  Import Drawing Turtle Module 
turtle.setup(420, 420)      #  Create form size 
turtle.done()       #  Stops brush drawing, but the drawing form does not close 

(2) The onkey () method in turtle module can be used to monitor the keys in the corresponding keyboard. The syntax format is as follows:


turtle.onkey(fun,key)

The parameters are described as follows:

fun: Represents the method that needs to be executed. key: This parameter is a string that represents the corresponding key in the keyboard, such as "a" or "space".

Note: When using the onkey () method to monitor the keyboard, you also need to call the turtle. listen () method to monitor the keyboard keys.

The key codes for monitoring the upper, lower, left and right keys in the keyboard are as follows:


turtle.listen()                        #  Event listener 
turtle.onkey(lambda: change(10, 0), 'Right')    #  Press the right key of the keyboard and the snake goes to the right 
turtle.onkey(lambda: change(-10, 0), 'Left')    #  Press the left key of the keyboard and the snake goes to the left 
turtle.onkey(lambda: change(0, 10), 'Up')       #  Press the key on the keyboard and the snake goes up 
turtle.onkey(lambda: change(0, -10), 'Down')    #  Press the keys on the keyboard and the snake goes down 

2. freegames module

The freegames module is a third-party module, so you need to install the module through the following command before using it:


pip install freegames

After the module is installed, you first need to import the vector and square functions in the freegames module. The code is as follows:


from freegames import vector,square  #  Import Vector Class and Drawing Function 

(1) The vector () class in the freegames module can create a 2-D vector object based on the specified x and y coordinates. The syntax format of the vector () class is as follows:


class freegames.vector(x,y)

The parameters are described as follows:

x: Represents horizontal coordinates. y: Represents vertical coordinates.

Realize the definition of food position, snake position and snake moving position and distance in the game. The key code is as follows:


food = vector(0,0)        #  Food 
snake = [vector(10, 0)]   #  Snake 
position = vector(0, -10) #  Move the position, 1 The distance of steps is 10

(2) Through square function in freegames module, a square with specified side length can be drawn at (x, y) and filled with specified color. The syntax format of the square () function is as follows:


freegames.square(x,y,size,name)

The parameters are described as follows:

x: Represents horizontal coordinates. y: Represents vertical coordinates. size: Represents the side length of the drawn square. name: Represents the name of the fill color.

Realize drawing food and snakes in the game. The key code is as follows:


turtle.setup(width=_CFG["width"],height=_CFG["height"],startx=_CFG["leftright"],starty=_CFG["topbottom"])
0

Instances


turtle.setup(width=_CFG["width"],height=_CFG["height"],startx=_CFG["leftright"],starty=_CFG["topbottom"])
1

For more wonderful articles about python games, please click to view the following topics:

python Tetris Game Collection

python Classic Games Summary

python WeChat Jump 1 Jump Game Collection

More interesting classic game implementation topics, share with you:

C + + Classic Games Summary

JavaScript classic games can't stop playing

java Classic Games Summary

javascript Classic Games Summary


Related articles: