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

pygame實(shí)現(xiàn)貪吃蛇游戲(上)

 更新時(shí)間:2019年10月29日 09:47:35   作者:冰風(fēng)漫天  
這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了pygame貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下

1.準(zhǔn)備工作

我們已經(jīng)初始化了一個(gè)400*400的界面,為方便看我們的游戲,我們先在界面上畫(huà)40*40的格子,即縱向切10份,橫向切10份,這樣我們就需要畫(huà)20個(gè)線(xiàn)段,下面是20個(gè)線(xiàn)段的畫(huà)法

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

繪制后效果如下

2.蛇頭和豆子的位置

可以使用random取一個(gè)隨機(jī)位置

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

繪制一個(gè)圓形的蛇頭

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

豆子的繪制類(lèi)似,我們可以把豆子的圈畫(huà)小一點(diǎn),把線(xiàn)寬畫(huà)寬一點(diǎn),這樣就有一個(gè)實(shí)心的豆子

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

現(xiàn)在看到的界面是這樣的

目前的完整代碼是這樣的

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

snake_x = random.randint(0,9)*40+20
snake_y = 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,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,0,255)) # 將界面設(shè)置為藍(lán)色

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

 pygame.draw.circle(screen,yellow,[snake_x,snake_y],20,2)
 pygame.draw.circle(screen,yellow,[bean_x,bean_y],10,10)
 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示

3.用鍵盤(pán)控制蛇頭的移動(dòng)

導(dǎo)入事件判斷的變量

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

現(xiàn)在再運(yùn)行程序時(shí),已經(jīng)看到可以對(duì)蛇頭進(jìn)行方向的控制了

4.使蛇頭向某一方向勻速移動(dòng)

首先我們定義一個(gè)用于計(jì)算時(shí)間間隔的時(shí)間戳

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

在主循環(huán)里判斷,如果時(shí)間滿(mǎn)了則觸發(fā)蛇頭移動(dòng)到下一個(gè)

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函數(shù)的實(shí)現(xiàn)如下

dire = random.randint(0,3) # 假設(shè)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

此外,主循環(huán)里鍵盤(pán)的判斷也要做下修改,一是要在鍵盤(pán)按下后修改移動(dòng)方向,二是按下時(shí)不用馬上移動(dòng)蛇頭,等待時(shí)間滿(mǎn)后的自動(dòng)移動(dòng),判斷代碼修改后如下

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

為避免蛇頭出來(lái)就撞墻,我們對(duì)初始的蛇頭方向再做個(gè)處理,讓蛇頭往空白多的地方前進(jìn),代碼如下

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

5.使給蛇增加身體

我們用一個(gè)方塊做蛇的身體,身體應(yīng)該是頭的后面一格,按蛇頭的移動(dòng)方向放到后面一格,如果后面一個(gè)已經(jīng)沒(méi)有位置了,則往垂直方向上放到上方或者下方
定義身體初始位置的代碼如下

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

主循環(huán)里增加矩形身體的繪制

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

在每次更新蛇位置時(shí)可以先把身體的位置變成蛇頭的位置,再進(jìn)行蛇頭移動(dòng)操作

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

目前的效果圖如下

最后附下目前的完整代碼,下章再介紹吃豆和身體變長(zhǎng)部分的代碼修改

# -*- coding=utf-8 -*-
import random
import pygame
from pygame.locals import KEYDOWN,K_LEFT,K_RIGHT,K_UP,K_DOWN
pygame.init()
screencaption = pygame.display.set_caption('first pygame')
screen = pygame.display.set_mode((400,400)) #設(shè)置400*400窗口

snake_x = random.randint(0,9)*40+20
snake_y = 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,255,0

bean_x,bean_y = get_bean_pos()

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

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

body_y = snake_y
if dire == 0: # 向左移動(dòng)
 if snake_x + 40 < 400:
  body_x = snake_x + 40
 else: # 身體不能放右側(cè)了,只能往上下方向放
  if snake_y > 200:
   body_x = snake_x
   body_y -= 40
  else:
   body_x = snake_x
   body_y += 40
else: # 向右移動(dòng)
 if snake_x - 40 > 0:
  body_x = snake_x - 40
 else: # 身體不能放左側(cè)了,只能往上下方向放
  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):
 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

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: # 和當(dāng)前方向不是同方向或反方向并且可以左移
      dire = 0
    if event.key == K_RIGHT:
     if dire!=0 and dire!=1 and snake_x + 40 < 400: # 和當(dāng)前方向不是同方向或反方向并且可以右移
      dire = 1
    if event.key == K_UP:
     if dire!=2 and dire!=3 and snake_y - 40 > 0: # 和當(dāng)前方向不是同方向或反方向并且可以上移
      dire = 2
    if event.key == K_DOWN:
     if dire!=2 and dire!=3 and snake_y + 40 < 400: # 和當(dāng)前方向不是同方向或反方向并且可以下移
      dire = 3

 screen.fill((0,0,255)) # 將界面設(shè)置為藍(lán)色

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

 pygame.draw.circle(screen,yellow,[snake_x,snake_y],20,2)
 pygame.draw.rect(screen,yellow,[body_x-20,body_y-20,40,40],5)

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

 pygame.display.update() # 必須調(diào)用update才能看到繪圖顯示

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論