This article mainly introduces Python to write the snake game example, practiced hand works, but also fun and can learn things, the need for friends can refer to the next
The first time in Python to write this more practical and fun things, the right to be practiced hand it
Game Description:
* P Key Control "pause/Start"
* Direction keys control the direction of the snake
The source code is as follows:
Copy the Code code as follows:
From Tkinter import *import tkmessagebox,sysfrom random import Randintclass Grid (object): Def __init__ (Self,master=none , window_width=800,window_height=600,grid_width=50,offset=10): Self.height = window_height Self.width = Windo W_width self.grid_width = grid_width Self.offset = Offset self.grid_x = self.width/self.grid_width self.grid_y = Self.height/self.grid_width self.bg = "#EBEBEB" Self.canvas = Canvas (Master, WIDTH=SELF.W Idth+2*self.offset, Height=self.height+2*self.offset, bg=self.bg) self.canvas.pack () self.grid_list () def Draw (self, POS, color,): x = pos[0]*self.grid_width + Self.offset y = pos[1]*self.grid_width + self.offset Self.canvas.create_rectangle (x, Y, X+self.grid_width, y+self.grid_width,fill=color,outline=self.bg) def Grid_lis T (self): Grid_list = [] for y in range (0,self.grid_y): For x in Range (0,self.grid_x): Grid_list.append ((x,Y)) Self.grid_list = Grid_listclass Food (object): Def-__init__ (Self, grid): Self.grid = grid-Self. color = "#23D978" Self.set_pos () def set_pos (self): x = Randint (0,self.grid.grid_x-1) y = Randint (0,self.grid.grid_y-1) Self.pos = (x, y) def display (self): Self.grid.draw (self.pos,self. color) class Snake (object): Def __init__ (self, grid): Self.grid = Grid Self.body = [(10,6), (10,7), (10,8)] Self.direction = "up" self.status = [' Run ', ' stop '] self.speed = Self.color = "#5FA8D9" Self.food = food (Self.grid) self.display_food () Self.gameover = False Self.score = 0 D EF Available_grid (self): return [i-I in self.grid.grid_list if I am not in self.body[2:]] def change_direction ( Self, direction): Self.direction = Direction def display (self): for (x, y) in SELF.BODY:SELF.G Rid.draw (x, y), Self.coloR) def display_food (self): while (Self.food.pos in self.body): Self.food.set_pos () Self.food.di Splay () def move (self): head = self.body[0] if self.direction = = "Up": New = (head[0], head[1] -1) elif self.direction = = ' down ': New = (head[0], head[1]+1) elif self.direction = = ' Left ': New = (Head[0]-1,head[1]) Else:new = (head[0]+1,head[1]) if not self.food.pos = = Head: Pop = Self.body.pop () self.grid.draw (pop,self.grid.bg) Else:self.display_f Ood () Self.score + = 1 Self.body.insert (0,new) if not new in Self.available_grid (): Self.status.reverse () Self.gameover = True Else:self.grid.draw (new,color=self . color) class Snakegame (Frame): Def __init__ (Self,master=none, *args, **kwargs): frame.__init__ (self, master) Self.master = Master Self.grid = Grid (Master=master,*args, **kwargs) Self.snake = Snake (Self.grid) Self.bind_all ("", self . Key_release) Self.snake.display () def run (self): if not self.snake.status[0] = = ' Stop ': self. Snake.move () if self.snake.gameover = = True:message = Tkmessagebox.showinfo ("Game over", "Your score: %d "% self.snake.score) if message = = ' OK ': Sys.exit () Self.after (SELF.SNAKE.SPEED,SELF.R UN) def key_release (Self, event): key = Event.keysym key_dict = {' Up ': ' Down ', ' Down ': ' Up ', ' left ': ' Right ', ' Right ': ' Left '} if Key_dict.has_key (key) and not key = = Key_dict[self.snake.direction]: self.snake.change _direction (Key) Self.snake.move () elif key = = ' P ': self.snake.status.reverse () if __name__ = = ' __main__ ': root = Tk () Snakegame = Snakegame (root) Snakegame.run () Snakegame.mainloop ()