1. 程式人生 > 程式設計 >使用Python第三方庫pygame寫個貪吃蛇小遊戲

使用Python第三方庫pygame寫個貪吃蛇小遊戲

今天看到幾個關於pygame模組的部落格和視訊,感覺非常有趣,這裡照貓畫虎寫了一個貪吃蛇小遊戲,目前還有待完善,但是基本遊戲功能已經實現,下面是程式碼:

# 匯入模組
import pygame
import random
 # 初始化
pygame.init()
w = 720   #視窗寬度
h = 600   #視窗高度
ROW = 30  #行數
COL = 36  #列數
#將所有的座標看作是一個個點,定義點類
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)
#顯示視窗和標題
size = (w,h)
window = pygame.display.set_mode(size)
pygame.display.set_caption('貪吃蛇')
#定義蛇頭座標
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),col = head.col+3)
]
#產生食物
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,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
# 遊戲迴圈
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.刪除身體最後一個
  if not eat:
    snake_list.pop()
  #移動
  if dire == 'left':
    head.col -= 1
  elif dire == 'right':
    head.col += 1
  elif dire == 'up':
    head.row -= 1
  elif dire == 'down':
    head.row += 1
  #檢測:
  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,w,h))
  #蛇頭
  rect(head,head_color)
  #食物
  rect(food,food_color)
  #蛇身
  for snake in snake_list:
    rect(snake,snake_color)
  pygame.display.flip()
  #遊戲幀數
  clock.tick(20)

效果:

使用Python第三方庫pygame寫個貪吃蛇小遊戲

總結

到此這篇關於使用Python第三方庫pygame寫個貪吃蛇小遊戲的文章就介紹到這了,更多相關python 貪吃蛇遊戲內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!