Python Pygame實(shí)戰(zhàn)之超級(jí)炸彈人游戲的實(shí)現(xiàn)
前言
如今的玩家們?cè)跓o(wú)聊的時(shí)候會(huì)玩些什么游戲呢?
王者還是吃雞是最多的選擇。但在80、90年代的時(shí)候多是一些很簡(jiǎn)單的游戲:《超級(jí)瑪麗》、《蜘蛛紙牌》、《魂斗羅》......
不知道還有人記得《炸彈人》這款懷舊的產(chǎn)品嘛?
啊這,不對(duì)不對(duì),放錯(cuò)圖片了下面這才是童年的正確打開(kāi)方式

今天小編就帶大家懷舊一波,來(lái)給大家用代碼仿制一下童年大家玩兒過(guò)的《炸彈人》小游戲!
一、環(huán)境安裝
1.素材(圖片)


2.環(huán)境安裝
本文是由Pygame寫(xiě)的小游戲。
涉及運(yùn)行環(huán)境:Python3.7、Pycharm社區(qū)版、Pygame模塊。
pip install +模塊名 或pip install -i https://pypi.douban.com/simple/ +模塊名
二、代碼演示
1.配置文件
'''配置文件'''
import os
'''屏幕大小'''
SCREENSIZE = (640, 480)
'''塊大小'''
BLOCKSIZE = 30
'''FPS'''
FPS = 30
'''游戲地圖路徑'''
GAMEMAPPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/maps/1.map', 'resources/maps/2.map']]
'''墻路徑'''
WALLPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/misc/wall0.png', 'resources/images/misc/wall1.png', 'resources/images/misc/wall2.png']]
'''英雄路徑'''
HERODKPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/dk/left.png', 'resources/images/dk/right.png', 'resources/images/dk/up.png', 'resources/images/dk/down.png']]
HEROZELDAPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/zelda/left.png', 'resources/images/zelda/right.png', 'resources/images/zelda/up.png', 'resources/images/zelda/down.png']]
HEROBATMANPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/batman/left.png', 'resources/images/batman/right.png', 'resources/images/batman/up.png', 'resources/images/batman/down.png']]
'''水果路徑'''
FRUITPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/misc/banana.png', 'resources/images/misc/cherry.png']]
'''背景路徑'''
BACKGROUNDPATHS = [os.path.join(os.getcwd(), path) for path in \
['resources/images/misc/bg0.png', 'resources/images/misc/bg1.png', 'resources/images/misc/bg2.png']]
'''爆炸和發(fā)射路徑'''
BOMBPATH = os.path.join(os.getcwd(), 'resources/images/misc/bomb.png')
FIREPATH = os.path.join(os.getcwd(), 'resources/images/misc/fire.png')
'''背景音樂(lè)'''
BGMPATH = os.path.join(os.getcwd(), 'resources/audio/bgm.mp3')
'''一些顏色'''
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)2.主程序
import sys
import cfg
import random
import pygame
from modules import *
'''游戲主程序'''
def main(cfg):
# 初始化
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(cfg.BGMPATH)
pygame.mixer.music.play(-1, 0.0)
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('炸彈人小游戲')
# 開(kāi)始界面
Interface(screen, cfg, mode='game_start')
# 游戲主循環(huán)
font = pygame.font.SysFont('Consolas', 15)
for gamemap_path in cfg.GAMEMAPPATHS:
# -地圖
map_parser = mapParser(gamemap_path, bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE)
# -水果
fruit_sprite_group = pygame.sprite.Group()
used_spaces = []
for i in range(5):
coordinate = map_parser.randomGetSpace(used_spaces)
used_spaces.append(coordinate)
fruit_sprite_group.add(Fruit(random.choice(cfg.FRUITPATHS), coordinate=coordinate, blocksize=cfg.BLOCKSIZE))
# -我方Hero
coordinate = map_parser.randomGetSpace(used_spaces)
used_spaces.append(coordinate)
ourhero = Hero(imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='ZELDA')
# -電腦Hero
aihero_sprite_group = pygame.sprite.Group()
coordinate = map_parser.randomGetSpace(used_spaces)
aihero_sprite_group.add(Hero(imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='BATMAN'))
used_spaces.append(coordinate)
coordinate = map_parser.randomGetSpace(used_spaces)
aihero_sprite_group.add(Hero(imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='DK'))
used_spaces.append(coordinate)
# -炸彈bomb
bomb_sprite_group = pygame.sprite.Group()
# -用于判斷游戲勝利或者失敗的flag
is_win_flag = False
# -主循環(huán)
screen = pygame.display.set_mode(map_parser.screen_size)
clock = pygame.time.Clock()
while True:
dt = clock.tick(cfg.FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(-1)
# --↑↓←→鍵控制上下左右, 空格鍵丟炸彈
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ourhero.move('up')
elif event.key == pygame.K_DOWN:
ourhero.move('down')
elif event.key == pygame.K_LEFT:
ourhero.move('left')
elif event.key == pygame.K_RIGHT:
ourhero.move('right')
elif event.key == pygame.K_SPACE:
if ourhero.bomb_cooling_count <= 0:
bomb_sprite_group.add(ourhero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
screen.fill(cfg.WHITE)
# --電腦Hero隨機(jī)行動(dòng)
for hero in aihero_sprite_group:
action, flag = hero.randomAction(dt)
if flag and action == 'dropbomb':
bomb_sprite_group.add(hero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
# --吃到水果加生命值(只要是Hero, 都能加)
ourhero.eatFruit(fruit_sprite_group)
for hero in aihero_sprite_group:
hero.eatFruit(fruit_sprite_group)
# --游戲元素都綁定到屏幕上
map_parser.draw(screen)
for bomb in bomb_sprite_group:
if not bomb.is_being:
bomb_sprite_group.remove(bomb)
explode_area = bomb.draw(screen, dt, map_parser)
if explode_area:
# --爆炸火焰范圍內(nèi)的Hero生命值將持續(xù)下降
if ourhero.coordinate in explode_area:
ourhero.health_value -= bomb.harm_value
for hero in aihero_sprite_group:
if hero.coordinate in explode_area:
hero.health_value -= bomb.harm_value
fruit_sprite_group.draw(screen)
for hero in aihero_sprite_group:
hero.draw(screen, dt)
ourhero.draw(screen, dt)
# --左上角顯示生命值
pos_x = showText(screen, font, text=ourhero.hero_name+'(our):'+str(ourhero.health_value), color=cfg.YELLOW, position=[5, 5])
for hero in aihero_sprite_group:
pos_x, pos_y = pos_x+15, 5
pos_x = showText(screen, font, text=hero.hero_name+'(ai):'+str(hero.health_value), color=cfg.YELLOW, position=[pos_x, pos_y])
# --我方玩家生命值小于等于0/電腦方玩家生命值均小于等于0則判斷游戲結(jié)束
if ourhero.health_value <= 0:
is_win_flag = False
break
for hero in aihero_sprite_group:
if hero.health_value <= 0:
aihero_sprite_group.remove(hero)
if len(aihero_sprite_group) == 0:
is_win_flag = True
break
pygame.display.update()
clock.tick(cfg.FPS)
if is_win_flag:
Interface(screen, cfg, mode='game_switch')
else:
break
Interface(screen, cfg, mode='game_end')
'''run'''
if __name__ == '__main__':
while True:
main(cfg)三、效果展示
1.游戲界面
都是有音樂(lè)背景的啦!

2.游戲開(kāi)始

3.游戲結(jié)束

以上就是Python Pygame實(shí)戰(zhàn)之超級(jí)炸彈人游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame炸彈人游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)戰(zhàn)之實(shí)現(xiàn)康威生命游戲
這篇文章主要介紹了Python實(shí)戰(zhàn)之實(shí)現(xiàn)康威生命游戲,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
python實(shí)現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多線程的方式及多條命令并發(fā)執(zhí)行,感興趣的小伙伴們可以參考一下2016-06-06
利用Python進(jìn)行全面的GPU環(huán)境檢測(cè)與分析
這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫(xiě)一個(gè)強(qiáng)大的 GPU 診斷工具,它能夠全面收集和分析系統(tǒng)中的 GPU 相關(guān)信息,感興趣的可以了解下2025-01-01
python 解決數(shù)據(jù)庫(kù)寫(xiě)入時(shí)float自動(dòng)變?yōu)檎麛?shù)的問(wèn)題
這篇文章主要介紹了python 解決數(shù)據(jù)庫(kù)寫(xiě)入時(shí)float自動(dòng)變?yōu)檎麛?shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Django中和時(shí)區(qū)相關(guān)的安全問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于Django中和時(shí)區(qū)相關(guān)的安全問(wèn)題的相關(guān)資料,需要的朋友可以參考下2020-10-10
Python中11種NumPy高級(jí)操作總結(jié)
熬夜整了了11種Numpy的高級(jí)操作,每一種都有參數(shù)解釋與小例子輔助說(shuō)明。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-05-05
Python Requests訪問(wèn)網(wǎng)絡(luò)更方便
這篇文章主要介紹了使用Python Requests訪問(wèn)網(wǎng)絡(luò),Python Requests 是一個(gè)非常強(qiáng)大的 HTTP 客戶端庫(kù),用于發(fā)送 HTTP 請(qǐng)求,獲取響應(yīng)等操作,通過(guò)這個(gè)庫(kù),你可以輕松地與 Web 服務(wù)進(jìn)行交互,實(shí)現(xiàn)各種網(wǎng)絡(luò)請(qǐng)求2024-01-01

