一、前言

嗨喽,大家好呀!这里是小熊猫

在你的童年记忆里,是否有一个蹦跳、顶蘑菇的小人已经被遗忘?

马里奥是靠吃蘑菇成长,闻名世界的超级巨星。特征是大鼻子、头戴帽子、身穿背带工作服、还留着胡子。

帽子加背带工作服、大鼻子和胡子等特征,离英雄的形象相差甚远。

再加上少许肥胖的身材,稍不留神可能就会把我们的英雄马里奥当成在便利店打工的中年大叔。

但是形象上所带来的个性和亲切感,却在玩家的心中根深蒂固

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

今天我就带着大家自制一款超级玛丽游戏,还原度超高哦~

还在等什么动动手就能拥有属于自己的”超级玛丽“游戏呢,赶快学起来吧~

二、所需准备

demo

graphics

三、代码

__author__ = 'marble_xu'

DEBUG = False
DEBUG_START_X = 110
DEBUG_START_y = 538 SCREEN_HEIGHT = 600
SCREEN_WIDTH = 800
SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT) ORIGINAL_CAPTION = "超级玛丽 问题解答源码领取Q群:660193417" ## COLORS ##
# R G B
GRAY = (100, 100, 100)
NAVYBLUE = ( 60, 60, 100)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
FOREST_GREEN = ( 31, 162, 35)
BLUE = ( 0, 0, 255)
SKY_BLUE = ( 39, 145, 251)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = ( 0, 255, 255)
BLACK = ( 0, 0, 0)
NEAR_BLACK = ( 19, 15, 48)
COMBLUE = (233, 232, 255)
GOLD = (255, 215, 0) BGCOLOR = WHITE SIZE_MULTIPLIER = 2.5
BRICK_SIZE_MULTIPLIER = 2.69
BACKGROUND_MULTIPLER = 2.679
GROUND_HEIGHT = SCREEN_HEIGHT - 62 GAME_TIME_OUT = 301 #STATES FOR ENTIRE GAME
MAIN_MENU = 'main menu'
LOAD_SCREEN = 'load screen'
TIME_OUT = 'time out'
GAME_OVER = 'game over'
LEVEL = 'level' #MAIN MENU CURSOR STATES
PLAYER1 = '1 PLAYER GAME'
PLAYER2 = '2 PLAYER GAME' #GAME INFO DICTIONARY KEYS
COIN_TOTAL = 'coin total'
SCORE = 'score'
TOP_SCORE = 'top score'
LIVES = 'lives'
CURRENT_TIME = 'current time'
LEVEL_NUM = 'level num'
PLAYER_NAME = 'player name'
PLAYER_MARIO = 'mario'
PLAYER_LUIGI = 'luigi' #MAP COMPONENTS
MAP_IMAGE = 'image_name'
MAP_MAPS = 'maps'
SUB_MAP = 'sub_map'
MAP_GROUND = 'ground'
MAP_PIPE = 'pipe'
PIPE_TYPE_NONE = 0
PIPE_TYPE_IN = 1 # can go down in the pipe
PIPE_TYPE_HORIZONTAL = 2 # can go right in the pipe
MAP_STEP = 'step'
MAP_BRICK = 'brick'
BRICK_NUM = 'brick_num'
TYPE_NONE = 0
TYPE_COIN = 1
TYPE_STAR = 2
MAP_BOX = 'box'
TYPE_MUSHROOM = 3
TYPE_FIREFLOWER = 4
TYPE_FIREBALL = 5
TYPE_LIFEMUSHROOM = 6
MAP_ENEMY = 'enemy'
ENEMY_TYPE_GOOMBA = 0
ENEMY_TYPE_KOOPA = 1
ENEMY_TYPE_FLY_KOOPA = 2
ENEMY_TYPE_PIRANHA = 3
ENEMY_TYPE_FIRESTICK = 4
ENEMY_TYPE_FIRE_KOOPA = 5
ENEMY_RANGE = 'range'
MAP_CHECKPOINT = 'checkpoint'
ENEMY_GROUPID = 'enemy_groupid'
MAP_INDEX = 'map_index'
CHECKPOINT_TYPE_ENEMY = 0
CHECKPOINT_TYPE_FLAG = 1
CHECKPOINT_TYPE_CASTLE = 2
CHECKPOINT_TYPE_MUSHROOM = 3
CHECKPOINT_TYPE_PIPE = 4 # trigger player to go right in a pipe
CHECKPOINT_TYPE_PIPE_UP = 5 # trigger player to another map and go up out of a pipe
CHECKPOINT_TYPE_MAP = 6 # trigger player to go to another map
CHECKPOINT_TYPE_BOSS = 7 # defeat the boss
MAP_FLAGPOLE = 'flagpole'
FLAGPOLE_TYPE_FLAG = 0
FLAGPOLE_TYPE_POLE = 1
FLAGPOLE_TYPE_TOP = 2
MAP_SLIDER = 'slider'
HORIZONTAL = 0
VERTICAL = 1
VELOCITY = 'velocity'
MAP_COIN = 'coin' #COMPONENT COLOR
COLOR = 'color'
COLOR_TYPE_ORANGE = 0
COLOR_TYPE_GREEN = 1
COLOR_TYPE_RED = 2 #BRICK STATES
RESTING = 'resting'
BUMPED = 'bumped'
OPENED = 'opened' #MUSHROOM STATES
REVEAL = 'reveal'
SLIDE = 'slide' #Player FRAMES
PLAYER_FRAMES = 'image_frames'
RIGHT_SMALL_NORMAL = 'right_small_normal'
RIGHT_BIG_NORMAL = 'right_big_normal'
RIGHT_BIG_FIRE = 'right_big_fire' #PLAYER States
STAND = 'standing'
WALK = 'walk'
JUMP = 'jump'
FALL = 'fall'
FLY = 'fly'
SMALL_TO_BIG = 'small to big'
BIG_TO_FIRE = 'big to fire'
BIG_TO_SMALL = 'big to small'
FLAGPOLE = 'flag pole'
WALK_AUTO = 'walk auto' # not handle key input in this state
END_OF_LEVEL_FALL = 'end of level fall'
IN_CASTLE = 'in castle'
DOWN_TO_PIPE = 'down to pipe'
UP_OUT_PIPE = 'up out of pipe' #PLAYER FORCES
PLAYER_SPEED = 'speed'
WALK_ACCEL = 'walk_accel'
RUN_ACCEL = 'run_accel'
JUMP_VEL = 'jump_velocity'
MAX_Y_VEL = 'max_y_velocity'
MAX_RUN_SPEED = 'max_run_speed'
MAX_WALK_SPEED = 'max_walk_speed'
SMALL_TURNAROUND = .35
JUMP_GRAVITY = .31
GRAVITY = 1.01 #LIST of ENEMIES
GOOMBA = 'goomba'
KOOPA = 'koopa'
FLY_KOOPA = 'fly koopa'
FIRE_KOOPA = 'fire koopa'
FIRE = 'fire'
PIRANHA = 'piranha'
FIRESTICK = 'firestick' #GOOMBA Stuff
LEFT = 'left'
RIGHT = 'right'
JUMPED_ON = 'jumped on'
DEATH_JUMP = 'death jump' #KOOPA STUFF
SHELL_SLIDE = 'shell slide' #FLAG STATE
TOP_OF_POLE = 'top of pole'
SLIDE_DOWN = 'slide down'
BOTTOM_OF_POLE = 'bottom of pole' #FIREBALL STATE
FLYING = 'flying'
BOUNCING = 'bouncing'
EXPLODING = 'exploding' #IMAGE SHEET
ENEMY_SHEET = 'smb_enemies_sheet'
ITEM_SHEET = 'item_objects'
__author__ = 'marble_xu'

