主頁 > 知識(shí)庫 > Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)

Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)

熱門標(biāo)簽:萊蕪電信外呼系統(tǒng) 銀川電話機(jī)器人電話 怎么辦理400客服電話 鶴壁手機(jī)自動(dòng)外呼系統(tǒng)違法嗎 沈陽防封電銷電話卡 高德地圖標(biāo)注收入咋樣 地圖標(biāo)注多個(gè) B52系統(tǒng)電梯外呼顯示E7 企業(yè)微信地圖標(biāo)注

簡單用py寫了一個(gè)貪吃蛇游戲,有單人、雙人模式,比較簡單,適合初學(xué)者練手。本上每行重要的語句都有注釋,做了什么事一目了然

這里介紹雙人模式

單人模式戳這里:Python簡易貪吃蛇小游戲(單人模式)

一、游戲設(shè)計(jì)要點(diǎn)

1.游戲主體窗口(尺寸)、畫布(尺寸、位置)、按鈕(尺寸、位置)、文字(大小、顏色、位置)、圖像、背景音樂及相關(guān)響應(yīng)函數(shù)(主要是鼠標(biāo)移動(dòng)及點(diǎn)擊的響應(yīng))的設(shè)計(jì)與合理排布
2.蛇與食物的類的屬性設(shè)計(jì)
3.蛇位置的更新(根據(jù)鍵盤輸入)、吃到食物加分的判定、食物的更新
4.蛇死亡的判定條件設(shè)計(jì)

二、主要模塊

1.pygame
2.sys
3.random

三、用到的類

1.Snake類,定義蛇頭蛇身元素的位置
2.Food類,定義食物元素的位置及單個(gè)元素的顏色

四、主要函數(shù)

1.new_food(),功能:生成一個(gè)不與蛇頭位置重合的食物并返回該食物對(duì)象

def new_food(head):
    while True:
        # 循環(huán),不斷實(shí)例化new_food對(duì)象直到生成一個(gè)不與蛇頭重合的食物
        new_food = Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))
        # 若new_food和蛇頭重合則不創(chuàng)鍵
        if new_food.x != head.x and new_food.y != head.y:
            break
        else:
            continue
    return new_food

2.draw_snake()、draw_food()函數(shù),功能:繪制蛇與食物的圖像,傳入?yún)?shù)為顏色和對(duì)象:

# 在窗體中繪制貪吃蛇
# 形參:一個(gè)是顏色另一個(gè)是實(shí)例化對(duì)象
def draw_snake(color, object):
    pygame.draw.circle(window, color, (object.x, object.y), 10)
    
# 在窗體中繪制食物
# 形參:實(shí)例化對(duì)象
def draw_food(food):
    pygame.draw.circle(window, food.color, (food.x, food.y), 10)

3.show_end函數(shù),功能:顯示雙人模式結(jié)束時(shí)的得分界面:

