亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

教你用Python寫一個水果忍者小游戲

 更新時間:2022年03月24日 09:29:09   作者:馬哥Linux運維  
水果忍者游戲,又稱切水果游戲,玩法簡單,水果忍者游戲在兒童中很受歡迎,下面這篇文章主要給大家介紹了關于如何利用Python寫一個水果忍者小游戲的相關資料,需要的朋友可以參考下

引言

水果忍者的玩法很簡單,盡可能的切開拋出的水果就行。

今天小五就用python簡單的模擬一下這個游戲。在這個簡單的項目中,我們用鼠標選擇水果來切割,同時炸彈也會隱藏在水果中,如果切開了三次炸彈,玩家就會失敗。

一、需要導入的包

import pygame, sys
    import os
    import random

二、窗口界面設置  

# 游戲窗口
    WIDTH = 800
    HEIGHT = 500
    FPS = 15             # gameDisplay的幀率,1/12秒刷新一次
    pygame.init()
    pygame.display.set_caption('水果忍者') # 標題
    gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 固定窗口大小
    clock = pygame.time.Clock()
    # 用到的顏色
    WHITE = (255,255,255)
    BLACK = (0,0,0)
    RED = (255,0,0)
    GREEN = (0,255,0)
    BLUE = (0,0,255)
    background = pygame.image.load('背景.jpg') # 背景
    font = pygame.font.Font(os.path.join(os.getcwd(), 'comic.ttf'), 42) # 字體
    score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分字體樣式

三、隨機生成水果位置    

def generate_random_fruits(fruit):
        fruit_path = "images/" + fruit + ".png"
        data[fruit] = {
            'img': pygame.image.load(fruit_path),
            'x' : random.randint(100,500),
            'y' : 800,
            'speed_x': random.randint(-10,10),
            'speed_y': random.randint(-80, -60),
            'throw': False,
            't': 0,
            'hit': False,
        }
        if random.random() >= 0.75:
            data[fruit]['throw'] = True
        else:
            data[fruit]['throw'] = False
    data = {}
    for fruit in fruits:
        generate_random_fruits(fruit)
  • 這個函數(shù)用于隨機生成水果和保存水果的數(shù)據(jù)。
  • 'x'和'y'存儲水果在x坐標和y坐標上的位置。
  • Speed_x和speed_y是存儲水果在x和y方向的移動速度。它也控制水果的對角線移動。
  • throw,用于判斷生成的水果坐標是否在游戲之外。如果在外面,那么將被丟棄。
  • data字典用于存放隨機生成的水果的數(shù)據(jù)。

四、繪制字體  

font_name = pygame.font.match_font('comic.ttf')
    def draw_text(display, text, size, x, y):
        font = pygame.font.Font(font_name, size)
        text_surface = font.render(text, True, WHITE)
        text_rect = text_surface.get_rect()
        text_rect.midtop = (x, y)
        gameDisplay.blit(text_surface, text_rect)
  • Draw_text函數(shù)可以在屏幕上繪制文字。
  • get_rect() 返回 Rect 對象。
  • X和y是X方向和Y方向的位置。
  • blit()在屏幕上的指定位置繪制圖像或?qū)懭胛淖帧?/li>

五、玩家生命的提示

# 繪制玩家的生命
    def draw_lives(display, x, y, lives, image) :
    for i in range(lives) :
    img = pygame.image.load(image)
    img_rect = img.get_rect()
    img_rect.x = int(x + 35 * i)
    img_rect.y = y
    display.blit(img, img_rect)
    def hide_cross_lives(x, y):
    gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))
  • img_rect獲取十字圖標的(x,y)坐標(位于最右上方)。
  • img_rect .x 設置下一個十字圖標距離前一個圖標35像素。
  • img_rect.y負責確定十字圖標從屏幕頂部開始的位置。

六、游戲開始與結(jié)束的畫面    

def show_gameover_screen():
        gameDisplay.blit(background, (0,0))
        draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
        if not game_over :
            draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)
        draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
        pygame.display.flip()
        waiting = True
        while waiting:
            clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                if event.type == pygame.KEYUP:
                    waiting = False
  • show_gameover_screen()函數(shù)顯示初始游戲畫面和游戲結(jié)束畫面。
  • pygame.display.flip()將只更新屏幕的一部分,但如果沒有參數(shù)傳遞,則會更新整個屏幕。
  • pygame.event.get()將返回存儲在pygame事件隊列中的所有事件。
  • 如果事件類型等于quit,那么pygame將退出。
  • event.KEYUP事件,當按鍵被按下和釋放時發(fā)生的事件。

七、游戲主循環(huán)  

