Python's example of a greedy snake game _python

Source: Internet
Author: User
Tags in python

The first time in Python to write this more practical and fun things, the right to be a practicing bar

Game Description:

* P key controls pause/start
* Direction key control the direction of the greedy snake

The source code is as follows:

Copy Code code as follows:

From Tkinter Import *
Import Tkmessagebox,sys
From random import Randint

Class Grid (object):
def __init__ (self,master=none,window_width=800,window_height=600,grid_width=50,offset=10):
Self.height = Window_height
Self.width = Window_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.width+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_list (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_list

Class 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 = 300
Self.color = "#5FA8D9"
Self.food = food (Self.grid)
Self.display_food ()
Self.gameover = False
Self.score = 0
def available_grid (self):
return [I to I in self.grid.grid_list if I is not in self.body[2:]]
def change_direction (self, direction):
Self.direction = Direction
def display (self):
for (X,y) in Self.body:
Self.grid.draw ((x,y), Self.color)
def display_food (self):
while (Self.food.pos in self.body):
Self.food.set_pos ()
Self.food.display ()
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_food ()
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.run)
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 ()

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.