import pygame as pg
from .. import setup, tools
from .. import constants as c
from . import coin, powerup class Box(pg.sprite.Sprite):
def __init__(self, x, y, type, group=None, name=c.MAP_BOX):
pg.sprite.Sprite.__init__(self) self.frames = []
self.frame_index = 0
self.load_frames()
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y self.rest_height = y
self.animation_timer = 0
self.first_half = True # First half of animation cycle
self.state = c.RESTING
self.y_vel = 0
self.gravity = 1.2
self.type = type
self.group = group
self.name = name def load_frames(self):
sheet = setup.GFX['tile_set']
frame_rect_list = [(384, 0, 16, 16), (400, 0, 16, 16),
(416, 0, 16, 16), (400, 0, 16, 16), (432, 0, 16, 16)]
for frame_rect in frame_rect_list:
self.frames.append(tools.get_image(sheet, *frame_rect,
c.BLACK, c.BRICK_SIZE_MULTIPLIER)) def update(self, game_info):
self.current_time = game_info[c.CURRENT_TIME]
if self.state == c.RESTING:
self.resting()
elif self.state == c.BUMPED:
self.bumped() def resting(self):
time_list = [375, 125, 125, 125]
if (self.current_time - self.animation_timer) > time_list[self.frame_index]:
self.frame_index += 1
if self.frame_index == 4:
self.frame_index = 0
self.animation_timer = self.current_time self.image = self.frames[self.frame_index] def bumped(self):
self.rect.y += self.y_vel
self.y_vel += self.gravity if self.rect.y > self.rest_height + 5:
self.rect.y = self.rest_height
self.state = c.OPENED
if self.type == c.TYPE_MUSHROOM:
self.group.add(powerup.Mushroom(self.rect.centerx, self.rect.y))
elif self.type == c.TYPE_FIREFLOWER:
self.group.add(powerup.FireFlower(self.rect.centerx, self.rect.y))
elif self.type == c.TYPE_LIFEMUSHROOM:
self.group.add(powerup.LifeMushroom(self.rect.centerx, self.rect.y))
self.frame_index = 4
self.image = self.frames[self.frame_index] def start_bump(self, score_group):
self.y_vel = -6
self.state = c.BUMPED if self.type == c.TYPE_COIN:
self.group.add(coin.Coin(self.rect.centerx, self.rect.y, score_group))
import pygame as pg
from source.main import main if __name__=='__main__':
main()
pg.quit()

