溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-18 20:44:35 來源:腳本之家 閱讀:209 作者:monster_hahaha 欄目:開發(fā)技術(shù)

今天看到幾個(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.原來的頭換到身體最前端
  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)

效果:

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

總結(jié)

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI