1. 程式人生 > 程式設計 >pygame實現貪吃蛇遊戲(上)

pygame實現貪吃蛇遊戲(上)

本文例項為大家分享了pygame貪吃蛇遊戲的具體程式碼,供大家參考,具體內容如下

1.準備工作

我們已經初始化了一個400*400的介面,為方便看我們的遊戲,我們先在介面上畫40*40的格子,即縱向切10份,橫向切10份,這樣我們就需要畫20個線段,下面是20個線段的畫法

for x in range(0,400,40):
 pygame.draw.line(screen,(255,255,255),(x,0),400),1)
 for y in range(0,(0,y),(400,1)

繪製後效果如下

2.蛇頭和豆子的位置

可以使用random取一個隨機位置

import random
snake_x = random.randint(0,9)*40+20
snake_y = random.randint(0,9)*40+20

繪製一個圓形的蛇頭

yellow = 255,0
pygame.draw.circle(screen,yellow,[snake_x,snake_y],20,2)

豆子的繪製類似,我們可以把豆子的圈畫小一點,把線寬畫寬一點,這樣就有一個實心的豆子

pygame.draw.circle(screen,[bean_x,bean_y],10,10)

現在看到的介面是這樣的

目前的完整程式碼是這樣的

# -*- coding=utf-8 -*-
import random
import pygame
pygame.init()
screencaption = pygame.display.set_caption('first pygame')
screen = pygame.display.set_mode((400,400)) #設定400*400視窗

snake_x = random.randint(0,9)*40+20

def get_bean_pos():
 return random.randint(0,9)*40+20,random.randint(0,9)*40+20

yellow = 255,0

bean_x,bean_y = get_bean_pos()

while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  exit()

 screen.fill((0,255)) # 將介面設定為藍色

 for x in range(0,1)

 pygame.draw.circle(screen,2)
 pygame.draw.circle(screen,10)
 pygame.display.update() # 必須呼叫update才能看到繪圖顯示

3.用鍵盤控制蛇頭的移動

匯入事件判斷的變數

from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN

在事件判斷中增加一下程式

if event.type == KEYDOWN:
 if event.key == K_LEFT:
    if snake_x-40>0: snake_x-=40
   if event.key == K_RIGHT:
    if snake_x+40<400: snake_x+=40
   if event.key == K_UP:
    if snake_y-40>0: snake_y-=40
   if event.key == K_DOWN:
    if snake_y+40<400: snake_y+=40

現在再執行程式時,已經看到可以對蛇頭進行方向的控制了

4.使蛇頭向某一方向勻速移動

首先我們定義一個用於計算時間間隔的時間戳

diff_ticks = 500 # 移動一次蛇頭的事件,單位毫秒
ticks = pygame.time.get_ticks()
ticks += diff_ticks

在主迴圈裡判斷,如果時間滿了則觸發蛇頭移動到下一個

if pygame.time.get_ticks() >= ticks:
  snake_x,snake_y = set_snake_next_pos(snake_x,snake_y)
  ticks += diff_ticks

set_snake_next_pos函式的實現如下

dire = random.randint(0,3) # 假設0、1、2、3分別代表方向左、右、上、下

def set_snake_next_pos(snake_x,snake_y):
 if dire == 0:
  if snake_x - 40 > 0:
   snake_x -= 40
 if dire == 1:
  if snake_x + 40 < 400:
   snake_x += 40
 if dire == 2:
  if snake_y - 40 > 0:
   snake_y -= 40
 if dire == 3:
  if snake_y + 40 < 400:
   snake_y += 40
 return snake_x,snake_y

此外,主迴圈裡鍵盤的判斷也要做下修改,一是要在鍵盤按下後修改移動方向,二是按下時不用馬上移動蛇頭,等待時間滿後的自動移動,判斷程式碼修改後如下

if event.type == KEYDOWN:
    if event.key == K_LEFT:
     if dire!=0 and dire!=1 and snake_x - 40 > 0: # 和當前方向不是同方向或反方向並且可以左移
      dire = 0
    if event.key == K_RIGHT:
     if dire!=0 and dire!=1 and snake_x + 40 < 400: # 和當前方向不是同方向或反方向並且可以右移
      dire = 1
    if event.key == K_UP:
     if dire!=2 and dire!=3 and snake_y - 40 > 0: # 和當前方向不是同方向或反方向並且可以上移
      dire = 2
    if event.key == K_DOWN:
     if dire!=2 and dire!=3 and snake_y + 40 < 400: # 和當前方向不是同方向或反方向並且可以下移
      dire = 3

為避免蛇頭出來就撞牆,我們對初始的蛇頭方向再做個處理,讓蛇頭往空白多的地方前進,程式碼如下

#dire = random.randint(0,3) # 假設0、1、2、3分別代表方向左、右、上、下
if snake_x < 5:
 dire = 1 # 往右移動
else:
 dire = 0 # 往左移動

5.使給蛇增加身體

我們用一個方塊做蛇的身體,身體應該是頭的後面一格,按蛇頭的移動方向放到後面一格,如果後面一個已經沒有位置了,則往垂直方向上放到上方或者下方
定義身體初始位置的程式碼如下

body_y = snake_y
if dire == 0: # 向左移動
 if snake_x + 40 < 400:
  body_x = snake_x + 40
 else: # 身體不能放右側了,只能往上下方向放
  if snake_y > 200:
   body_x = snake_x
   body_y -= 40
  else:
   body_x = snake_x
   body_y += 40
else: # 向右移動
 if snake_x - 40 > 0:
  body_x = snake_x - 40
 else: # 身體不能放左側了,只能往上下方向放
  if snake_y > 200:
   body_x = snake_x
   body_y -= 40
  else:
   body_x = snake_x
   body_y += 40

主迴圈裡增加矩形身體的繪製

pygame.draw.rect(screen,[body_x-20,body_y-20,40,40],5)

在每次更新蛇位置時可以先把身體的位置變成蛇頭的位置,再進行蛇頭移動操作

if pygame.time.get_ticks() >= ticks:
  body_x = snake_x
  body_y = snake_y
  snake_x,snake_y)
  ticks += diff_ticks

