使⽤Python写⼀个贪吃蛇游戏实例代码
我在程序中加⼊了分数显⽰,三种特殊⾷物,将贪吃蛇的游戏逻辑写到了SnakeGame的类中,⽽不是在Snake类中。特殊⾷物:
1.绿⾊:普通,吃了增加体型
2.红⾊:吃了减少体型
3.⾦⾊:吃了回到最初体型
4.变⾊⾷物:吃了会根据⾷物颜⾊改变蛇的颜⾊
#coding=UTF-8
from Tkinter import *
from random import randint
import tkMessageBox
class Grid(object):
def __init__(self, master=None,height=16, width=24, offset=10, grid_width=50, bg="#808080"):
self.height = height
self.width = width
self.offset = offset
self.bg = bg
self.canvas = Canvas(master, width=self.id_width+2*self.offset, height=self.id_width+
2*self.offset, bg=self.bg)
self.canvas.pack(side=RIGHT, fill=Y)
def draw(self, pos, color, ):
x = pos[0] * id_width + self.offset
y = pos[1] * id_width + self.offset
#outline属性要与⽹格的背景⾊(self.bg)相同,要不然会很丑
ate_rectangle(x, y, x + id_width, y + id_width, fill=color, outline=self.bg)
class Food(object):
def __init__(self, grid, color = "#23D978"):
self.set_pos()
def set_pos(self):
x = randint(0, id.width - 1)
y = randint(0, id.height - 1)
self.pos = (x, y)
def display(self):
class Snake(object):
def __init__(self, grid, color = "#000000"):
self.body = [(8, 11), (8, 12), (8, 13)]
self.direction = "Up"
for i in self.body:
#这个⽅法⽤于游戏重新开始时初始化贪吃蛇的位置
def initial(self):
while not len(self.body) == 0:
pop = self.body.pop()
self.body = [(8, 11), (8, 12), (8, 13)]
self.direction = "Up"
for i in self.body:
#蛇像⼀个指定点移动
def move(self, new):
self.body.insert(0, new)
pop = self.body.pop()
#蛇像⼀个指定点移动,并增加长度
def add(self ,new):
self.body.insert(0, new)
#蛇吃到了特殊⾷物1,剪短⾃⾝的长度
def cut_down(self,new):
self.body.insert(0, new)
for i in range(0,3):
pop = self.body.pop()
#蛇吃到了特殊⾷物2,回到最初长度贪吃蛇的编程代码
def init(self, new):
self.body.insert(0, new)
while len(self.body) > 3:
pop = self.body.pop()
#蛇吃到了特殊⾷物3,改变了⾃⾝的颜⾊,纯属好玩
def change(self, new, color):
self.body.insert(0, new)
for item in self.body:
class SnakeGame(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.snake = id)
self.food = id)
self.gameover = False
self.score = 0
self.status = ['run', 'stop']
self.speed = 300
self.display_food()
#⽤于设置变⾊⾷物
00000","#5FA8D9","#32CD32")    self.i = 0
#界⾯左侧显⽰分数
self.m = StringVar()
self.ft1 = ('Fixdsys', 40, "bold")
self.m1 = Message(master, textvariable=self.m, aspect=5000, font=self.ft1, bg="#696969")
self.m1.pack(side=LEFT, fill=Y)
#这个⽅法⽤于游戏重新开始时初始化游戏
def initial(self):
self.gameover = False
self.score = 0
self.snake.initial()
#type1:普通⾷物 type2:减少2 type3:⼤乐透,回到最初状态 type4:吃了会变⾊
def display_food(self):
lor = "#23D978"
pe = 1
if randint(0, 40) == 5:
lor = "#FFD700"
pe = 3
while (self.food.pos in self.snake.body):
self.food.set_pos()
self.food.display()
elif randint(0, 4) == 2:
lor = "#EE82EE"
pe = 4
while (self.food.pos in self.snake.body):
self.food.set_pos()
self.food.display()
elif len(self.snake.body) > 10 and randint(0, 16) == 5:
lor = "#BC8F8F"
pe = 2
while (self.food.pos in self.snake.body):
self.food.set_pos()
self.food.display()
else:
while (self.food.pos in self.snake.body):
self.food.set_pos()
self.food.display()
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.direction = key
elif key == 'p':
verse()
def run(self):
#⾸先判断游戏是否暂停
if not self.status[0] == 'stop':
#判断游戏是否结束
if self.gameover == True:
message = tkMessageBox.showinfo("Game Over", "your score: %d" % self.score)
if message == 'ok':
self.initial()
if pe == 4:
color = lor_c[self.i]
self.i = (self.i+1)%10
lor = color
self.food.display()
else:
self.after(self.speed, self.run)
def move(self, color="#EE82EE"):
# 计算蛇下⼀次移动的点
head = self.snake.body[0]
if self.snake.direction == 'Up':
if head[1] - 1 < 0:
new = (head[0], 16)
else:
new = (head[0], head[1] - 1)
elif self.snake.direction == 'Down':
new = (head[0], (head[1] + 1) % 16)
elif self.snake.direction == 'Left':
if head[0] - 1 < 0:
new = (24, head[1])
else:
new = (head[0] - 1, head[1])
else:
new = ((head[0] + 1) % 24, head[1])
#撞到⾃⼰,设置游戏结束的标志位,等待下⼀循环
if new in self.snake.body:
self.gameover=True
#吃到⾷物
elif new == self.food.pos:
if pe == 1:
self.snake.add(new)
elif pe == 2:
self.snake.cut_down(new)
elif pe == 4:
self.snake.change(new, color)
else:
self.snake.init(new)
self.display_food()
self.score = self.score+1
#什么都没撞到,继续前进
else:
ve(new)
if __name__ == '__main__':
root = Tk()
snakegame = SnakeGame(root)
snakegame.run()
snakegame.mainloop()
总结
以上所述是⼩编给⼤家介绍的使⽤Python写⼀个贪吃蛇游戏实例代码,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。