您的位置:网站首页 > 动态 > 动态

超级玛丽单机版(小玛莉水果机单机版)

2022-10-05 11:44     发布者:张俊明
导读导语哈喽!哈喽!我是木木子,今日游戏更新——超级玛丽华丽上线啦!“超级玛丽”有多少人还记得这款经典游戏?对于90、00后应该不大熟悉,但多多少少印象中见过那个戴帽子的大胡子穿着背带裤的马里奥!??这款

导语

哈喽!哈喽!我是木木子,今日游戏更新——超级玛丽华丽上线啦

“超级玛丽”有多少人还记得这款经典游戏?对于90、00后应该不大熟悉,但多多少少印象中见过

那个戴帽子的大胡子穿着背带裤的马里奥

??

这款游戏1985年发售,因上手简单、情节有趣等因素迅速走红!

?

陪伴70后、80后走过了青涩难忘的童年超级玛丽成了大家心目中的经典!

如果你的童年也曾被魔性的 灯~灯灯~灯~灯灯~灯洗脑那就接着来怀旧一番吧~

今天木木子就带着大家自制一款超级玛丽游戏,还原度超高哦~还在等什么动动手就能拥有属于自

己的”超级玛丽“游戏呢,赶快学起来吧??~

?

正文

嗯呐~写游戏Python还是用的Pygame模块啦

1)准备中

1.1环境安装

Python3、Pycharm、Pygame模块很多自带的模块等。

模块安装统一用的豆瓣镜像源:?

pip install -i http://pypi.douban.com/simple/ +模块名。

1.2图片素材+背景音乐+字体(可修改)

?

2)开始敲代码

2.1 运行程序:mario_level_1.py。

!/usr/bin/env python
__author__ = &39;超级玛丽-源码基地:959755565&39;

&34;&34;&34;
This is an attempt to recreate the first level of
Super Mario Bros for the NES.
&34;&34;&34;

import sys
import pygame as pg
from data.main import main
import cProfile


if __name__==&39;__main__&39;:
    main()
    pg.quit()
    sys.exit()

2.2 配置音乐文字等setup.py。

__author__ = &39;Python源码基地:959755565人&39;

&34;&34;&34;
This module initializes the display and creates dictionaries of resources.
&34;&34;&34;

import os
import pygame as pg
from . import tools
from .import constants as c

ORIGINAL_CAPTION = c.ORIGINAL_CAPTION


os.environ[&39;SDL_VIDEO_CENTERED&39;] = &39;1&39;
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(&34;resources&34;,&34;fonts&34;))
MUSIC = tools.load_all_music(os.path.join(&34;resources&34;,&34;music&34;))
GFX   = tools.load_all_gfx(os.path.join(&34;resources&34;,&34;graphics&34;))
SFX   = tools.load_all_sfx(os.path.join(&34;resources&34;,&34;sound&34;))

2.3游戏音乐设置game_sound.py。

__author__ = &39;Python源码基地:959755565&39;

import pygame as pg
from . import setup
from . import constants as c

class Sound(object):
    &34;&34;&34;Handles all sound for the game&34;&34;&34;
    def __init__(self, overhead_info):
        &34;&34;&34;Initialize the class&34;&34;&34;
        self.sfx_dict = setup.SFX
        self.music_dict = setup.MUSIC
        self.overhead_info = overhead_info
        self.game_info = overhead_info.game_info
        self.set_music_mixer()



    def set_music_mixer(self):
        &34;&34;&34;Sets music for level&34;&34;&34;
        if self.overhead_info.state == c.LEVEL:
            pg.mixer.music.load(self.music_dict[&39;main_theme&39;])
            pg.mixer.music.play()
            self.state = c.NORMAL
        elif self.overhead_info.state == c.GAME_OVER:
            pg.mixer.music.load(self.music_dict[&39;game_over&39;])
            pg.mixer.music.play()
            self.state = c.GAME_OVER


    def update(self, game_info, mario):
        &34;&34;&34;Updates sound object with game info&34;&34;&34;
        self.game_info = game_info
        self.mario = mario
        self.handle_state()

    def  handle_state(self):
        &34;&34;&34;Handles the state of the soundn object&34;&34;&34;
        if self.state == c.NORMAL:
            if self.mario.dead:
                self.play_music(&39;death&39;, c.MARIO_DEAD)
            elif self.mario.invincible 
                    and self.mario.losing_invincibility == False:
                self.play_music(&39;invincible&39;, c.MARIO_INVINCIBLE)
            elif self.mario.state == c.FLAGPOLE:
                self.play_music(&39;flagpole&39;, c.FLAGPOLE)
            elif self.overhead_info.time == 100:
                self.play_music(&39;out_of_time&39;, c.TIME_WARNING)


        elif self.state == c.FLAGPOLE:
            if self.mario.state == c.WALKING_TO_CASTLE:
                self.play_music(&39;stage_clear&39;, c.STAGE_CLEAR)

        elif self.state == c.STAGE_CLEAR:
            if self.mario.in_castle:
                self.sfx_dict[&39;count_down&39;].play()
                self.state = c.FAST_COUNT_DOWN

        elif self.state == c.FAST_COUNT_DOWN:
            if self.overhead_info.time == 0:
                self.sfx_dict[&39;count_down&39;].stop()
                self.state = c.WORLD_CLEAR

        elif self.state == c. TIME_WARNING:
            if pg.mixer.music.get_busy() == 0:
                self.play_music(&39;main_theme_sped_up&39;, c.SPED_UP_NORMAL)
            elif self.mario.dead:
                self.play_music(&39;death&39;, c.MARIO_DEAD)

        elif self.state == c.SPED_UP_NORMAL:
            if self.mario.dead:
                self.play_music(&39;death&39;, c.MARIO_DEAD)
            elif self.mario.state == c.FLAGPOLE:
                self.play_music(&39;flagpole&39;, c.FLAGPOLE)

        elif self.state == c.MARIO_INVINCIBLE:
            if (self.mario.current_time - self.mario.invincible_start_timer) > 11000:
                self.play_music(&39;main_theme&39;, c.NORMAL)
            elif self.mario.dead:
                self.play_music(&39;death&39;, c.MARIO_DEAD)


        elif self.state == c.WORLD_CLEAR:
            pass
        elif self.state == c.MARIO_DEAD:
            pass
        elif self.state == c.GAME_OVER:
            pass

    def play_music(self, key, state):
        &34;&34;&34;Plays new music&34;&34;&34;
        pg.mixer.music.load(self.music_dict[key])
        pg.mixer.music.play()
        self.state = state

    def stop_music(self):
        &34;&34;&34;Stops playback&34;&34;&34;
        pg.mixer.music.stop()

