全网最强Python版《超级玛丽》小游戏,我还是你的马里奥嘛?
生活随笔
收集整理的這篇文章主要介紹了
全网最强Python版《超级玛丽》小游戏,我还是你的马里奥嘛?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前文
大家好!我是梨子同學!
希望大家多多支持我!哈哈
為了感謝每一個關注我的小可愛:💓每篇文章的項目源碼都是無償分享滴💓見文末!
很多csdn的功能還在研究中,還有小編的文筆不好勿怪,會慢慢進步跟大家一起學習的
小編也一直在學習編程,如果代碼小程序出現(xiàn)錯誤歡迎大家評論區(qū)留言哈!
最后——如果文章有幫助到你,記得“關注”、“點贊”、“評論”三連哦~
?
導語🎁
哈嘍!哈嘍!我是梨子,今天給大家上線一款超級好玩有趣的童年游戲!
當當當——超級瑪麗!
?正文🎁
嗯吶~寫游戲Python還是用的Pygame模塊啦
1)準備中🛒
1.1環(huán)境安裝🎑
Python3、Pycharm、Pygame模塊很多自帶的模塊等。
模塊安裝統(tǒng)一用的豆瓣鏡像源:?
pip install -i https://pypi.douban.com/simple/ +模塊名。1.2圖片素材+背景音樂+字體(可修改)🎑
2)開始敲代碼🛒
2.1 運行程序:mario_level_1.py。🎑
#!/usr/bin/env python __author__ = '超級瑪麗-源碼基地'""" This is an attempt to recreate the first level of Super Mario Bros for the NES. """import sys import pygame as pg from data.main import main import cProfileif __name__=='__main__':main()pg.quit()sys.exit()2.2 配置音樂文字等setup.py。🎑
__author__ = 'Python源碼基地'""" This module initializes the display and creates dictionaries of resources. """import os import pygame as pg from . import tools from .import constants as cORIGINAL_CAPTION = c.ORIGINAL_CAPTIONos.environ['SDL_VIDEO_CENTERED'] = '1' pg.init() pg.event.set_allowed([pg.KEYDOWN, pg.KEYUP, pg.QUIT]) pg.display.set_caption(c.ORIGINAL_CAPTION) SCREEN = pg.display.set_mode(c.SCREEN_SIZE) SCREEN_RECT = SCREEN.get_rect()FONTS = tools.load_all_fonts(os.path.join("resources","fonts")) MUSIC = tools.load_all_music(os.path.join("resources","music")) GFX = tools.load_all_gfx(os.path.join("resources","graphics")) SFX = tools.load_all_sfx(os.path.join("resources","sound"))2.3游戲音樂設置game_sound.py。🎑
__author__ = 'Python顧木子吖'import pygame as pg from . import setup from . import constants as cclass Sound(object):"""Handles all sound for the game"""def __init__(self, overhead_info):"""Initialize the class"""self.sfx_dict = setup.SFXself.music_dict = setup.MUSICself.overhead_info = overhead_infoself.game_info = overhead_info.game_infoself.set_music_mixer()def set_music_mixer(self):"""Sets music for level"""if self.overhead_info.state == c.LEVEL:pg.mixer.music.load(self.music_dict['main_theme'])pg.mixer.music.play()self.state = c.NORMALelif self.overhead_info.state == c.GAME_OVER:pg.mixer.music.load(self.music_dict['game_over'])pg.mixer.music.play()self.state = c.GAME_OVERdef update(self, game_info, mario):"""Updates sound object with game info"""self.game_info = game_infoself.mario = marioself.handle_state()def handle_state(self):"""Handles the state of the soundn object"""if self.state == c.NORMAL:if self.mario.dead:self.play_music('death', c.MARIO_DEAD)elif self.mario.invincible \and self.mario.losing_invincibility == False:self.play_music('invincible', c.MARIO_INVINCIBLE)elif self.mario.state == c.FLAGPOLE:self.play_music('flagpole', c.FLAGPOLE)elif self.overhead_info.time == 100:self.play_music('out_of_time', c.TIME_WARNING)elif self.state == c.FLAGPOLE:if self.mario.state == c.WALKING_TO_CASTLE:self.play_music('stage_clear', c.STAGE_CLEAR)elif self.state == c.STAGE_CLEAR:if self.mario.in_castle:self.sfx_dict['count_down'].play()self.state = c.FAST_COUNT_DOWNelif self.state == c.FAST_COUNT_DOWN:if self.overhead_info.time == 0:self.sfx_dict['count_down'].stop()self.state = c.WORLD_CLEARelif self.state == c. TIME_WARNING:if pg.mixer.music.get_busy() == 0:self.play_music('main_theme_sped_up', c.SPED_UP_NORMAL)elif self.mario.dead:self.play_music('death', c.MARIO_DEAD)elif self.state == c.SPED_UP_NORMAL:if self.mario.dead:self.play_music('death', c.MARIO_DEAD)elif self.mario.state == c.FLAGPOLE:self.play_music('flagpole', c.FLAGPOLE)elif self.state == c.MARIO_INVINCIBLE:if (self.mario.current_time - self.mario.invincible_start_timer) > 11000:self.play_music('main_theme', c.NORMAL)elif self.mario.dead:self.play_music('death', c.MARIO_DEAD)elif self.state == c.WORLD_CLEAR:passelif self.state == c.MARIO_DEAD:passelif self.state == c.GAME_OVER:passdef play_music(self, key, state):"""Plays new music"""pg.mixer.music.load(self.music_dict[key])pg.mixer.music.play()self.state = statedef stop_music(self):"""Stops playback"""pg.mixer.music.stop()2.4取得的分數(shù)🎑
__author__ = '源碼基地:#959755565#'import pygame as pg from .. import setup from .. import constants as cclass Digit(pg.sprite.Sprite):"""Individual digit for score"""def __init__(self, image):super(Digit, self).__init__()self.image = imageself.rect = image.get_rect()class Score(object):"""Scores that appear, float up, and disappear"""def __init__(self, x, y, score, flag_pole=False):self.x = xself.y = yif flag_pole:self.y_vel = -4else:self.y_vel = -3self.sprite_sheet = setup.GFX['item_objects']self.create_image_dict()self.score_string = str(score)self.create_digit_list()self.flag_pole_score = flag_poledef create_image_dict(self):"""Creates the dictionary for all the number 圖片 needed"""self.image_dict = {}image0 = self.get_image(1, 168, 3, 8)image1 = self.get_image(5, 168, 3, 8)image2 = self.get_image(8, 168, 4, 8)image4 = self.get_image(12, 168, 4, 8)image5 = self.get_image(16, 168, 5, 8)image8 = self.get_image(20, 168, 4, 8)image9 = self.get_image(32, 168, 5, 8)image10 = self.get_image(37, 168, 6, 8)image11 = self.get_image(43, 168, 5, 8)self.image_dict['0'] = image0self.image_dict['1'] = image1self.image_dict['2'] = image2self.image_dict['4'] = image4self.image_dict['5'] = image5self.image_dict['8'] = image8self.image_dict['3'] = image9self.image_dict['7'] = image10self.image_dict['9'] = image11def get_image(self, x, y, width, height):"""Extracts image from sprite sheet"""image = pg.Surface([width, height]).convert()rect = image.get_rect()image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))image.set_colorkey(c.BLACK)image = pg.transform.scale(image,(int(rect.width*c.BRICK_SIZE_MULTIPLIER),int(rect.height*c.BRICK_SIZE_MULTIPLIER)))return imagedef create_digit_list(self):"""Creates the group of 圖片 based on score received"""self.digit_list = []self.digit_group = pg.sprite.Group()for digit in self.score_string:self.digit_list.append(Digit(self.image_dict[digit]))self.set_rects_for_images()def set_rects_for_images(self):"""Set the rect attributes for each image in self.image_list"""for i, digit in enumerate(self.digit_list):digit.rect = digit.image.get_rect()digit.rect.x = self.x + (i * 10)digit.rect.y = self.ydef update(self, score_list, level_info):"""Updates score movement"""for number in self.digit_list:number.rect.y += self.y_velif score_list:self.check_to_delete_floating_scores(score_list, level_info)if self.flag_pole_score:if self.digit_list[0].rect.y <= 120:self.y_vel = 0def draw(self, screen):"""Draws score numbers onto screen"""for digit in self.digit_list:screen.blit(digit.image, digit.rect)def check_to_delete_floating_scores(self, score_list, level_info):"""Check if scores need to be deleted"""for i, score in enumerate(score_list):if int(score.score_string) == 1000:if (score.y - score.digit_list[0].rect.y) > 130:score_list.pop(i)else:if (score.y - score.digit_list[0].rect.y) > 75:score_list.pop(i)?3)完整的游戲🛒
由于代碼太多太多了如下圖所示:所以還是放在文末自己拿完整的代碼哈!
?4)效果展示(僅部分)🛒
4.1 Part 1 游戲運行界面——🎑
?4.2 Part 2 三條命——🎑
4.3 Part 3 吃了蘑菇的馬里奧——🎑
總結🎁
雖然現(xiàn)在市面上沖擊著各種游戲,但在我們心目中馬里奧依舊是那個留著意式大胡子,上天盾地,
無所不能,頭頂金幣,腳踏烏龜拯救公主的超級英雄!
對游戲感興趣的小伙伴兒趕緊自己動手造一個吧~
?
你們的支持是我最大的動力!!記得三連哦~mua?歡迎大家閱讀往期的文章哦~
關注小編獲取更多精彩內容!
?制作不易,記得一鍵三連哦!!?如需打包好的源碼+素材免費分享滴!!傳送門
總結
以上是生活随笔為你收集整理的全网最强Python版《超级玛丽》小游戏,我还是你的马里奥嘛?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python小游戏】扫雷游戏竟有世界排
- 下一篇: 【Opencv实战】入门也能学会的「黑白