def show_end():
    while True:
        window.blit(init_background, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_end()
        # 定義標(biāo)題
        pygame.display.set_caption("貪吃蛇大冒險(xiǎn)")
        # 定義提示文字
        font = pygame.font.SysFont("simHei", 40)
        fontsurf = font.render('游戲結(jié)束! 玩家1得分: %s 玩家2得分:%s' % (score1, score2), True, black)
        window.blit(fontsurf, (150, 100))
        button("返回主菜單", 370, 300, 200, 40, blue, brightred, into_game)
        button("退出游戲", 370, 470, 200, 40, red, brightred, exit_end)
        pygame.display.update()
        clock.tick(20)

4.exit_end()函數(shù),功能:在初始界面和游戲結(jié)束顯示得分界面點(diǎn)擊右上角的"×"時(shí),直接退出整個(gè)游戲:

# 初始界面和游戲中途點(diǎn)擊退出游戲時(shí)
def exit_end():
    pygame.quit()
    sys.exit()

5.start_game_double()函數(shù),功能:實(shí)現(xiàn)雙人正常模式:

def start_game_double():
    # 播放音樂
    pygame.mixer.music.play(-1)
    # 定義存分?jǐn)?shù)的全局變量
    global score1
    global score2
    score1 = score2 = 0
    # 初始化存放玩家鍵盤輸入運(yùn)動(dòng)方向的變量
    run_direction1 = "right"
    run_direction2 = "up"
    # 初始化貪吃蛇運(yùn)動(dòng)方向的變量
    run1 = run_direction1
    run2 = run_direction2
    # 實(shí)例化貪吃蛇和食物對(duì)象
    head1 = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
    head2 = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
    # 實(shí)例化蛇身長度為2個(gè)單位
    snake_body1 = [Snake(head1.x, head1.y + 20), Snake(head1.x, head1.y + 40)]
    snake_body2 = [Snake(head2.x, head2.y + 20), Snake(head2.x, head2.y + 40)]
    # 實(shí)例化食物列表,列表隨著其中食物被吃掉應(yīng)該不斷縮短
    food_list = [Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))]
    for i in range(1,24):
        food_list.append(Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255))))
    # 實(shí)例化單個(gè)食物,方便循環(huán)內(nèi)生成單個(gè)新食物
    food = Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))
    while True:
        window.blit(background, (0,0))
        # 監(jiān)聽玩家鍵盤輸入的運(yùn)動(dòng)方向值,并根據(jù)輸入轉(zhuǎn)為up、down、right或left,方便程序中調(diào)用
        # pygame.event.get()返回一個(gè)列表,存放本次game執(zhí)行中程序遇到的一連串事件(按時(shí)間順序依次存放)
        for event in pygame.event.get():
            # pygame.QUIT事件是指用戶點(diǎn)擊窗口右上角的"×"
            if event.type == pygame.QUIT:
                # 顯示結(jié)果界面
                show_end()
            # 若事件類型是按下鍵盤,分↑ ↓ ← →四種情況討論
            elif event.type == pygame.KEYDOWN:
                # 若事件類型是按下鍵盤↑
                # key是鍵值,表示按下去的鍵值是什么
                if event.key == pygame.K_UP:
                    run_direction2 = "up"
                # 若事件類型是按下鍵盤↓
                if event.key == pygame.K_DOWN:
                    run_direction2 = "down"
                # 若事件類型是按下鍵盤←
                if event.key == pygame.K_LEFT:
                    run_direction2 = "left"
                # 若事件類型是按下鍵盤→
                if event.key == pygame.K_RIGHT:
                    run_direction2 = "right"
                # 若事件類型是按下鍵盤↑
                if event.key == ord('w'):
                    run_direction1 = "up"
                # 若事件類型是按下鍵盤↓
                if event.key == ord('s'):
                    run_direction1 = "down"
                # 若事件類型是按下鍵盤←
                if event.key == ord('a'):
                    run_direction1 = "left"
                # 若事件類型是按下鍵盤→
                if event.key == ord('d'):
                    run_direction1 = "right"
        # 繪制初始化的25個(gè)食物圖像(24+1=25)
        # 隨著該列表中的食物被吃掉,列表應(yīng)該不斷pop以清除已經(jīng)被吃的事物
        for item in food_list:
            draw_food(item)
        # 繪制被貪吃蛇吃掉后新增的食物圖像
        draw_food(food)
        # 繪制蛇頭圖像
        # 在繪制蛇頭之前先檢查是不是已經(jīng)死亡,如果已死亡,則不繪制
        # ?。〔荒芡ㄟ^die_flag判斷是否死亡因?yàn)槊看窝h(huán)一開頭die_flag都初始化為False
        # 因此最好的方法是通過snake_body是否為空判斷
        if len(snake_body1) != 0:
            draw_snake(black, head1)
        if len(snake_body2) != 0:
            draw_snake(black, head2)
        # 繪制蛇身圖像
        for item in snake_body1:
            draw_snake(blue, item)
        for item in snake_body2:
            draw_snake(green, item)
        # 若蛇未死亡,則插入蛇頭位置到蛇身列表中
        # 即:若蛇已死亡,則保持snake_body為空不變
        if len(snake_body1) != 0:
            snake_body1.insert(0, Snake(head1.x, head1.y))
        if len(snake_body2) != 0:
            snake_body2.insert(0, Snake(head2.x, head2.y))
        # 判斷貪吃蛇原運(yùn)動(dòng)方向(run)與玩家鍵盤輸入的運(yùn)動(dòng)方向(run_direction)是否違反正常運(yùn)動(dòng)情況
        if run1 == "up" and not run_direction1 == "down":
            run1 = run_direction1
        if run1 == "down" and not run_direction1 == "up":
            run1 = run_direction1
        if run1 == "left" and not run_direction1 == "right":
            run1 = run_direction1
        if run1 == "right" and not run_direction1 == "left":
            run1 = run_direction1
        if run2 == "up" and not run_direction2 == "down":
            run2 = run_direction2
        if run2 == "down" and not run_direction2 == "up":
            run2 = run_direction2
        if run2 == "left" and not run_direction2 == "right":
            run2 = run_direction2
        if run2 == "right" and not run_direction2 == "left":
            run2 = run_direction2
        # 根據(jù)玩家鍵入方向進(jìn)行蛇頭坐標(biāo)的更新
        if run1 == "up":
            head1.y -= 20
        if run1 == "down":
            head1.y += 20
        if run1 == "left":
            head1.x -= 20
        if run1 == "right":
            head1.x += 20
        if run2 == "up":
            head2.y -= 20
        if run2 == "down":
            head2.y += 20
        if run2 == "left":
            head2.x -= 20
        if run2 == "right":
            head2.x += 20
        # 判斷兩條蛇是否死亡
        # 初始化四個(gè)死亡標(biāo)志為False
        die_flag1 = die_flag2 = False
        # 此時(shí)snake_body1,2中均已包含蛇頭
        # snake_body1,2第一個(gè)元素是蛇頭,故不能從0號(hào)元素開始比較
        # 因?yàn)樵撋呱哳^必然和自己重合
        # 這里snake_body1,2均從1號(hào)元素開始
        # 所以snake_body1[1:]+snake_body2[1:]是純粹存儲(chǔ)蛇身的列表
        for body in snake_body1[1:]+snake_body2[1:]:
            if head1.x == body.x and head1.y == body.y:
                die_flag1 = True
            if head2.x == body.x and head2.y == body.y:
                die_flag2 = True
        if die_flag1 == True or head1.x  0 or head1.x > 960 or head1.y  0 or head1.y > 600:
            # 注意:這邊雖然蛇身列表清空,但head1對(duì)象仍存在
            # 故必須要在上面的繪制蛇頭代碼前面加上if先判斷蛇是否死亡
            snake_body1.clear()
        if die_flag2 == True or head2.x  0 or head2.x > 960 or head2.y  0 or head2.y > 600:
            die_flag2 = True
            # 注意:這邊雖然蛇身列表清空,但head1對(duì)象仍存在
            # 故必須要在上面的繪制蛇頭代碼前面加上if先判斷蛇是否死亡
            snake_body2.clear()
        # 若兩條蛇都死亡
        # 同樣地,只能通過snake_body是否為空判斷蛇是否死亡
        if len(snake_body1) == 0 and len(snake_body2) == 0:
            show_end()
        # 判斷蛇頭和食物坐標(biāo),若相等,則加分,并生成新的食物
        # 定義標(biāo)志,表明是否找到和蛇頭相等的食物
        global flag1
        global flag2
        flag1 = flag2 = 0
        # 如果蛇頭和食物重合
        for item in food_list:
            # 在蛇1沒死且蛇頭1和某一食物坐標(biāo)相等的條件下
            if len(snake_body1) != 0 and (head1.x == item.x and head1.y == item.y or head1.x == food.x and head1.y == food.y):
                flag1 = 1
                score1 += 1
                # 彈出被吃掉的這個(gè)食物
                food_list.pop(food_list.index(item))
                # 再產(chǎn)生一個(gè)食物
                food = new_food(head1)
                # 把新食物插入food_list,下一次循環(huán)中會(huì)更新繪制食物全體
                food_list.append(food)
                break
            # 在蛇2沒死的且蛇頭2和某一食物坐標(biāo)相等的條件下
            elif len(snake_body2) != 0 and head2.x == item.x and head2.y == item.y or head2.x == food.x and head2.y == food.y:
                flag2 = 1
                score2 += 1
                # 彈出被吃掉的這個(gè)食物
                food_list.pop(food_list.index(item))
                # 再產(chǎn)生一個(gè)食物
                food = new_food(head2)
                # 把新食物插入food_list,下一次循環(huán)中會(huì)更新繪制食物全體
                food_list.append(food)
                break
        # 蛇1必須沒死,否則pop會(huì)引發(fā)異常
        if len(snake_body1) != 0 and flag1 == 0:
            snake_body1.pop()
        # 蛇2必須沒死,否則pop會(huì)引發(fā)異常
        if len(snake_body2) != 0 and flag2 == 0:
            snake_body2.pop ()
        font = pygame.font.SysFont("simHei", 25)
        mode_title1 = mode_title2 = font.render('正常模式', False, grey)
        socre_title1 = font.render('得分: %s' % score1, False, grey)
        socre_title2 = font.render('得分: %s' % score2, False, grey)
        window.blit(mode_title1, (50, 30))
        window.blit(socre_title1, (50, 65))
        window.blit(mode_title2, (800, 30))
        window.blit(socre_title2, (800, 65))
        # 更新蛇頭蛇身和食物的數(shù)據(jù)
        pygame.display.update()
        # 通過幀率設(shè)置貪吃蛇速度
        clock.tick(8)

