欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python第三方庫(kù)pygame寫個(gè)貪吃蛇小游戲

 更新時(shí)間:2020年03月06日 12:12:47   作者:monster_hahaha  
這篇文章主要介紹了使用Python第三方庫(kù)pygame寫個(gè)貪吃蛇小游戲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

今天看到幾個(gè)關(guān)于pygame模塊的博客和視頻,感覺非常有趣,這里照貓畫虎寫了一個(gè)貪吃蛇小游戲,目前還有待完善,但是基本游戲功能已經(jīng)實(shí)現(xiàn),下面是代碼:

# 導(dǎo)入模塊
import pygame
import random
 # 初始化
pygame.init()
w = 720   #窗口寬度
h = 600   #窗口高度
ROW = 30  #行數(shù)
COL = 36  #列數(shù)
#將所有的坐標(biāo)看作是一個(gè)個(gè)點(diǎn),定義點(diǎn)類
class Point:   
  row = 0
  col = 0
  def __init__(self, row, col):
    self.row = row
    self.col = col
  def copy(self):
    return Point(row = self.row,col = self.col)
#顯示窗口和標(biāo)題
size = (w, h)
window = pygame.display.set_mode(size)
pygame.display.set_caption('貪吃蛇')
#定義蛇頭坐標(biāo)
head = Point(row = ROW/2, col = COL/2)
#蛇身體
snake_list = [
  Point(row = head.row,col = head.col+1),
  Point(row = head.row,col = head.col+2),
  Point(row = head.row,col = head.col+3)
]
#產(chǎn)生食物
def pro_food():
  #食物不能與蛇重疊
  while True:
    pos = Point(row=random.randint(1,ROW-2), col=random.randint(1,COL-2))
    is_coll = False
    if head.row == pos.row and head.col == pos.col:
      is_coll = True
    for snake in snake_list:
      if snake.col == pos.col and snake.row == pos.row:
        is_coll = True
        break
    if not is_coll:
      return pos
food = pro_food()
#定義顏色
bg_color = (255, 255, 255)
head_color = (0, 128, 128)
food_color = (255, 255, 0)
snake_color = (200,200,200)
#給定初始方向
dire = 'left'
def rect(point, color):
  cell_width = w/COL
  cell_height = h/ROW
  left = point.col*cell_width
  top = point.row*cell_height
  pygame.draw.rect(
    window, color,
    (left,top,cell_width, cell_height, )
  )
  pass
# 游戲循環(huán)
quit = True
clock = pygame.time.Clock()
while quit:
  for event in pygame.event.get():
    #退出方式
    if event.type == pygame.QUIT:
      quit = False
    elif event.type == pygame.KEYDOWN:
      #鍵盤控制
      if event.key == 273 or event.key == 119:
        if dire == 'left' or dire == 'right':
          dire = 'up'
      elif event.key == 274 or event.key == 115:
        if dire == 'left' or dire == 'right':
          dire = 'down'
      elif event.key == 276 or event.key == 97:
        if dire == 'up' or dire == 'down':
          dire = 'left'
      elif event.key == 275 or event.key == 100:
        if dire == 'up' or dire == 'down':
          dire = 'right'
  #吃
  eat=(head.row == food.row and head.col == food.col)
  if eat:
    food = pro_food()
  #處理身體
  #1.原來(lái)的頭換到身體最前端
  snake_list.insert(0,head.copy())
  #2.刪除身體最后一個(gè)
  if not eat:
    snake_list.pop()
  #移動(dòng)
  if dire == 'left':
    head.col -= 1
  elif dire == 'right':
    head.col += 1
  elif dire == 'up':
    head.row -= 1
  elif dire == 'down':
    head.row += 1
  #檢測(cè):
  dead=False
  #1.撞墻
  if head.col < 0 or head.row< 0 or head.col >= COL or head.row >= ROW:
    dead=True
  #2.撞自己
  for snake in snake_list:
    if head.col == snake.col and head.row == snake.row:
      dead=True
      break
  if dead:
    print('dead')
    quit = False
  #繪制背景
  pygame.draw.rect(window, bg_color, (0, 0, w, h))
  #蛇頭
  rect(head, head_color)
  #食物
  rect(food,food_color)
  #蛇身
  for snake in snake_list:
    rect(snake,snake_color)
  pygame.display.flip()
  #游戲幀數(shù)
  clock.tick(20)

效果:

總結(jié)

到此這篇關(guān)于使用Python第三方庫(kù)pygame寫個(gè)貪吃蛇小游戲的文章就介紹到這了,更多相關(guān)python 貪吃蛇游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器的步驟詳解

    Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器的步驟詳解

    這篇文章主要介紹了Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器,,本文分場(chǎng)景通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 詳解Pandas與openpyxl庫(kù)的超強(qiáng)結(jié)合

    詳解Pandas與openpyxl庫(kù)的超強(qiáng)結(jié)合

    Pandas絕對(duì)是Python中處理Excel最快、最好用的庫(kù),但是使用 openpyxl 的一些優(yōu)勢(shì)是能夠輕松地使用樣式、條件格式等自定義電子表格,感興趣的可以了解一下
    2021-09-09
  • pycharm重置設(shè)置,恢復(fù)默認(rèn)設(shè)置的方法

    pycharm重置設(shè)置,恢復(fù)默認(rèn)設(shè)置的方法

    今天小編就為大家分享一篇pycharm重置設(shè)置,恢復(fù)默認(rèn)設(shè)置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-10-10
  • python 實(shí)現(xiàn)快速生成連續(xù)、隨機(jī)字母列表

    python 實(shí)現(xiàn)快速生成連續(xù)、隨機(jī)字母列表

    今天小編就為大家分享一篇python 實(shí)現(xiàn)快速生成連續(xù)、隨機(jī)字母列表,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2019-11-11
  • python實(shí)現(xiàn)事件驅(qū)動(dòng)

    python實(shí)現(xiàn)事件驅(qū)動(dòng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)事件驅(qū)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 詳解Python sys.argv使用方法

    詳解Python sys.argv使用方法

    在本文中我們給大家詳細(xì)講解了關(guān)于Python sys.argv使用方法以及注意事項(xiàng),有此需要的讀者們跟著學(xué)習(xí)下。
    2019-05-05
  • 解決Matplotlib中文顯示亂碼的完整教程

    解決Matplotlib中文顯示亂碼的完整教程

    在使用 Matplotlib 繪制圖表時(shí),很多開發(fā)者都會(huì)遇到中文無(wú)法正常顯示的問題,默認(rèn)情況下,Matplotlib 并不支持中文字符,需要手動(dòng)設(shè)置字體,這篇博客將教你如何解決 Matplotlib 繪圖顯示中文的常見問題,確保中文字符能夠在圖表中正確顯示,需要的朋友可以參考下
    2024-12-12
  • 用python爬取今日說法每期數(shù)據(jù)

    用python爬取今日說法每期數(shù)據(jù)

    大家好,本篇文章主要講的是用python爬取今日說法每期數(shù)據(jù),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • python十進(jìn)制轉(zhuǎn)二進(jìn)制的詳解

    python十進(jìn)制轉(zhuǎn)二進(jìn)制的詳解

    在本篇文章里小編給大家整理了關(guān)于python十進(jìn)制轉(zhuǎn)二進(jìn)制的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。
    2020-02-02
  • python人工智能tensorflow函數(shù)tf.layers.dense使用方法

    python人工智能tensorflow函數(shù)tf.layers.dense使用方法

    這篇文章主要介紹了python人工智能tensorflow函數(shù)tf.layers.dense的使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論