失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python超级玛丽马里奥源代码

Python超级玛丽马里奥源代码

时间:2019-02-07 01:59:53

相关推荐

Python超级玛丽马里奥源代码

使用Python实现的超级马里奥、玛丽源程序,程序行入口marrio_level_1.py,本程序可实现单人或双人游戏。运行程序请需安装pygame,data为程序相关文件,其中components为程序中各种组件,resources为资源文件(含字体、声音、图形等)程序运行截图。完整源代码,文末有下载链接

main.py

__author__ = 'Python代码狂人'from . import setup,toolsfrom .states import main_menu,load_screen,level1from . import constants as cdef main():"""Add states to control here."""run_it = tools.Control(setup.ORIGINAL_CAPTION)state_dict = {c.MAIN_MENU: main_menu.Menu(),c.LOAD_SCREEN: load_screen.LoadScreen(),c.TIME_OUT: load_screen.TimeOut(),c.GAME_OVER: load_screen.GameOver(),c.LEVEL1: level1.Level1()}run_it.setup_states(state_dict, c.MAIN_MENU)run_it.main()

setup.py

__author__ = 'Pythont代码狂人'"""This module initializes the display and creates dictionaries of resources."""import osimport pygame as pgfrom . import toolsfrom .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"))

tools.py

__author__ = 'Python代码狂人'import osimport pygame as pgkeybinding = {'action':pg.K_s,'jump':pg.K_a,'left':pg.K_LEFT,'right':pg.K_RIGHT,'down':pg.K_DOWN}class Control(object):"""Control class for entire project. Contains the game loop, and containsthe event_loop which passes events to States as needed. Logic for flippingstates is also found here."""def __init__(self, caption):self.screen = pg.display.get_surface()self.done = Falseself.clock = pg.time.Clock()self.caption = captionself.fps = 60self.show_fps = Falseself.current_time = 0.0self.keys = pg.key.get_pressed()self.state_dict = {}self.state_name = Noneself.state = Nonedef setup_states(self, state_dict, start_state):self.state_dict = state_dictself.state_name = start_stateself.state = self.state_dict[self.state_name]def update(self):self.current_time = pg.time.get_ticks()if self.state.quit:self.done = Trueelif self.state.done:self.flip_state()self.state.update(self.screen, self.keys, self.current_time)def flip_state(self):previous, self.state_name = self.state_name, self.state.nextpersist = self.state.cleanup()self.state = self.state_dict[self.state_name]self.state.startup(self.current_time, persist)self.state.previous = previousdef event_loop(self):for event in pg.event.get():if event.type == pg.QUIT:self.done = Trueelif event.type == pg.KEYDOWN:self.keys = pg.key.get_pressed()self.toggle_show_fps(event.key)elif event.type == pg.KEYUP:self.keys = pg.key.get_pressed()self.state.get_event(event)def toggle_show_fps(self, key):if key == pg.K_F5:self.show_fps = not self.show_fpsif not self.show_fps:pg.display.set_caption(self.caption)def main(self):"""Main loop for entire program"""while not self.done:self.event_loop()self.update()pg.display.update()self.clock.tick(self.fps)if self.show_fps:fps = self.clock.get_fps()with_fps = "{} - {:.2f} FPS".format(self.caption, fps)pg.display.set_caption(with_fps)class _State(object):def __init__(self):self.start_time = 0.0self.current_time = 0.0self.done = Falseself.quit = Falseself.next = Noneself.previous = Noneself.persist = {}def get_event(self, event):passdef startup(self, current_time, persistant):self.persist = persistantself.start_time = current_timedef cleanup(self):self.done = Falsereturn self.persistdef update(self, surface, keys, current_time):passdef load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', 'jpg', 'bmp')):graphics = {}for pic in os.listdir(directory):name, ext = os.path.splitext(pic)if ext.lower() in accept:img = pg.image.load(os.path.join(directory, pic))if img.get_alpha():img = img.convert_alpha()else:img = img.convert()img.set_colorkey(colorkey)graphics[name]=imgreturn graphicsdef load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):songs = {}for song in os.listdir(directory):name,ext = os.path.splitext(song)if ext.lower() in accept:songs[name] = os.path.join(directory, song)return songsdef load_all_fonts(directory, accept=('.ttf')):return load_all_music(directory, accept)def load_all_sfx(directory, accept=('.wav','.mpe','.ogg','.mdi')):effects = {}for fx in os.listdir(directory):name, ext = os.path.splitext(fx)if ext.lower() in accept:effects[name] = pg.mixer.Sound(os.path.join(directory, fx))return effects

