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

Python貪吃蛇小游戲?qū)嵗窒?/h1>
 更新時(shí)間:2021年09月06日 10:55:57   作者:Maggie晨曦  
這篇文章主要為大家詳細(xì)介紹了Python貪吃蛇小游戲?qū)嵗?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

使用的庫(kù)

pygame 、random 、pyautogui

流程簡(jiǎn)述

1.設(shè)置初始參數(shù)

設(shè)置每個(gè)網(wǎng)格大小為 20 px ,size可以隨意修改但最好為20的倍數(shù),設(shè)置初始方向向右,初始蛇長(zhǎng)為 3 。

# 初始化參數(shù)
size = 320
screen = pygame.display.set_mode([size,size],0 , 32)
pygame.display.set_caption("貪吃蛇")
cell_size = 20
cell_num = int(size/20)
x , y = [60,0]
# 初始方向向右
dir_snake = 'R'
body_snake = [[0,0],[20,0],[40,0],[60,0]]
# 初始蛇長(zhǎng)為3
len_snake = 3
# 初始食物坐標(biāo)
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
# 蛇移動(dòng)速度
clock = pygame.time.Clock()

2.鍵盤控制

鍵盤上下左右控制蛇方向,禁止反向。

3.食物

蛇頭吃到食物后,蛇長(zhǎng)加一 ,random 一個(gè)隨機(jī)坐標(biāo),如果坐標(biāo)再蛇身上則繼續(xù) random。

if [x,y] == food_coor[:2]:
    food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
    if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]:
        food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
    len_snake +=1

4. 勝負(fù)判斷

蛇頭與蛇身碰撞判輸,彈出游戲結(jié)束窗口,說(shuō)明最終蛇長(zhǎng)為多長(zhǎng)。

if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:
    pyautogui.alert(text='游戲結(jié)束,最終蛇長(zhǎng)為{}'.format(len_snake))
    exit()

代碼及結(jié)果

代碼

import pygame , random ,pyautogui
from pygame.locals import *
# 初始化參數(shù)
size = 500
screen = pygame.display.set_mode([size,size],0 , 32)
pygame.display.set_caption("貪吃蛇")
cell_size = 20
cell_num = int(size/20)
x , y = [60,0]
# 初始方向向右
dir_snake = 'R'
body_snake = [[0,0],[20,0],[40,0],[60,0]]
# 初始蛇長(zhǎng)為3
len_snake = 3
# 初始食物坐標(biāo)
food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
# 蛇移動(dòng)速度
clock = pygame.time.Clock()
# bg
def bg():
    for i in range(0, size , int(cell_size)):
        pygame.draw.line(screen,[72,72,72],[i,0],[i,size])
    for i in range(0, size, int(cell_size)):
        pygame.draw.line(screen, [72, 72, 72], [0, i], [size,i])
# 蛇
def snake(K):
    for x,y in K[:-len_snake - 1:-1]:
        pygame.draw.rect(screen,[255,255,255],[x,y,20,20],0)
# 食物
def food():
    pygame.draw.rect(screen,[255,0,0],food_coor,0)