(https://jq.qq.com/?_wv=1027&k=ONMKhFSZ)

由于代码太多太多了如下图所示(一部分):所以还是大家自己拿完整的代码哈!

四、效果展示(仅部分)

4.1 运行效果页

4.2 开局页面

4.3 吃蘑菇变大效果

4.4 吃了太阳花效果

尾语

Thank you Mario! But our princess is 1n another castle! ”
谢谢你马里奥,但我们的公主在另一座城堡里!

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

你们的支持是我最大的动力!!感谢陪伴~

记得三连哦~ 欢迎大家阅读往期的文章呀

我是小熊猫,咱下篇文章见(✿◡‿◡)

python做小游戏——做个马里奥分分钟解决的更多相关文章

  1. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  2. 记一次python + selenium小项目出现的问题与解决办法

    记一次python + selenium小项目出现的问题与解决办法 如何接入代理 def crawl_xdaili(self):#代理 可不用 需要时 解除注释 """ ...

  3. python 金币小游戏

    我最近用python的pygame做了一个金币小游戏 游戏规则:移动挡板接住金币 游戏截图: 代码如下: import pygame.freetype import sys import random ...

  4. python 打字小游戏

    最近随便用python的pygame编了这个打字小游戏 只要有字母调到窗口底部就结束 上代码: import pygame.freetype import sys import random pyga ...

  5. Python——项目-小游戏2-动画绘制

    实现游戏循环还有事件的监听 在上一讲中 你需要完成这样的这样的效果, 如果你还没有完成,请不要继续往下阅读!!切记切记切记.,重要的事情说三遍 我们来看一下什么是游戏循环 所谓的游戏循环很好的理解 就 ...

  6. python学习 小游戏

    基于python3.4 while循环 #!/usr/bin/python3 #-*- coding=utf-8 -*- import random import sys import os luck ...

  7. Python——项目-小游戏

    开始我们的项目 飞机大战 1 项目的初体验 以及前期准备 游戏初体验画面 验证一下本地第三方包有没有导入 python3 -m pygame.examples.aliens 如果没有出现游戏画面请先安 ...

  8. 把川普射上太阳—如何用python制作小游戏

    1.准备 开始之前,你要确保Python和pip已经成功安装在电脑上噢,如果没有,请访问这篇文章:超详细Python安装指南 进行安装. Windows环境下打开Cmd(开始—运行—CMD),苹果系统 ...

  9. Python hangman小游戏

    hangman # words.py 使用pickle永久性存储数据 import pickle filename = 'words.pk' data = ['cat', 'dog', 'perro' ...

随机推荐

  1. canvas 隐藏 踩坑

    当我在把canvas绘制完成时,要把canvas隐藏起来.试了display 和  opacity 都不行. 然后我用了 position: absolute; left:1000px; top:0; ...

  2. 2021.08.16 P1078 文化之旅(最短路)

    2021.08.16 P1078 文化之旅(最短路) 题意: n个地,k个信仰,每个地都有自己的信仰,信仰之间会相互排斥,同信仰之间也会相互排斥,有m条路,问从s到t的最短距离是多少? 有一位使者要游 ...

  3. gnome shell 扩展命令行开启和关闭

    #关闭 gnome-shell-extension-tool -d 扩展名 #开启 gnome-shell-extension-tool -e 扩展名 例如: gnome-shell-extensio ...

  4. Revit二次开发之创建风管

      在Revit中,风管用于连接管件,风道末端和机械设备,今天简单尝试了下使用RevitAPI创建风管,现分享下我的方法.   风管从类型上可分为三类:一般风管,软风管和风管占位符:从形状上也分为三类 ...

  5. 基于.NetCore开发博客项目 StarBlog - (3) 模型设计

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  6. go ants源码分析

    golang ants 源码分析 结构图 poolwithfunc与pool相差不大,这里我们只分析ants默认pool的流程 文件 作用 ants.go 定义常量.errors显示.默认建一个大小为 ...

  7. gogin web框架部署学习

    首先去git上面找了一个gin框架拿来学习gin web开发: flipped-aurora/gin-vue-admin: 基于vite+vue3+gin搭建的开发基础平台(已完成setup语法糖版本 ...

  8. NS2中couldn‘t read file “../tcl/mobility/scene/cbr-3-test“: no such file or directory解决方法

    运行wireless.tcl 文件时报错:couldn't read file "../../uAMPS/ns-leach.tcl": no such file or direct ...

  9. 一次 HTTP 请求就需要一次 TCP 连接吗?

    一次 HTTP 请求就需要一次 TCP 连接吗? 本文写于 2021 年 2 月 9 日 太长不看版本:短连接需要,长连接不需要. 一次 HTTP 请求就需要一次 TCP 连接吗? TCP 的连接与断 ...

  10. drools中Fact的equality modes

    一.equality modes介绍 在drools中存在如下2种equality modes. 1.identity模式 identity:这是默认的情况.drools引擎使用IdentityHas ...