constansts.py

__author__ = 'Python代码狂人'SCREEN_HEIGHT = 600SCREEN_WIDTH = 800SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)ORIGINAL_CAPTION = "Super Mario Bros 1-1"## COLORS ### R G BGRAY = (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 = WHITESIZE_MULTIPLIER = 2.5BRICK_SIZE_MULTIPLIER = 2.69BACKGROUND_MULTIPLER = 2.679GROUND_HEIGHT = SCREEN_HEIGHT - 62#MARIO FORCESWALK_ACCEL = .15RUN_ACCEL = 20SMALL_TURNAROUND = .35GRAVITY = 1.01JUMP_GRAVITY = .31JUMP_VEL = -10FAST_JUMP_VEL = -12.5MAX_Y_VEL = 11MAX_RUN_SPEED = 800MAX_WALK_SPEED = 6#Mario StatesSTAND = 'standing'WALK = 'walk'JUMP = 'jump'FALL = 'fall'SMALL_TO_BIG = 'small to big'BIG_TO_FIRE = 'big to fire'BIG_TO_SMALL = 'big to small'FLAGPOLE = 'flag pole'WALKING_TO_CASTLE = 'walking to castle'END_OF_LEVEL_FALL = 'end of level fall'#GOOMBA StuffLEFT = 'left'RIGHT = 'right'JUMPED_ON = 'jumped on'DEATH_JUMP = 'death jump'#KOOPA STUFFSHELL_SLIDE = 'shell slide'#BRICK STATESRESTING = 'resting'BUMPED = 'bumped'#COIN STATESOPENED = 'opened'#MUSHROOM STATESREVEAL = 'reveal'SLIDE = 'slide'#COIN STATESSPIN = 'spin'#STAR STATESBOUNCE = 'bounce'#FIRE STATESFLYING = 'flying'BOUNCING = 'bouncing'EXPLODING = 'exploding'#Brick and coin box contentsMUSHROOM = 'mushroom'STAR = 'star'FIREFLOWER = 'fireflower'SIXCOINS = '6coins'COIN = 'coin'LIFE_MUSHROOM = '1up_mushroom'FIREBALL = 'fireball'#LIST of ENEMIESGOOMBA = 'goomba'KOOPA = 'koopa'#LEVEL STATESFROZEN = 'frozen'NOT_FROZEN = 'not frozen'IN_CASTLE = 'in castle'FLAG_AND_FIREWORKS = 'flag and fireworks'#FLAG STATETOP_OF_POLE = 'top of pole'SLIDE_DOWN = 'slide down'BOTTOM_OF_POLE = 'bottom of pole'#1UP scoreONEUP = '379'#MAIN MENU CURSOR STATESPLAYER1 = '1 player'PLAYER2 = '2 player'#OVERHEAD INFO STATESMAIN_MENU = 'main menu'LOAD_SCREEN = 'loading screen'LEVEL = 'level'GAME_OVER = 'game over'FAST_COUNT_DOWN = 'fast count down'END_OF_LEVEL = 'end of level'#GAME INFO DICTIONARY KEYSCOIN_TOTAL = 'coin total'SCORE = 'score'TOP_SCORE = 'top score'LIVES = 'lives'CURRENT_TIME = 'current time'LEVEL_STATE = 'level state'CAMERA_START_X = 'camera start x'MARIO_DEAD = 'mario dead'#STATES FOR ENTIRE GAMEMAIN_MENU = 'main menu'LOAD_SCREEN = 'load screen'TIME_OUT = 'time out'GAME_OVER = 'game over'LEVEL1 = 'level1'#SOUND STATEZNORMAL = 'normal'STAGE_CLEAR = 'stage clear'WORLD_CLEAR = 'world clear'TIME_WARNING = 'time warning'SPED_UP_NORMAL = 'sped up normal'MARIO_INVINCIBLE = 'mario invincible'

因代码太长,无法全部展示,完整代码下载地址:超级玛丽/download/weixin_42756970/85839538,

更多Python源代码,请关注公众号:Python代码大全,

如果觉得《Python超级玛丽马里奥源代码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。