亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Python寫一個貪吃蛇游戲?qū)嵗a

 更新時間:2017年08月21日 09:45:58   作者:Suisnail  
這篇文章主要介紹了使用Python寫一個貪吃蛇游戲?qū)嵗a,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

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

特殊食物:

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

2.紅色:吃了減少體型

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

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

#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.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屬性要與網(wǎng)格的背景色(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()
    #用于設(shè)置變色食物
    self.color_c = ("#FFB6C1","#6A5ACD","#0000FF","#F0FFF0","#FFFFE0","#F0F8FF","#EE82EE","#000000","#5FA8D9","#32CD32")
    self.i = 0
    #界面左側(cè)顯示分?jǐn)?shù)
    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:大樂透,回到最初狀態(tài) 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':
      #判斷游戲是否結(jié)束
      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])
      #撞到自己,設(shè)置游戲結(jié)束的標(biāo)志位,等待下一循環(huán)
    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))
    #什么都沒撞到,繼續(xù)前進
    else:
      self.snake.move(new)
if __name__ == '__main__':
  root = Tk()
  snakegame = SnakeGame(root)
  snakegame.run()
  snakegame.mainloop()

總結(jié)

以上所述是小編給大家介紹的使用Python寫一個貪吃蛇游戲?qū)嵗a,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • python根據(jù)距離和時長計算配速示例

    python根據(jù)距離和時長計算配速示例

    這篇文章主要介紹了python根據(jù)距離和時長計算配速示例,需要的朋友可以參考下
    2014-02-02
  • Python中Pandas庫的數(shù)據(jù)處理與分析

    Python中Pandas庫的數(shù)據(jù)處理與分析

    Python的Pandas庫是數(shù)據(jù)科學(xué)領(lǐng)域中非常重要的一個庫,它使數(shù)據(jù)清洗和分析工作變得更快更簡單,Pandas結(jié)合了NumPy的高性能數(shù)組計算功能以及電子表格和關(guān)系型數(shù)據(jù)庫(如SQL)的靈活數(shù)據(jù)處理能力,需要的朋友可以參考下
    2023-07-07
  • Python中異步HTTP客戶端/服務(wù)器框架aiohttp的使用全面指南

    Python中異步HTTP客戶端/服務(wù)器框架aiohttp的使用全面指南

    aiohttp 是一個基于 Python asyncio 的異步 HTTP 客戶端/服務(wù)器框架,專為高性能網(wǎng)絡(luò)編程設(shè)計,本文將為大家詳細(xì)介紹一下它的具體使用,需要的可以了解下
    2025-06-06
  • 下載官網(wǎng)python并安裝的步驟詳解

    下載官網(wǎng)python并安裝的步驟詳解

    在本篇文章里小編給大家整理了關(guān)于下載官網(wǎng)python并安裝的步驟詳解,需要的朋友們參考學(xué)習(xí)下。
    2019-10-10
  • Python學(xué)生信息管理系統(tǒng)修改版

    Python學(xué)生信息管理系統(tǒng)修改版

    這篇文章主要為大家詳細(xì)介紹了python學(xué)生信息管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 一個小示例告訴你Python語言的優(yōu)雅之處

    一個小示例告訴你Python語言的優(yōu)雅之處

    本篇中, 我們展示一下一段非常小的代碼, 這段代碼十分吸引我們, 因為它使用十分優(yōu)雅和直接的方式解決了一個常見的問題.
    2014-07-07
  • Python基于network模塊制作電影人物關(guān)系圖

    Python基于network模塊制作電影人物關(guān)系圖

    這篇文章主要介紹了Python基于network模塊制作電影人物關(guān)系圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 為什么是 Python -m

    為什么是 Python -m

    這篇文章給大家介紹了Python -m的含義及python -m 和 python 的區(qū)別解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-06-06
  • python設(shè)計模式之抽象工廠模式詳解

    python設(shè)計模式之抽象工廠模式詳解

    這篇文章主要介紹了python設(shè)計模式之抽象工廠模式,通過案例分析,主要說明了該項設(shè)計模式的主要解決問題,優(yōu)缺點以及何時使用等,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Python線性分類介紹

    Python線性分類介紹

    這篇文章主要介紹了Python線性分類,線性分類指在機器學(xué)習(xí)領(lǐng)域,分類的目標(biāo)是指將具有相似特征的對象聚集。而一個線性分類器則透過特征的線性組合來做出分類決定,以達到此種目的。對象的特征通常被描述為特征值,而在向量中則描述為特征向量,需要的朋友可以參考下
    2022-02-02

最新評論