python语言实现贪吃蛇游戏

 更新时间:2020年11月13日 17:26:00   作者:Kuroneko_cl  
这篇文章主要为大家详细介绍了python语言实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

新手自学python(简易贪吃蛇代码)
环境python3.7

刚刚大学毕业进入工作岗位,发现同事基本都会写py脚本,于是自学了一下,并写了一个简单的贪吃蛇代码,我觉得写的还是比较容易看懂,适合新手接触python。

# -*- coding: utf-8 -*-
import tkinter as tk 
# 使用Tkinter前需要先导入
import tkinter.messagebox
import pickle
import random
import time

# 第1步,实例化object,建立窗口window
window = tk.Tk() 
# 第2步,给窗口的可视化起名字
window.title('Greedy snake')
# 第3步,设定窗口的大小(长 * 宽)
# window.geometry('1004x504') # 这里的乘是小x

# 第5步,创建一个主frame,长在主window窗口上
frame = tk.Frame(window, bg = 'blue', bd = 2, relief = tk.FLAT)
frame.pack(side = 'left')
#当前框架被选中,意思是键盘触发,只对这个框架有效
frame.focus_set()
Labellist = [] #存放所有方块的label
Blocklist = [] #存放背景方块的值 1:被占用 0:空闲
Snakelist = [] #存放snake的坐标
height = 15
width = 20
#snack前进方向
left = 0
right = 1
up = 2
down =3
pause = 0
start = 1
class App(tk.Frame): 
 def __init__(self,master):  
  self.window = master  
  tk.Frame.__init__(self)  
  master.bind('<Up>',self.Up)  
  master.bind('<Left>',self.Left)  
  master.bind('<Right>',self.Right)  
  master.bind('<Down>',self.Down)  
  master.bind('<p>',self.Pause)  
  master.bind('<s>',self.Start)  
  master.bind('<r>',self.Restart)  
  self.Init_snake() #初始化界面方法
  self.time = 1000  
  self.Onetime()     
 def Up(self, event):  
  if self.Istart:   
   self.direction = up 
 def Down(self, event):  
  if self.Istart:   
   self.direction = down 
 def Left(self, event):  
  if self.Istart:   
   self.direction = left 
 def Right(self, event):  
  if self.Istart:   
   self.direction = right
 def Init_snake(self):  
  del Labellist[:]  
  del Blocklist[:]  
  del Snakelist[:]    
  #初始化背景方块  
  LabelRowList = []  
  BlockRowlist = []  
  c = r = 0  
  for k in range(width*height):   
   LN=tk.Label(frame,text = ' ', bg = 'black', fg = 'white', relief = tk.FLAT, bd = 4)   
   LN.grid(row=r,column=c,sticky=tk.N+tk.E+tk.S+tk.W)   
   LabelRowList.append(LN)   
   BlockRowlist.append(0)   
   c=c+1 
   if c>=20:
    r=r+1
    c=0    
    Labellist.append(LabelRowList)    
    Blocklist.append(BlockRowlist)    
    LabelRowList = []    
    BlockRowlist = []  
  #初始化snake  
  self.Istart = 0  
  self.direction = left
  self.direction_last = left
  self.overflag = 0 
  #snake head的初始位置  
  self.x = 7  
  self.y = 8  
  #snake tail的初始位置  
  self.x_tail = 7  
  self.y_tail = 10  
  Snakelist.append((7,8))  
  Snakelist.append((7,9))  
  Snakelist.append((7,10))  
  self.snakelen = len(Snakelist)


  Blocklist[self.x][self.y] = 1  
  Blocklist[self.x][self.y+1] = 1  
  Blocklist[self.x][self.y+2] = 1  
  Labellist[self.x][self.y].config(bg = 'green', relief = tk.RAISED)  
  Labellist[self.x][self.y+1].config(bg = 'white', relief = tk.RAISED)  
  Labellist[self.x][self.y+2].config(bg = 'white', relief = tk.RAISED)  
  #初始化food  
  self.food_x = random.randint(0,14)  
  self.food_y = random.randint(0,19)  
  while Blocklist[self.food_x][self.food_y] == 1:   
   self.food_x = random.randint(0,14)   
   self.food_y = random.randint(0,19)     
  Blocklist[self.food_x][self.food_y] = 1  
  Labellist[self.food_x][self.food_y].config(bg = 'red', relief = tk.RIDGE)
 def Pause(self, event):  
  self.Istart = pause 
 def Start(self, event):  
  self.Istart = start 
 def Restart(self, event):  
  self.Init_snake()
 def Onetime(self): #每1000ms做一次界面刷新  
  if self.Istart and self.overflag == 0: 
   if (self.direction_last == down and self.direction == up )or(self.direction_last == up and self.direction == down )or(self.direction_last ==left and self.direction == right )or(self.direction_last ==right and self.direction == left ):
    self.direction = self.direction_last
   self.direction_last = self.direction 
   x0 = self.x   
   y0 = self.y   
   if self.direction == left:    
    if x0 == self.food_x and y0-1 == self.food_y:          
     Labellist[x0][y0-1].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)
     self.food_x = random.randint(0,14)     
     self.food_y = random.randint(0,19)     
     while Blocklist[self.food_x][self.food_y] == 1:      
      self.food_x = random.randint(0,14)      
      self.food_y = random.randint(0,19)     
     Blocklist[self.food_x][self.food_y] = 1     
     Labellist[self.food_x][self.food_y].config(bg = 'red', relief = tk.RIDGE)
     self.snakelen += 1     
     Snakelist.insert(0,(x0,y0-1))      
     self.x = x0     
     self.y = y0 - 1    
    elif (x0>=0 and x0<height and y0-1>=0 and y0-1<width and Blocklist[x0][y0-1] == 0) or (self.x_tail == x0 and self.y_tail == y0 - 1):               
     Blocklist[self.x_tail][self.y_tail] = 0     
     Labellist[self.x_tail][self.y_tail].config(bg = 'black', relief = tk.FLAT)     
     Blocklist[x0][y0-1] = 1     
     Labellist[x0][y0-1].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)           
     del Snakelist[self.snakelen - 1]     
     Snakelist.insert(0,(x0,y0-1))      
     self.x = x0     
     self.y = y0 - 1     
     self.x_tail = Snakelist[self.snakelen - 1][0]     
     self.y_tail = Snakelist[self.snakelen - 1][1]    
    else:     
     tk.messagebox.showinfo(title = 'snake', message = 'game over!!!')     
     self.overflag = 1     
   elif self.direction == up:    
    if x0-1 == self.food_x and y0 == self.food_y:           
     Labellist[x0-1][y0].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)
     self.food_x = random.randint(0,14)     
     self.food_y = random.randint(0,19)     
     while Blocklist[self.food_x][self.food_y] == 1:      
      self.food_x = random.randint(0,14)      
      self.food_y = random.randint(0,19)     
     Blocklist[self.food_x][self.food_y] = 1     
     Labellist[self.food_x][self.food_y].config(bg = 'red', relief = tk.RIDGE) 
     self.snakelen += 1     
     Snakelist.insert(0,(x0-1,y0))      
     self.x = x0 - 1     
     self.y = y0 
    elif (x0-1 >=0 and x0-1<height and y0>=0 and y0<width and Blocklist[x0-1][y0] == 0) or (self.x_tail == x0-1 and self.y_tail == y0):          
     Blocklist[self.x_tail][self.y_tail] = 0     
     Labellist[self.x_tail][self.y_tail].config(bg = 'black', relief = tk.FLAT)     
     Blocklist[x0-1][y0] = 1     
     Labellist[x0-1][y0].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)
     del Snakelist[self.snakelen - 1]     
     Snakelist.insert(0,(x0 - 1,y0))      
     self.x = x0 - 1     
     self.y = y0     
     self.x_tail = Snakelist[self.snakelen - 1][0]     
     self.y_tail = Snakelist[self.snakelen - 1][1]    
    else:     
     tk.messagebox.showinfo(title = 'snake', message = 'game over!!!')     
     self.overflag = 1   
   elif self.direction == down:    
    if x0+1 == self.food_x and y0 == self.food_y:          
     Labellist[x0+1][y0].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)
     self.food_x = random.randint(0,14)     
     self.food_y = random.randint(0,19)     
     while Blocklist[self.food_x][self.food_y] == 1:      
      self.food_x = random.randint(0,14)      
      self.food_y = random.randint(0,19)     
     Blocklist[self.food_x][self.food_y] = 1     
     Labellist[self.food_x][self.food_y].config(bg = 'red', relief = tk.RIDGE) 
     self.snakelen += 1     
     Snakelist.insert(0,(x0+1,y0))      
     self.x = x0 + 1     
     self.y = y0 
    elif (x0+1 >=0 and x0+1 <height and y0>=0 and y0<width and Blocklist[x0+1][y0] == 0) or (self.x_tail == x0+1 and self.y_tail == y0):               
     Blocklist[self.x_tail][self.y_tail] = 0     
     Labellist[self.x_tail][self.y_tail].config(bg = 'black', relief = tk.FLAT)     
     Blocklist[x0+1][y0] = 1     
     Labellist[x0+1][y0].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED) 
     del Snakelist[self.snakelen - 1]     
     Snakelist.insert(0,(x0 + 1,y0))      
     self.x = x0 + 1     
     self.y = y0     
     self.x_tail = Snakelist[self.snakelen - 1][0]     
     self.y_tail = Snakelist[self.snakelen - 1][1]    
    else:     
     tk.messagebox.showinfo(title = 'snake', message = 'game over!!!')     
     self.overflag = 1   
   elif self.direction == right:    
    if x0 == self.food_x and y0+1 == self.food_y:          
     Labellist[x0][y0+1].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED)
     self.food_x = random.randint(0,14)     
     self.food_y = random.randint(0,19)     
     while Blocklist[self.food_x][self.food_y] == 1:      
      self.food_x = random.randint(0,14)      
      self.food_y = random.randint(0,19)     
     Blocklist[self.food_x][self.food_y] = 1     
     Labellist[self.food_x][self.food_y].config(bg = 'red', relief = tk.RIDGE) 
     self.snakelen += 1     
     Snakelist.insert(0,(x0,y0 + 1))      
     self.x = x0     
     self.y = y0 + 1 
    elif (x0>=0 and x0<height and y0+1>=0 and y0+1<width and Blocklist[x0][y0+1] == 0) or (self.x_tail == x0 and self.y_tail == y0+1):              
     Blocklist[self.x_tail][self.y_tail] = 0     
     Labellist[self.x_tail][self.y_tail].config(bg = 'black', relief = tk.FLAT)     
     Blocklist[x0][y0+1] = 1     
     Labellist[x0][y0+1].config(bg = 'green', relief = tk.RAISED)     
     Labellist[x0][y0].config(bg = 'white', relief = tk.RAISED) 
     del Snakelist[self.snakelen - 1]     
     Snakelist.insert(0,(x0,y0 + 1))      
     self.x = x0     
     self.y = y0 + 1     
     self.x_tail = Snakelist[self.snakelen - 1][0]     
     self.y_tail = Snakelist[self.snakelen - 1][1]    
    else:     
     tk.messagebox.showinfo(title = 'snake', message = 'game over!!!')     
     self.overflag = 1  
  self.after(self.time,self.Onetime)
