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 c
def 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 c
ORIGINAL_CAPTION = c.ORIGINAL_CAPTION
在线商城管理系统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 = _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 pg
keybinding ={
keybinding ={
'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 contains    the event_loop which passes events to States as needed. Logic for flipping    states is also found here."""
def__init__(self, caption):
self.screen = _surface()
self.done =False
self.clock = pg.time.Clock()
self.caption = captionaccess考试
self.fps =60
self.show_fps =False
self.current_time =0.0
self.keys = _pressed()
self.state_dict ={}
self.state_name =None
self.state =None
def setup_states(self, state_dict, start_state):
self.state_dict = state_dict
self.state_name = start_state
self.state = self.state_dict[self.state_name]
def update(self):
self.current_time = _ticks()
if self.state.quit:
self.done =True
elif 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,
persist = self.state.cleanup()
self.state = self.state_dict[self.state_name]
self.state.startup(self.current_time, persist)
self.state.previous = previous
def event_loop(self):
for event in ():
pe== pg.QUIT:
self.done =True
pe== pg.KEYDOWN:
self.keys = _pressed()
pe== pg.KEYUP:
self.keys = _pressed()
_event(event)
def toggle_show_fps(self, key):
if key == pg.K_F5:
self.show_fps =not self.show_fps
if not self.show_fps:
pg.display.set_caption(self.caption)
def main(self):
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 = _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.0
self.current_time =0.0
self.done =False
self.quit =False
<=None
self.previous =None
self.persist ={}
def get_event(self, event):
pass
def startup(self, current_time, persistant):
python基础代码大全下载self.persist = persistant
self.start_time = current_time
def cleanup(self):
self.done =False
return self.persist
def update(self, surface, keys, current_time):
pass
def 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))
_alpha():
img = vert_alpha()
else:
img = vert()
img.set_colorkey(colorkey)
graphics[name]=img
return graphics
def load_all_music(directory, accept=('.wav','.mp3','.ogg','.mdi')):
songs ={}
this is的3种用法
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 songs
def 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 =600
SCREEN_WIDTH =800
SCREEN_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 = WHITE
SIZE_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 =.35
GRAVITY =1.01
JUMP_GRAVITY =.31
JUMP_VEL =-10
FAST_JUMP_VEL =-12.5
MAX_Y_VEL =11
MAX_RUN_SPEED =800
MAX_WALK_SPEED =6
#Mario States
STAND ='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 Stuff
LEFT ='left'
RIGHT ='right'
JUMPED_ON ='jumped on'
DEATH_JUMP ='death jump'
#KOOPA STUFF
SHELL_SLIDE ='shell slide'
#BRICK STATES
RESTING ='resting'
BUMPED ='bumped'
#COIN STATES
OPENED ='opened'
#MUSHROOM STATES
REVEAL ='reveal'
SLIDE ='slide'
#COIN STATES
SPIN ='spin'
#STAR STATES
BOUNCE ='bounce'
#FIRE STATES
FLYING ='flying'
BOUNCING ='bouncing'
EXPLODING ='exploding'
#Brick and coin box contents
MUSHROOM ='mushroom'
STAR ='star'
FIREFLOWER ='fireflower'
SIXCOINS ='6coins'
COIN ='coin'
LIFE_MUSHROOM ='1up_mushroom' FIREBALL ='fireball'
#LIST of ENEMIES
GOOMBA ='goomba'
KOOPA ='koopa'

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。