目前的效果圖如下

最後附下目前的完整程式碼,下章再介紹吃豆和身體變長部分的程式碼修改

# -*- coding=utf-8 -*-
import random
import pygame
from pygame.locals import KEYDOWN,K_DOWN
pygame.init()
screencaption = pygame.display.set_caption('first pygame')
screen = pygame.display.set_mode((400,bean_y = get_bean_pos()

diff_ticks = 500 # 移動一次蛇頭的事件,單位毫秒
ticks = pygame.time.get_ticks()
ticks += diff_ticks

#dire = random.randint(0,3) # 假設0、1、2、3分別代表方向左、右、上、下
if snake_x < 5:
 dire = 1 # 往右移動
else:
 dire = 0 # 往左移動

body_y = snake_y
if dire == 0: # 向左移動
 if snake_x + 40 < 400:
  body_x = snake_x + 40
 else: # 身體不能放右側了,只能往上下方向放
  if snake_y > 200:
   body_x = snake_x
   body_y -= 40
  else:
   body_x = snake_x
   body_y += 40
else: # 向右移動
 if snake_x - 40 > 0:
  body_x = snake_x - 40
 else: # 身體不能放左側了,只能往上下方向放
  if snake_y > 200:
   body_x = snake_x
   body_y -= 40
  else:
   body_x = snake_x
   body_y += 40

def set_snake_next_pos(snake_x,snake_y

while True:
 for event in pygame.event.get():
   if event.type == pygame.QUIT:
    pygame.quit()
    exit()
   if event.type == KEYDOWN:
    if event.key == K_LEFT:
     if dire!=0 and dire!=1 and snake_x - 40 > 0: # 和當前方向不是同方向或反方向並且可以左移
      dire = 0
    if event.key == K_RIGHT:
     if dire!=0 and dire!=1 and snake_x + 40 < 400: # 和當前方向不是同方向或反方向並且可以右移
      dire = 1
    if event.key == K_UP:
     if dire!=2 and dire!=3 and snake_y - 40 > 0: # 和當前方向不是同方向或反方向並且可以上移
      dire = 2
    if event.key == K_DOWN:
     if dire!=2 and dire!=3 and snake_y + 40 < 400: # 和當前方向不是同方向或反方向並且可以下移
      dire = 3

 screen.fill((0,40):
  pygame.draw.line(screen,2)
 pygame.draw.rect(screen,5)

 pygame.draw.circle(screen,10)

 pygame.display.update() # 必須呼叫update才能看到繪圖顯示

 if pygame.time.get_ticks() >= ticks:
  body_x = snake_x
  body_y = snake_y
  snake_x,snake_y)
  ticks += diff_ticks

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。