6.start_kgame_double()函數(shù),功能:實(shí)現(xiàn)雙人穿墻模式:

def start_kgame_double():
    # 播放音樂
    pygame.mixer.music.play(-1)
    # 定義存分?jǐn)?shù)的全局變量
    global score1
    global score2
    score1 = score2 = 0
    # 初始化存放玩家鍵盤輸入運(yùn)動(dòng)方向的變量
    run_direction1 = "right"
    run_direction2 = "up"
    # 初始化貪吃蛇運(yùn)動(dòng)方向的變量
    run1 = run_direction1
    run2 = run_direction2
    # 實(shí)例化貪吃蛇和食物對(duì)象
    head1 = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
    head2 = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
    # 實(shí)例化蛇身長度為2個(gè)單位
    snake_body1 = [Snake(head1.x, head1.y + 20), Snake(head1.x, head1.y + 40)]
    snake_body2 = [Snake(head2.x, head2.y + 20), Snake(head2.x, head2.y + 40)]
    # 實(shí)例化食物列表,列表隨著其中食物被吃掉應(yīng)該不斷縮短
    food_list = [Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))]
    for i in range(1,24):
        food_list.append(Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255))))
    # 實(shí)例化單個(gè)食物,方便循環(huán)內(nèi)生成單個(gè)新食物
    food = Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))
    while True:
        window.blit(background, (0,0))
        # 監(jiān)聽玩家鍵盤輸入的運(yùn)動(dòng)方向值,并根據(jù)輸入轉(zhuǎn)為up、down、right或left,方便程序中調(diào)用
        # pygame.event.get()返回一個(gè)列表,存放本次game執(zhí)行中程序遇到的一連串事件(按時(shí)間順序依次存放)
        for event in pygame.event.get():
            # pygame.QUIT事件是指用戶點(diǎn)擊窗口右上角的"×"
            if event.type == pygame.QUIT:
                # 顯示結(jié)果界面
                show_end()
            # 若事件類型是按下鍵盤,分↑ ↓ ← →四種情況討論
            elif event.type == pygame.KEYDOWN:
                # 若事件類型是按下鍵盤↑
                # key是鍵值,表示按下去的鍵值是什么
                if event.key == pygame.K_UP:
                    run_direction2 = "up"
                # 若事件類型是按下鍵盤↓
                if event.key == pygame.K_DOWN:
                    run_direction2 = "down"
                # 若事件類型是按下鍵盤←
                if event.key == pygame.K_LEFT:
                    run_direction2 = "left"
                # 若事件類型是按下鍵盤→
                if event.key == pygame.K_RIGHT:
                    run_direction2 = "right"
                # 若事件類型是按下鍵盤↑
                if event.key == ord('w'):
                    run_direction1 = "up"
                # 若事件類型是按下鍵盤↓
                if event.key == ord('s'):
                    run_direction1 = "down"
                # 若事件類型是按下鍵盤←
                if event.key == ord('a'):
                    run_direction1 = "left"
                # 若事件類型是按下鍵盤→
                if event.key == ord('d'):
                    run_direction1 = "right"
        # 繪制初始化的25個(gè)食物圖像(24+1=25)
        # 隨著該列表中的食物被吃掉,列表應(yīng)該不斷pop以清除已經(jīng)被吃的事物
        for item in food_list:
            draw_food(item)
        # 繪制被貪吃蛇吃掉后新增的食物圖像
        draw_food(food)
        # 繪制蛇頭圖像
        # 在繪制蛇頭之前先檢查是不是已經(jīng)死亡,如果已死亡,則不繪制
        if len(snake_body1) != 0:
            draw_snake(black, head1)
        if len(snake_body2) != 0:
            draw_snake(black, head2)
        # 繪制蛇身圖像
        for item in snake_body1:
            draw_snake(blue, item)
        for item in snake_body2:
            draw_snake(green, item)
        # 插入蛇頭位置到蛇身列表中
        if len(snake_body1) != 0:
            snake_body1.insert(0, Snake(head1.x, head1.y))
        if len(snake_body2) != 0:
            snake_body2.insert(0, Snake(head2.x, head2.y))
        # 判斷貪吃蛇原運(yùn)動(dòng)方向(run)與玩家鍵盤輸入的運(yùn)動(dòng)方向(run_direction)是否違反正常運(yùn)動(dòng)情況
        if run1 == "up" and not run_direction1 == "down":
            run1 = run_direction1
        if run1 == "down" and not run_direction1 == "up":
            run1 = run_direction1
        if run1 == "left" and not run_direction1 == "right":
            run1 = run_direction1
        if run1 == "right" and not run_direction1 == "left":
            run1 = run_direction1
        if run2 == "up" and not run_direction2 == "down":
            run2 = run_direction2
        if run2 == "down" and not run_direction2 == "up":
            run2 = run_direction2
        if run2 == "left" and not run_direction2 == "right":
            run2 = run_direction2
        if run2 == "right" and not run_direction2 == "left":
            run2 = run_direction2
        # 根據(jù)玩家鍵入方向進(jìn)行蛇頭坐標(biāo)的更新
        if run1 == "up":
            head1.y -= 20
        if run1 == "down":
            head1.y += 20
        if run1 == "left":
            head1.x -= 20
        if run1 == "right":
            head1.x += 20
        if run2 == "up":
            head2.y -= 20
        if run2 == "down":
            head2.y += 20
        if run2 == "left":
            head2.x -= 20
        if run2 == "right":
            head2.x += 20
        # 實(shí)現(xiàn)穿墻
        # 蛇頭穿出窗體共有8種情況
        if head1.x  0:
            head1.x = 960
        if head1.x > 960:
            head1.x = 0
        if head1.y  0:
            head1.y = 600
        if head1.y > 600:
            head1.y = 0
        if head2.x  0:
            head2.x = 960
        if head2.x > 960:
            head2.x = 0
        if head2.y  0:
            head2.y = 600
        if head2.y > 600:
            head2.y = 0
        # 定義死亡標(biāo)志位
        die_flag1 = die_flag2 = False
        for body in snake_body1[1:]+snake_body2[1:]:
            if head1.x == body.x and head1.y == body.y:
                die_flag1 = True
            if head2.x == body.x and head2.y == body.y:
                die_flag2 = True
        if die_flag1 == True:
            snake_body1.clear()
        if die_flag2 == True:
            snake_body2.clear()
        # 若兩條蛇都死亡
        if len(snake_body1) == 0 and len(snake_body2) == 0:
            show_end()
        # 判斷蛇頭和食物坐標(biāo),若相等,則加分,并生成新的食物
        # 定義標(biāo)志,表明是否找到和蛇頭相等的食物
        global flag1
        global flag2
        flag1 = flag2 = 0
        # 如果蛇頭和食物重合
        for item in food_list:
            # 在蛇1沒死且蛇頭1和某一食物坐標(biāo)相等的條件下
            if len(snake_body1) != 0 and (head1.x == item.x and head1.y == item.y or head1.x == food.x and head1.y == food.y):
                flag1 = 1
                score1 += 1
                # 彈出被吃掉的這個(gè)食物
                food_list.pop(food_list.index(item))
                # 再產(chǎn)生一個(gè)食物
                food = new_food(head1)
                # 把新食物插入food_list,下一次循環(huán)中會(huì)更新繪制食物全體
                food_list.append(food)
                break
            # 在蛇2沒死的且蛇頭2和某一食物坐標(biāo)相等的條件下
            elif len(snake_body2) != 0 and head2.x == item.x and head2.y == item.y or head2.x == food.x and head2.y == food.y:
                flag2 = 1
                score2 += 1
                # 彈出被吃掉的這個(gè)食物
                food_list.pop(food_list.index(item))
                # 再產(chǎn)生一個(gè)食物
                food = new_food(head2)
                # 把新食物插入food_list,下一次循環(huán)中會(huì)更新繪制食物全體
                food_list.append(food)
                break
        # 蛇1必須沒死,否則pop會(huì)引發(fā)異常
        if len(snake_body1) != 0 and flag1 == 0:
            snake_body1.pop()
        # 蛇2必須沒死,否則pop會(huì)引發(fā)異常
        if len(snake_body2) != 0 and flag2 == 0:
            snake_body2.pop ()
        font = pygame.font.SysFont("simHei", 25)
        mode_title1 = mode_title2 = font.render('穿墻模式', False, grey)
        socre_title1 = font.render('得分: %s' % score1, False, grey)
        socre_title2 = font.render('得分: %s' % score2, False, grey)
        window.blit(mode_title1, (50, 30))
        window.blit(socre_title1, (50, 65))
        window.blit(mode_title2, (800, 30))
        window.blit(socre_title2, (800, 65))
        # 更新蛇頭蛇身和食物的數(shù)據(jù)
        pygame.display.update()
        # 通過幀率設(shè)置貪吃蛇速度
        clock.tick(8)

