python語(yǔ)言實(shí)現(xiàn)貪吃蛇游戲
本文實(shí)例為大家分享了python實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
新手自學(xué)python(簡(jiǎn)易貪吃蛇代碼)
環(huán)境python3.7
剛剛大學(xué)畢業(yè)進(jìn)入工作崗位,發(fā)現(xiàn)同事基本都會(huì)寫(xiě)py腳本,于是自學(xué)了一下,并寫(xiě)了一個(gè)簡(jiǎn)單的貪吃蛇代碼,我覺(jué)得寫(xiě)的還是比較容易看懂,適合新手接觸python。
# -*- coding: utf-8 -*-
import tkinter as tk
# 使用Tkinter前需要先導(dǎo)入
import tkinter.messagebox
import pickle
import random
import time
# 第1步,實(shí)例化object,建立窗口window
window = tk.Tk()
# 第2步,給窗口的可視化起名字
window.title('Greedy snake')
# 第3步,設(shè)定窗口的大小(長(zhǎng) * 寬)
# window.geometry('1004x504') # 這里的乘是小x
# 第5步,創(chuàng)建一個(gè)主frame,長(zhǎng)在主window窗口上
frame = tk.Frame(window, bg = 'blue', bd = 2, relief = tk.FLAT)
frame.pack(side = 'left')
#當(dāng)前框架被選中,意思是鍵盤(pán)觸發(fā),只對(duì)這個(gè)框架有效
frame.focus_set()
Labellist = [] #存放所有方塊的label
Blocklist = [] #存放背景方塊的值 1:被占用 0:空閑
Snakelist = [] #存放snake的坐標(biāo)
height = 15
width = 20
#snack前進(jìn)方向
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
#二級(jí)菜單:game
gamemenu=tk.Menu(mainmenu)
mainmenu.add_cascade(label='游戲',menu=gamemenu)
gamemenu.add_command(label = '開(kāi)始/暫停',command=Start_Stop)
gamemenu.add_command(label = '重置',command=Restart)
gamemenu.add_command(label = '退出',command=window.quit)
app = App(window)
window.mainloop()
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專(zhuān)題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Python pandas plot輸出圖形中顯示中文亂碼問(wèn)題
今天小編就為大家分享一篇解決Python pandas plot輸出圖形中顯示中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python 實(shí)戰(zhàn)開(kāi)發(fā)校園管理系統(tǒng)詳細(xì)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開(kāi)發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-10-10
如何在Python中利用matplotlib.pyplot畫(huà)出函數(shù)圖詳解
通過(guò)圖像可以直觀地學(xué)習(xí)函數(shù)變化、分布等規(guī)律,在學(xué)習(xí)函數(shù)、概率分布等方面效果顯著,下面這篇文章主要給大家介紹了關(guān)于如何在Python中利用matplotlib.pyplot畫(huà)出函數(shù)圖的相關(guān)資料,需要的朋友可以參考下2022-08-08
Python使用pandas將表格數(shù)據(jù)進(jìn)行處理
這篇文章主要介紹了Python使用pandas將表格數(shù)據(jù)進(jìn)行處理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
Python+OpenCV進(jìn)行不規(guī)則多邊形ROI區(qū)域提取
ROI即感興趣區(qū)域。機(jī)器視覺(jué)、圖像處理中,從被處理的圖像以方框、圓、橢圓、不規(guī)則多邊形等方式勾勒出需要處理的區(qū)域,稱(chēng)為感興趣區(qū)域,ROI。本文將利用Python和OpenCV實(shí)現(xiàn)不規(guī)則多邊形ROI區(qū)域提取,需要的可以參考一下2022-03-03
對(duì)python3 sort sorted 函數(shù)的應(yīng)用詳解
今天小編就為大家分享一篇對(duì)python3 sort sorted 函數(shù)的應(yīng)用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)共線性處理詳解
今天小編就為大家分享一篇python數(shù)據(jù)預(yù)處理 :數(shù)據(jù)共線性處理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