# 主循環(huán)
def running():
    global x ,y , dir_snake ,food_coor , len_snake
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        screen.fill((40, 43, 46))
        # 方向規(guī)則
        if dir_snake == 'U':
            y -= 20
        elif dir_snake == 'R':
            x += 20
        elif dir_snake == 'D':
            y += 20
        elif dir_snake == 'L':
            x -= 20
        # 鍵盤方向控制
        if event.type == KEYDOWN:
            if event.key == K_LEFT and dir_snake != 'R':
                dir_snake = 'L'
            elif event.key == K_DOWN and dir_snake != 'U':
                dir_snake = 'D'
            elif event.key == K_RIGHT and dir_snake != 'L':
                dir_snake = 'R'
            elif event.key == K_UP and dir_snake != 'D':
                dir_snake = 'U'
        # 撞墻之后
        if x < 0:
            x += size
        elif x >= size:
            x -= size
        elif y < 0:
            y += size
        elif y >= size:
            y -= size
        # draw蛇體
        body_snake.append([x,y])
        snake(body_snake)
        food()
        # 吃到食物后food換位置
        if [x,y] == food_coor[:2]:
            food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
            if food_coor[:2] in body_snake[-2:-len_snake -1 :-1]:
                food_coor = [random.randint(0, cell_num - 1) * 20, random.randint(0, cell_num - 1) * 20, 20, 20]
            len_snake +=1
        # 游戲結(jié)束
        if body_snake[-1] in body_snake[-2:-len_snake -1 :-1]:
            pyautogui.alert(text='游戲結(jié)束,最終蛇長(zhǎng)為{}'.format(len_snake))
            exit()
        # 格線
        bg()
        # 右下角顯示蛇長(zhǎng)
        font = pygame.font.SysFont("simsunnsimsun", 40)
        text_surface = font.render("{}".format(len_snake), True, (255,255, 255))
        screen.blit(text_surface , (size-40,size-40))
        pygame.display.update()
        # 蛇的移動(dòng)速度隨著蛇的長(zhǎng)度而越來(lái)越快
        clock.tick(len_snake * 2)

if __name__ == '__main__':
    pygame.init()
    running()

輸出結(jié)果

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

相關(guān)文章

  • Python?中?yeild?的用法詳解

    Python?中?yeild?的用法詳解

    yield?是?Python?中的關(guān)鍵字,用于生成器函數(shù)中,可以將函數(shù)變成一個(gè)迭代器,實(shí)現(xiàn)惰性計(jì)算,節(jié)省內(nèi)存空間。本文將介紹?yield?的基本用法和實(shí)現(xiàn)原理,以及與?yield?相關(guān)的注意事項(xiàng)和常見問(wèn)題。
    2023-06-06
  • 使用keras框架cnn+ctc_loss識(shí)別不定長(zhǎng)字符圖片操作

    使用keras框架cnn+ctc_loss識(shí)別不定長(zhǎng)字符圖片操作

    這篇文章主要介紹了使用keras框架cnn+ctc_loss識(shí)別不定長(zhǎng)字符圖片操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python用二分法求平方根的案例

    Python用二分法求平方根的案例

    這篇文章主要介紹了Python用二分法求平方根的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python基礎(chǔ)面向?qū)ο笾^承與派生詳解

    Python基礎(chǔ)面向?qū)ο笾^承與派生詳解

    這篇文章主要為大家介紹了Python基礎(chǔ)面向?qū)ο笾^承與派生詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • python中assert用法實(shí)例分析

    python中assert用法實(shí)例分析

    這篇文章主要介紹了python中assert用法,實(shí)例分析了assert的功能及相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • pytorch實(shí)現(xiàn)CNN卷積神經(jīng)網(wǎng)絡(luò)

    pytorch實(shí)現(xiàn)CNN卷積神經(jīng)網(wǎng)絡(luò)

    這篇文章主要為大家詳細(xì)介紹了pytorch實(shí)現(xiàn)CNN卷積神經(jīng)網(wǎng)絡(luò),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • Python調(diào)用C/C++動(dòng)態(tài)鏈接庫(kù)的方法詳解

    Python調(diào)用C/C++動(dòng)態(tài)鏈接庫(kù)的方法詳解

    這篇文章主要介紹了Python調(diào)用C/C++動(dòng)態(tài)鏈接庫(kù)的方法,需要的朋友可以參考下
    2014-07-07
  • python求最大值,不使用內(nèi)置函數(shù)的實(shí)現(xiàn)方法

    python求最大值,不使用內(nèi)置函數(shù)的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python求最大值,不使用內(nèi)置函數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • 對(duì)python周期性定時(shí)器的示例詳解

    對(duì)python周期性定時(shí)器的示例詳解

    今天小編就為大家分享一篇對(duì)python周期性定時(shí)器的示例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • pytorch permute維度轉(zhuǎn)換方法

    pytorch permute維度轉(zhuǎn)換方法

    今天小編就為大家分享一篇pytorch permute維度轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12

最新評(píng)論