Python Pygame實(shí)戰(zhàn)之水果忍者游戲的實(shí)現(xiàn)
導(dǎo)語(yǔ)
Hey!下午好,我是木木子,關(guān)注我,一起玩游戲吧~
?
?微信小游戲很久之前刮起了一股切水果熱潮,還記得嘛?我記得純粹是因?yàn)檫@個(gè)游戲家里的孩子依舊沒(méi)放棄~
比如:果盤(pán)忍者|水果切切切|一起切水果|全民切西瓜|王牌飛刀手......
那時(shí)候——各種同類型的切水果小游戲?qū)映霾桓F,并“前仆后繼”地紛紛霸占小程序排行榜前列。那場(chǎng)面簡(jiǎn)直是經(jīng)典手游《水果忍者》的強(qiáng)勢(shì)回歸!
今天木木子手把手教大家寫(xiě)一款簡(jiǎn)單又好玩兒的切水果小游戲,來(lái)比拼一下吧——battlebattle!
還沒(méi)有“切過(guò)水果”的朋友們你們就OUT了!
本文是寫(xiě)的水果忍者小游戲啦~還是用的大家所熟悉的Pygame模塊啦~本文超詳細(xì)講解哦!
一、準(zhǔn)備中
1.0 游戲規(guī)則
Python版本的水果忍者小編初始化設(shè)置是玩家3條生命值,切到相應(yīng)的水果相應(yīng)加分,切到易爆物
比如炸彈這些就會(huì)相應(yīng)的減少生命值,在生命值內(nèi)可以一直切切切,切的越多分?jǐn)?shù)越高,相應(yīng)的生命值耗盡即結(jié)束游戲哦!快試試你能得幾分?
哈哈哈,今天也錄制了游戲視頻的,看著視頻更有玩游戲的感覺(jué)嘛~
1.1 游戲圖片素材(可修改)
1.2 游戲字體素材(可修改)
二、環(huán)境安裝
本文木子是用的Python3、Pycharm寫(xiě)的。模塊Pygame、random隨機(jī)出現(xiàn)水果以及一些自帶的。
這里模塊安裝命令統(tǒng)一鏡像源豆瓣:
pip install -i https://pypi.douban.com/simple/ +模塊名
三、開(kāi)始敲代碼
3.0 設(shè)置界面玩家生命值等
player_lives = 3 # 生命 score = 0 # 得分 fruits = ['melon', 'orange', 'pomegranate', 'guava', 'bomb'] # 水果和炸彈
3.1 導(dǎo)入模塊
import pygame, sys import os import random
3.2 界面背景、字體設(shè)置
background = pygame.image.load('背景圖/02.png') # 背景 font = pygame.font.Font(os.path.join(os.getcwd(), '字體/comic.ttf'), 42) # 字體 score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分的字體樣式
3.3 游戲窗口設(shè)置
WIDTH = 800 HEIGHT = 500 FPS = 12 # gameDisplay的幀率,1/12秒刷新一次 pygame.init() pygame.display.set_caption('水果忍者_(dá)csdn賬號(hào):顧木子吖') # 標(biāo)題 gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 游戲窗口 clock = pygame.time.Clock()
3.4 隨機(jī)生成水果的位置與數(shù)據(jù)存放
def generate_random_fruits(fruit): fruit_path = "images/" + fruit + ".png" data[fruit] = { 'img': pygame.image.load(fruit_path), 'x' : random.randint(100,500), # 水果在x坐標(biāo)軸上的位置 'y' : 800, 'speed_x': random.randint(-10,10), # 水果在x方向時(shí)的速度和對(duì)角線移動(dòng) 'speed_y': random.randint(-80, -60), # y方向時(shí)的速度 'throw': False, # 如果生成水果的位置在gameDisplay之外,將被丟棄 't': 0, 'hit': False, } if random.random() >= 0.75: # 返回在[0.0, 1.0]范圍內(nèi)的下一個(gè)隨機(jī)浮點(diǎn)數(shù),以保持水果在游戲中的顯示。 data[fruit]['throw'] = True else: data[fruit]['throw'] = False
3.5 用一個(gè)字典來(lái)存放水果的數(shù)據(jù)
data = {} for fruit in fruits: generate_random_fruits(fruit) def hide_cross_lives(x, y): gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))
3.6 在屏幕中繪制字體
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)
3.7 繪制玩家的生命
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)
3.8 游戲開(kāi)始與結(jié)束畫(huà)面
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 any key to start the game", 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
3.9 游戲主循環(huán)
first_round = True game_over = True # 超過(guò)3個(gè)炸彈,終止游戲循環(huán) game_running = True # 管理游戲循環(huán) 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(): # 檢查是否關(guān)閉窗口 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'] # x方向上移動(dòng)水果 value['y'] += value['speed_y'] # y方向上移動(dòng) value['speed_y'] += (1 * value['t']) # 遞增 value['t'] += 1 if value['y'] <= 800: gameDisplay.blit(value['img'], (value['x'], value['y'])) # 動(dòng)態(tài)顯示水果 else: generate_random_fruits(key) current_position = pygame.mouse.get_pos() # 獲取鼠標(biāo)的位置,單位為像素 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) # 超過(guò)3次炸彈,提示游戲結(jié)束,重置窗口 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()
四、游戲展示效果
4.1 Part 1 動(dòng)態(tài)視頻展示效果如下
Python版水果忍者,有趣有趣~
4.2 Part 2 靜態(tài)截圖展示效果如下
(1)游戲進(jìn)入界面——
(2)?修改下背景圖進(jìn)入的界面——?這個(gè)感覺(jué)貌似好看點(diǎn)兒~
4.3 Part 3 靜態(tài)進(jìn)入游戲界面截圖如下
以上就是Python Pygame實(shí)戰(zhàn)之水果忍者游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame水果忍者的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python爬蟲(chóng)之場(chǎng)內(nèi)ETF基金獲取
這篇文章主要介紹了python爬蟲(chóng)之場(chǎng)內(nèi)ETF基金獲取,ETF?是一種場(chǎng)內(nèi)交易型基金,可以在盤(pán)中進(jìn)行交易,交易性比場(chǎng)外基金強(qiáng)一點(diǎn),下文基于python的相關(guān)資料展開(kāi),需要的小伙伴可以參考一下2022-05-05python實(shí)現(xiàn)連接mongodb的方法
這篇文章主要介紹了python實(shí)現(xiàn)連接mongodb的方法,涉及Python使用pymongo模塊的基本技巧,需要的朋友可以參考下2015-05-05超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克
這篇文章主要介紹了OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python Opencv實(shí)戰(zhàn)之文字檢測(cè)OCR
這篇文章主要為大家詳細(xì)介紹了如何利用Python Opencv實(shí)現(xiàn)文字檢測(cè)OCR功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-08-08詳解Python手寫(xiě)數(shù)字識(shí)別模型的構(gòu)建與使用
這篇文章主要為大家詳細(xì)介紹了Python中手寫(xiě)數(shù)字識(shí)別模型的構(gòu)建與使用,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2022-12-12對(duì)Python中創(chuàng)建進(jìn)程的兩種方式以及進(jìn)程池詳解
今天小編就為大家分享一篇對(duì)Python中創(chuàng)建進(jìn)程的兩種方式以及進(jìn)程池詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01