Use Python to write a cool snake game instance code.
I added the score display in the program. I wrote the game logic of the Snake to the class of the Snake, rather than in the class of the Snake.
Special food:
1. Green: Normal, eat to increase the body size
2. RED: reduced body size
3. golden color: after eating, the system returns to its initial shape.
4. color-changing food: the color of the snake is changed based on the food color.
# Coding = UTF-8from Tkinter import * from random import randintimport tkMessageBoxclass Grid (object): def _ init _ (self, master = None, height = 16, width = 24, offset = 10, grid_width = 50, bg = "#808080"): self. height = height self. width = width self. offset = offset self. grid_width = grid_width self. bg = bg self. canvas = Canvas (master, width = self. width * self. grid_width + 2 * self. offset, height = self. height * self. grid_width + 2 * self. offset, bg = self. bg) self. canvas. pack (side = RIGHT, fill = Y) def draw (self, pos, color,): x = pos [0] * self. grid_width + self. offset y = pos [1] * self. grid_width + self. offset # The outline attribute must match the grid background color (self. bg) Same, otherwise it will be ugly self. canvas. create_rectangle (x, y, x + self. grid_width, y + self. grid_width, fill = color, outline = self. bg) class Food (object): def _ init _ (self, grid, color = "#23D978"): self. grid = grid self. color = color self. set_pos () self. type = 1 def set_pos (self): x = randint (0, self. grid. width-1) y = randint (0, self. grid. height-1) self. pos = (x, y) def display (self): self. grid. draw (self. pos, self. color) class Snake (object): def _ init _ (self, grid, color = "#000000"): self. grid = grid self. color = color self. body = [(8, 11), (8, 12), (8, 13)] self. direction = "Up" for I in self. body: self. grid. draw (I, self. color) # This method is used to initialize the position def initial (self): while not len (self. body) = 0: pop = self. body. pop () self. grid. draw (pop, self. grid. bg) self. body = [(8, 11), (8, 12), (8, 13)] self. direction = "Up" self. color = "#000000" for I in self. body: self. grid. draw (I, self. color) # A snake moves def move (self, new) Like a specified point: self. body. insert (0, new) pop = self. body. pop () self. grid. draw (pop, self. grid. bg) self. grid. draw (new, self. color) # The snake moves like a specified point and adds the length def add (self, new): self. body. insert (0, new) self. grid. draw (new, self. color) # snake eats special food. 1. cut its own length. def cut_down (self, new): self. body. insert (0, new) self. grid. draw (new, self. color) for I in range (0, 3): pop = self. body. pop () self. grid. draw (pop, self. grid. bg) # snake eats special food 2 and returns to the initial length of def init (self, new): self. body. insert (0, new) self. grid. draw (new, self. color) while len (self. body)> 3: pop = self. body. pop () self. grid. draw (pop, self. grid. bg) # When a snake eats a special food, it changes its color. This is purely a fun def change (self, new, color): self. color = color self. body. insert (0, new) for item in self. body: self. grid. draw (item, self. color) class snail keame (Frame): def _ init _ (self, master): Frame. _ init _ (self, master) self. grid = Grid (master) self. snake = Snake (self. grid) self. food = Food (self. grid) self. gameover = False self. score = 0 self. status = ['run', 'stop'] self. speed= 300 self. grid. canvas. bind_all ("<KeyRelease>", self. key_release) self. display_food () # used to set the color-changing food self. color_c = ("# FFB6C1", "# 6A5ACD", "# 0000FF", "# F0FFF0", "# FFFFE0", "# F0F8FF", "# EE82EE ", & quot; #000000 & quot;, & quot; #5FA8D9 & quot;, & quot; #32CD32 & quot;) self. I = 0 # The score self is displayed on the left side of the page. m = StringVar () self. ft1 = ('fixdsys ', 40, "bold") self. m1 = Message (master, textvariable = self. m, aspect = 5000, font = self. ft1, bg = "#696969") self. m1.pack (side = LEFT, fill = Y) self. m. set ("Score:" + str (self. score) # This method is used to initialize the game def initial (self): self. gameover = False self. score = 0 self. m. set ("Score:" + str (self. score) self. snake. initial () # type1: Common Food type2: reduce 2 type3: lotto, back to initial state type4: if you eat it, it will change color def display_food (self): self. food. color = "#23D978" self. food. type = 1 if randint (0, 40) = 5: self. food. color = "# FFD700" self. food. type = 3 while (self. food. pos in self. snake. body): self. food. set_pos () self. food. display () elif randint (0, 4) = 2: self. food. color = "# EE82EE" self. food. type = 4 while (self. food. pos in self. snake. body): self. food. set_pos () self. food. display () elif len (self. snake. body)> 10 and randint (0, 16) = 5: self. food. color = "# BC8F8F" self. food. type = 2 while (self. food. pos in self. snake. body): self. food. set_pos () self. food. display () else: while (self. food. pos in self. snake. body): self. food. set_pos () self. food. display () def key_release (self, event): key = event. keysym key_dict = {"Up": "Down", "Down": "Up", "Left": "Right", "Right ": "Left"} # The snake cannot follow the if key_dict.has_key (key) and not key = key_dict [self. snake. direction]: self. snake. direction = key self. move () elif key = 'P': self. status. reverse () def run (self): # first, judge whether the game is suspended if not self. status [0] = 'stop': # determine whether the game is over if self. gameover = True: message = tkMessageBox. showinfo ("Game Over", "your score: % d" % self. score) if message = 'OK': self. initial () if self. food. type = 4: color = self. color_c [self. i] self. I = (self. I + 1) % 10 self. food. color = color self. food. display () self. move (color) else: self. move () self. after (self. speed, self. run) def move (self, color = "# EE82EE"): # Calculate the point head of the next snake Movement = self. snake. body [0] if self. snake. direction = 'up': if head [1]-1 <0: new = (head [0], 16) else: new = (head [0], head [1]-1) elif self. snake. direction = 'low': new = (head [0], (head [1] + 1) % 16) elif self. snake. direction = 'left': if head [0]-1 <0: new = (24, head [1]) else: new = (head [0]-1, head [1]) else: new = (head [0] + 1) % 24, head [1]) # Hit yourself and set the Ending position of the game, wait for the next loop if new in self. snake. body: self. gameover = True # elif new = self. food. pos: if self. food. type = 1: self. snake. add (new) elif self. food. type = 2: self. snake. cut_down (new) elif self. food. type = 4: self. snake. change (new, color) else: self. snake. init (new) self. display_food () self. score = self. score + 1 self. m. set ("Score:" + str (self. score) # Keep moving forward without hitting anything. else: self. snake. move (new) if _ name _ = '_ main _': root = Tk () snkegame = snkegame (root) snkegame. run () snail keame. mainloop ()
Summary
The above section describes how to use Python to write an instance code for a snake game. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!