2.4取得的分数

__author__ = &39;源码基地:959755565&39;

import pygame as pg
from .. import setup
from .. import constants as c


class Digit(pg.sprite.Sprite):
    &34;&34;&34;Individual digit for score&34;&34;&34;
    def __init__(self, image):
        super(Digit, self).__init__()
        self.image = image
        self.rect = image.get_rect()


class Score(object):
    &34;&34;&34;Scores that appear, float up, and disappear&34;&34;&34;
    def __init__(self, x, y, score, flag_pole=False):
        self.x = x
        self.y = y
        if flag_pole:
            self.y_vel = -4
        else:
            self.y_vel = -3
        self.sprite_sheet = setup.GFX[&39;item_objects&39;]
        self.create_image_dict()
        self.score_string = str(score)
        self.create_digit_list()
        self.flag_pole_score = flag_pole


    def create_image_dict(self):
        &34;&34;&34;Creates the dictionary for all the number 图片 needed&34;&34;&34;
        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[&39;0&39;] = image0
        self.image_dict[&39;1&39;] = image1
        self.image_dict[&39;2&39;] = image2
        self.image_dict[&39;4&39;] = image4
        self.image_dict[&39;5&39;] = image5
        self.image_dict[&39;8&39;] = image8
        self.image_dict[&39;3&39;] = image9
        self.image_dict[&39;7&39;] = image10
        self.image_dict[&39;9&39;] = image11


    def get_image(self, x, y, width, height):
        &34;&34;&34;Extracts image from sprite sheet&34;&34;&34;
        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 image


    def create_digit_list(self):
        &34;&34;&34;Creates the group of 图片 based on score received&34;&34;&34;
        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):
        &34;&34;&34;Set the rect attributes for each image in self.image_list&34;&34;&34;
        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.y


    def update(self, score_list, level_info):
        &34;&34;&34;Updates score movement&34;&34;&34;
        for number in self.digit_list:
            number.rect.y += self.y_vel

        if 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 = 0


    def draw(self, screen):
        &34;&34;&34;Draws score numbers onto screen&34;&34;&34;
        for digit in self.digit_list:
            screen.blit(digit.image, digit.rect)


    def check_to_delete_floating_scores(self, score_list, level_info):
        &34;&34;&34;Check if scores need to be deleted&34;&34;&34;
        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.0 展示动态视频一波,完美。

超级马里奥动态视频

4.1 Part 1 游戏运行界面——

?

?4.2 Part 2 三条命——

?

4.3 Part 3 吃了蘑菇的马里奥——

?

总结

虽然现在市面上冲击着各种游戏,但在我们心目中马里奥依旧是那个留着意式大胡子,上天遁地,

无所不能,头顶金币,脚踏乌龟拯救公主的超级英雄!

对游戏感兴趣的小伙伴儿赶紧自己动手造一个吧~

??

你们的支持是我最大的动力!!记得三连哦~mua 欢迎大家阅读往期的文章哦~

关注小编获取更多精彩内容!私信小编06获取源码吖!

??

?

免责声明:本文章由会员“张俊明”发布如果文章侵权,请联系我们处理,本站仅提供信息存储空间服务如因作品内容、版权和其他问题请于本站联系

猜你喜欢

最新文章