Python寫的貪吃蛇游戲例子
第一次用Python寫這種比較實(shí)用且好玩的東西,權(quán)當(dāng)練手吧
游戲說明:
* P鍵控制“暫停/開始”
* 方向鍵控制貪吃蛇的方向
源代碼如下:
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 for i in self.grid.grid_list if i 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()
相關(guān)文章
TensorFlow基于MNIST數(shù)據(jù)集實(shí)現(xiàn)車牌識(shí)別(初步演示版)
這篇文章主要介紹了TensorFlow基于MNIST數(shù)據(jù)集實(shí)現(xiàn)車牌識(shí)別(初步演示版),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python 使用 multiprocessing 模塊創(chuàng)建進(jìn)程池的操作方法
在現(xiàn)代計(jì)算任務(wù)中,尤其是處理大量數(shù)據(jù)或計(jì)算密集型任務(wù)時(shí),使用并行處理可以顯著提升程序性能,Python的multiprocessing模塊提供了創(chuàng)建進(jìn)程池的功能,通過預(yù)先創(chuàng)建的進(jìn)程來并發(fā)執(zhí)行任務(wù),避免了頻繁的進(jìn)程創(chuàng)建和銷毀,感興趣的朋友一起看看吧2024-10-10Python實(shí)現(xiàn)制作透明背景的電子印章
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言實(shí)現(xiàn)制作透明背景的電子印章,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-09-09利用Python命令行傳遞實(shí)例化對(duì)象的方法
最近在工作中遇到了一個(gè)問題,需要接收啟動(dòng)腳本傳遞過來的實(shí)例化后的對(duì)象,通過在網(wǎng)上查找資料發(fā)現(xiàn)了兩個(gè)方法,文中通過實(shí)例代碼就給大家詳細(xì)介紹了這兩種方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11python通過elixir包操作mysql數(shù)據(jù)庫實(shí)例代碼
這篇文章主要介紹了python通過elixir包操作mysql數(shù)據(jù)庫,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Python3結(jié)合Dlib實(shí)現(xiàn)人臉識(shí)別和剪切
本篇文章給大家詳細(xì)分析了Python3結(jié)合Dlib實(shí)現(xiàn)人臉識(shí)別和剪切這個(gè)技術(shù),對(duì)此有興趣的朋友參考學(xué)習(xí)下。2018-01-01Django User 模塊之 AbstractUser 擴(kuò)展詳解
這篇文章主要介紹了Django User 模塊之 AbstractUser 擴(kuò)展詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03一文詳解Python如何處理函數(shù)調(diào)用超時(shí)問題
在Python開發(fā)中,我們經(jīng)常會(huì)遇到需要控制函數(shù)執(zhí)行時(shí)間的場(chǎng)景,本文將深入探討Python中處理函數(shù)調(diào)用超時(shí)的幾種方法,感興趣的小伙伴可以參考一下2025-04-04