def Start_Stop(): 
 app.Istart = 1 - app.Istart 
def Restart(): 
 app.Restart(0) 
#主菜单
mainmenu = tk.Menu(window)
window['menu'] = mainmenu
#二级菜单:game
gamemenu=tk.Menu(mainmenu)
mainmenu.add_cascade(label='游戏',menu=gamemenu)
gamemenu.add_command(label = '开始/暂停',command=Start_Stop)
gamemenu.add_command(label = '重置',command=Restart)
gamemenu.add_command(label = '退出',command=window.quit)
app = App(window)  
window.mainloop()

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决Python pandas plot输出图形中显示中文乱码问题

    解决Python pandas plot输出图形中显示中文乱码问题

    今天小编就为大家分享一篇解决Python pandas plot输出图形中显示中文乱码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • Python 实战开发校园管理系统详细流程

    Python 实战开发校园管理系统详细流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用Python开发一套校园管理系统,包含各种人员,如教师、学生等。学校的系统通常还包括一些课程的信息,大家可以在过程中查缺补漏,提升水平
    2021-10-10
  • 如何在Python中利用matplotlib.pyplot画出函数图详解

    如何在Python中利用matplotlib.pyplot画出函数图详解

    通过图像可以直观地学习函数变化、分布等规律,在学习函数、概率分布等方面效果显著,下面这篇文章主要给大家介绍了关于如何在Python中利用matplotlib.pyplot画出函数图的相关资料,需要的朋友可以参考下
    2022-08-08
  • Python生成图文并茂的PDF报告的方法详解

    Python生成图文并茂的PDF报告的方法详解

    reportlab是Python的一个标准库,可以画图、画表格、编辑文字,最后可以输出PDF格式。本文将利用reportlab模块生成图文并茂的PDF报告,感兴趣的可以了解一下
    2022-06-06
  • Python图像处理Pillow库的安装使用

    Python图像处理Pillow库的安装使用

    本文详细介绍了Python第三方库Pillow的使用,通过导入Pillow库、打开和保存图像、基本图像操作以及图像处理高级功能的代码示例,我们了解了Pillow库的强大功能和灵活性,感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • Python使用pandas将表格数据进行处理

    Python使用pandas将表格数据进行处理

    这篇文章主要介绍了Python使用pandas将表格数据进行处理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-08-08
  • Python+OpenCV进行不规则多边形ROI区域提取

    Python+OpenCV进行不规则多边形ROI区域提取

    ROI即感兴趣区域。机器视觉、图像处理中,从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI。本文将利用Python和OpenCV实现不规则多边形ROI区域提取,需要的可以参考一下
    2022-03-03
  • 对python3 sort sorted 函数的应用详解

    对python3 sort sorted 函数的应用详解

    今天小编就为大家分享一篇对python3 sort sorted 函数的应用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Django基础之Model操作步骤(介绍)

    Django基础之Model操作步骤(介绍)

    下面小编就为大家带来一篇Django基础之Model操作步骤(介绍)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • python数据预处理 :数据共线性处理详解

    python数据预处理 :数据共线性处理详解

    今天小编就为大家分享一篇python数据预处理 :数据共线性处理详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02

最新评论