国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用Python寫一個貪吃蛇游戲實例代碼

2020-02-16 02:07:30
字體:
來源:轉載
供稿:網友

我在程序中加入了分數顯示,三種特殊食物,將貪吃蛇的游戲邏輯寫到了SnakeGame的類中,而不是在Snake類中。

特殊食物:

1.綠色:普通,吃了增加體型

2.紅色:吃了減少體型

3.金色:吃了回到最初體型

4.變色食物:吃了會根據食物顏色改變蛇的顏色

#coding=UTF-8from Tkinter import *from random import randintimport tkMessageBoxclass 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.grid_width = grid_width    self.bg = bg    self.canvas = Canvas(master, width=self.width*self.grid_width+2*self.offset, height=self.height*self.grid_width+                                              2*self.offset, bg=self.bg)    self.canvas.pack(side=RIGHT, fill=Y)  def draw(self, pos, color, ):    x = pos[0] * self.grid_width + self.offset    y = pos[1] * self.grid_width + self.offset    #outline屬性要與網格的背景色(self.bg)相同,要不然會很丑    self.canvas.create_rectangle(x, y, x + self.grid_width, y + self.grid_width, fill=color, outline=self.bg)class Food(object):  def __init__(self, grid, color = "#23D978"):    self.grid = grid    self.color = color    self.set_pos()    self.type = 1  def set_pos(self):    x = randint(0, self.grid.width - 1)    y = randint(0, self.grid.height - 1)    self.pos = (x, y)  def display(self):    self.grid.draw(self.pos, self.color)class Snake(object):  def __init__(self, grid, color = "#000000"):    self.grid = grid    self.color = color    self.body = [(8, 11), (8, 12), (8, 13)]    self.direction = "Up"    for i in self.body:      self.grid.draw(i, self.color)  #這個方法用于游戲重新開始時初始化貪吃蛇的位置  def initial(self):    while not len(self.body) == 0:      pop = self.body.pop()      self.grid.draw(pop, self.grid.bg)    self.body = [(8, 11), (8, 12), (8, 13)]    self.direction = "Up"    self.color = "#000000"    for i in self.body:      self.grid.draw(i, self.color)  #蛇像一個指定點移動  def move(self, new):    self.body.insert(0, new)    pop = self.body.pop()    self.grid.draw(pop, self.grid.bg)    self.grid.draw(new, self.color)  #蛇像一個指定點移動,并增加長度  def add(self ,new):    self.body.insert(0, new)    self.grid.draw(new, self.color)  #蛇吃到了特殊食物1,剪短自身的長度  def cut_down(self,new):    self.body.insert(0, new)    self.grid.draw(new, self.color)    for i in range(0,3):      pop = self.body.pop()      self.grid.draw(pop, self.grid.bg)  #蛇吃到了特殊食物2,回到最初長度  def init(self, new):    self.body.insert(0, new)    self.grid.draw(new, self.color)    while len(self.body) > 3:      pop = self.body.pop()      self.grid.draw(pop, self.grid.bg)   #蛇吃到了特殊食物3,改變了自身的顏色,純屬好玩  def change(self, new, color):    self.color = color    self.body.insert(0, new)    for item in self.body:      self.grid.draw(item, self.color)class SnakeGame(Frame):  def __init__(self, master):    Frame.__init__(self, master)    self.grid = Grid(master)    self.snake = Snake(self.grid)    self.food = Food(self.grid)    self.gameover = False    self.score = 0    self.status = ['run', 'stop']    self.speed = 300    self.grid.canvas.bind_all("<KeyRelease>", self.key_release)    self.display_food()    #用于設置變色食物    self.color_c = ("#FFB6C1","#6A5ACD","#0000FF","#F0FFF0","#FFFFE0","#F0F8FF","#EE82EE","#000000","#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)    self.m.set("Score:"+str(self.score))  #這個方法用于游戲重新開始時初始化游戲  def initial(self):    self.gameover = False    self.score = 0    self.m.set("Score:"+str(self.score))    self.snake.initial()  #type1:普通食物 type2:減少2 type3:大樂透,回到最初狀態 type4:吃了會變色  def display_food(self):    self.food.color = "#23D978"    self.food.type = 1    if randint(0, 40) == 5:      self.food.color = "#FFD700"      self.food.type = 3      while (self.food.pos in self.snake.body):        self.food.set_pos()      self.food.display()    elif randint(0, 4) == 2:      self.food.color = "#EE82EE"      self.food.type = 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:      self.food.color = "#BC8F8F"      self.food.type = 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      self.move()    elif key == 'p':      self.status.reverse()  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 self.food.type == 4:        color = self.color_c[self.i]        self.i = (self.i+1)%10        self.food.color = color        self.food.display()        self.move(color)      else:        self.move()    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 self.food.type == 1:        self.snake.add(new)      elif self.food.type == 2:        self.snake.cut_down(new)      elif self.food.type == 4:        self.snake.change(new, color)      else:        self.snake.init(new)      self.display_food()      self.score = self.score+1      self.m.set("Score:" + str(self.score))    #什么都沒撞到,繼續前進    else:      self.snake.move(new)if __name__ == '__main__':  root = Tk()  snakegame = SnakeGame(root)  snakegame.run()  snakegame.mainloop()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平远县| 寿光市| 峨边| 富民县| 宜宾市| 玉门市| 依兰县| 武定县| 修武县| 得荣县| 新宾| 苏尼特左旗| 太谷县| 柳州市| 凤凰县| 新龙县| 长阳| 平阳县| 道真| 奉贤区| 二手房| 石嘴山市| 多伦县| 巧家县| 武清区| 衡东县| 澄江县| 甘肃省| 阿拉善盟| 如东县| 醴陵市| 中宁县| 惠州市| 桂阳县| 阳西县| 鞍山市| 本溪| 和平区| 碌曲县| 海安县| 建平县|