7.button()函數(shù),功能:實(shí)現(xiàn)按鈕樣式設(shè)計(jì)和響應(yīng)鼠標(biāo)操作:

def button(msg, x, y, w, h, ic, ac, action=None):
    # 獲取鼠標(biāo)位置
    mouse = pygame.mouse.get_pos()
    # 獲取鼠標(biāo)點(diǎn)擊情況
    click = pygame.mouse.get_pressed()
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(window, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(window, ic, (x, y, w, h))
    # 設(shè)置按鈕中的文字樣式和居中對(duì)齊
    font = pygame.font.SysFont('simHei', 20)
    smallfont = font.render(msg, True, white)
    smallrect = smallfont.get_rect()
    smallrect.center = ((x + (w / 2)), (y + (h / 2)))
    window.blit(smallfont, smallrect)

8.into_game()函數(shù),功能:實(shí)現(xiàn)游戲初始界面,選擇模式:

def into_game():
    while True:
        window.blit(init_background, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_end()
        # 設(shè)置字體
        font = pygame.font.SysFont("simHei", 50)
        # 初始界面顯示文字
        fontsurf = font.render('歡迎來到貪吃蛇大冒險(xiǎn)!', True, black)  # 文字
        fontrect = fontsurf.get_rect()
        fontrect.center = ((480), 200)
        window.blit(fontsurf, fontrect)
        button("正常模式", 370, 370, 200, 40, blue, brightred, start_game_double)
        button("可穿墻模式", 370, 420, 200, 40, violte, brightred, start_kgame_double)
        button("退出游戲", 370, 470, 200, 40, red, brightred, exit_end)
        pygame.display.update()
        clock.tick(20)

9.主函數(shù),功能:初始化參數(shù)設(shè)定,進(jìn)入游戲:

if __name__ == '__main__':
    # 定義需要用到的顏色
    white = (255, 255, 255)
    red = (200, 0, 0)
    green = (0, 128, 0)
    blue = (0, 202, 254)
    violte = (194, 8, 234)
    brightred = (255, 0, 0)
    brightgreen = (0, 255, 0)
    black = (0, 0, 0)
    grey = (129, 131, 129)
    # 設(shè)計(jì)窗口
    window = pygame.display.set_mode((960, 600))
    # 定義標(biāo)題
    pygame.display.set_caption("貪吃蛇大冒險(xiǎn)")
    # 定義背景圖片
    init_background = pygame.image.load("image/init_bgimg.jpg")
    background = pygame.image.load("image/bgimg.jpg")
    # 背景音樂
    pygame.mixer.init()
    pygame.mixer.music.load("background.mp3")
    # 創(chuàng)建時(shí)鐘
    clock = pygame.time.Clock()
    # 初始化,自檢所有模塊是否完整
    pygame.init()
    # 初始界面
    into_game()

注:其中的圖片、背景音樂需要自己找合適(尺寸要與窗口大小相適應(yīng))的,也可以參考我上傳的資源貪吃蛇雙人版源碼+圖片+音樂。

附:貪吃蛇 單人+雙人整合版源碼

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

您可能感興趣的文章:
  • Python實(shí)現(xiàn)貪吃蛇小游戲(單人模式)
  • python貪吃蛇游戲代碼
  • Python寫的貪吃蛇游戲例子
  • 使用Python寫一個(gè)貪吃蛇游戲?qū)嵗a
  • Python 實(shí)現(xiàn) 貪吃蛇大作戰(zhàn) 代碼分享
  • Python貪吃蛇游戲編寫代碼
  • 利用python實(shí)現(xiàn)簡易版的貪吃蛇游戲(面向python小白)
  • 教你一步步利用python實(shí)現(xiàn)貪吃蛇游戲
  • python實(shí)現(xiàn)貪吃蛇游戲
  • 保姆級(jí)python教程寫個(gè)貪吃蛇大冒險(xiǎn)

標(biāo)簽:銀川 三亞 呼倫貝爾 呼倫貝爾 葫蘆島 湘西 烏魯木齊 安慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)》,本文關(guān)鍵詞  Python,實(shí)現(xiàn),貪吃,蛇,小游戲,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python實(shí)現(xiàn)貪吃蛇小游戲(雙人模式)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章