Python超级玛丽马里奥源代码
生活随笔
收集整理的這篇文章主要介紹了
Python超级玛丽马里奥源代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Python實現的超級馬里奧、瑪麗源程序,程序行入口marrio_level_1.py,本程序可實現單人或雙人游戲。運行程序請需安裝pygame,data為程序相關文件,其中components為程序中各種組件,resources為資源文件(含字體、聲音、圖形等)程序運行截圖。完整源代碼,文末有下載鏈接
main.py
__author__ = 'Python代碼狂人'from . import setup,tools from .states import main_menu,load_screen,level1 from . 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 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"))tools.py
__author__ = 'Python代碼狂人'import os import 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 effectsconstansts.py
__author__ = 'Python代碼狂人'SCREEN_HEIGHT = 600 SCREEN_WIDTH = 800SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)ORIGINAL_CAPTION = "Super Mario Bros 1-1"## 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 = WHITESIZE_MULTIPLIER = 2.5 BRICK_SIZE_MULTIPLIER = 2.69 BACKGROUND_MULTIPLER = 2.679 GROUND_HEIGHT = SCREEN_HEIGHT - 62#MARIO FORCES WALK_ACCEL = .15 RUN_ACCEL = 20 SMALL_TURNAROUND = .35GRAVITY = 1.01 JUMP_GRAVITY = .31 JUMP_VEL = -10 FAST_JUMP_VEL = -12.5 MAX_Y_VEL = 11MAX_RUN_SPEED = 800 MAX_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 STATES OPENED = '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 STATE TOP_OF_POLE = 'top of pole' SLIDE_DOWN = 'slide down' BOTTOM_OF_POLE = 'bottom of pole'#1UP score ONEUP = '379'#MAIN MENU CURSOR STATES PLAYER1 = '1 player' PLAYER2 = '2 player'#OVERHEAD INFO STATES MAIN_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 KEYS COIN_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 GAME MAIN_MENU = 'main menu' LOAD_SCREEN = 'load screen' TIME_OUT = 'time out' GAME_OVER = 'game over' LEVEL1 = 'level1'#SOUND STATEZ NORMAL = 'normal' STAGE_CLEAR = 'stage clear' WORLD_CLEAR = 'world clear' TIME_WARNING = 'time warning' SPED_UP_NORMAL = 'sped up normal' MARIO_INVINCIBLE = 'mario invincible'因代碼太長,無法全部展示,完整代碼下載地址:超級瑪麗https://download.csdn.net/download/weixin_42756970/85839538,
更多Python源代碼,請關注公眾號:Python代碼大全,
總結
以上是生活随笔為你收集整理的Python超级玛丽马里奥源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Excel制作抽奖软件
- 下一篇: ESLint : Expected li