first_round = True
    game_over = True        
    game_running = True    
    while game_running :
        if game_over :
            if first_round :
                show_gameover_screen()
                first_round = False
            game_over = False
            player_lives = 3
            draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
            score = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_running = False
        gameDisplay.blit(background, (0, 0))
        gameDisplay.blit(score_text, (0, 0))
        draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
        for key, value in data.items():
            if value['throw']:
                value['x'] += value['speed_x']
                value['y'] += value['speed_y']
                value['speed_y'] += (1 * value['t'])
                value['t'] += 1
                if value['y'] <= 800:
                    gameDisplay.blit(value['img'], (value['x'], value['y']))
                else:
                    generate_random_fruits(key)
                current_position = pygame.mouse.get_pos()
                if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
                        and current_position[1] > value['y'] and current_position[1] < value['y']+60:
                    if key == 'bomb':
                        player_lives -= 1
                        if player_lives == 0:
                            hide_cross_lives(690, 15)
                        elif player_lives == 1 :
                            hide_cross_lives(725, 15)
                        elif player_lives == 2 :
                            hide_cross_lives(760, 15)
                        if player_lives < 0 :
                            show_gameover_screen()
                            game_over = True
                        half_fruit_path = "images/explosion.png"
                    else:
                        half_fruit_path = "images/" + "half_" + key + ".png"
                    value['img'] = pygame.image.load(half_fruit_path)
                    value['speed_x'] += 10
                    if key != 'bomb' :
                        score += 1
                    score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
                    value['hit'] = True
            else:
                generate_random_fruits(key)
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
  • 這是游戲的主循環(huán)
  • 如果超過3個炸彈被切掉,game_over終止游戲,同時循環(huán)。
  • game_running 用于管理游戲循環(huán)。
  • 如果事件類型是退出,那么游戲窗口將被關閉。
  • 在這個游戲循環(huán)中,我們動態(tài)顯示屏幕內(nèi)的水果。
  • 如果一個水果沒有被切開,那么它將不會發(fā)生任何事情。如果水果被切開,那么一個半切開的水果圖像應該出現(xiàn)在該水果的地方。
  • 如果用戶點擊了三次炸彈,將顯示GAME OVER信息,并重置窗口。
  • clock.tick()將保持循環(huán)以正確的速度運行。循環(huán)應該在每1/12秒后更新一次。

總結(jié)

到此這篇關于教你用Python寫一個水果忍者小游戲的文章就介紹到這了,更多相關Python水果忍者小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python3實現(xiàn)的判斷回文鏈表算法示例

    Python3實現(xiàn)的判斷回文鏈表算法示例

    這篇文章主要介紹了Python3實現(xiàn)的判斷回文鏈表算法,結(jié)合實例形式分析了Python3針對鏈表是否為回文鏈表進行判斷的相關算法實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • python獲取交互式ssh shell的方法

    python獲取交互式ssh shell的方法

    今天小編就為大家分享一篇python獲取交互式ssh shell的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python pandas 的索引方式 data.loc[],data[][]示例詳解

    Python pandas 的索引方式 data.loc[],data[][]示例詳解

    這篇文章主要介紹了Python pandas 的索引方式 data.loc[], data[][]的相關資料,其中data.loc[index,column]使用.loc[ ]第一個參數(shù)是行索引,第二個參數(shù)是列索引,本文結(jié)合實例代碼講解的非常詳細,需要的朋友可以參考下
    2023-02-02
  • 解決selenium+Headless Chrome實現(xiàn)不彈出瀏覽器自動化登錄的問題

    解決selenium+Headless Chrome實現(xiàn)不彈出瀏覽器自動化登錄的問題

    這篇文章主要介紹了解決selenium+Headless Chrome實現(xiàn)不彈出瀏覽器自動化登錄的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python字典和列表性能之間的比較

    Python字典和列表性能之間的比較

    今天給大家介紹的是Python列表和字典的相關知識,文中對Python字典和列表的性能作了充分的比較,好奇的小伙伴們一起來看看吧,需要的朋友可以參考下
    2021-06-06
  • 基于Python把網(wǎng)站域名解析成ip地址

    基于Python把網(wǎng)站域名解析成ip地址

    這篇文章主要介紹了基于Python把網(wǎng)站域名解析成ip地址,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 如何使用python-opencv批量生成帶噪點噪線的數(shù)字驗證碼

    如何使用python-opencv批量生成帶噪點噪線的數(shù)字驗證碼

    這篇文章主要介紹了如何使用python-opencv批量生成帶噪點噪線的數(shù)字驗證碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Python matplotlib 繪制雙Y軸曲線圖的示例代碼

    Python matplotlib 繪制雙Y軸曲線圖的示例代碼

    Matplotlib是非常強大的python畫圖工具,這篇文章主要介紹了Python matplotlib 繪制雙Y軸曲線圖,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • python信號量,條件變量和事件詳解

    python信號量,條件變量和事件詳解

    這篇文章主要為大家介紹了python的信號量,條件變量和事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • python實現(xiàn)文件分組復制到不同目錄的例子

    python實現(xiàn)文件分組復制到不同目錄的例子

    這篇文章主要介紹了python實現(xiàn)文件按組復制到不同目錄的例子,需要的朋友可以參考下
    2014-06-06

最新評論