python实现超级马里奥的案例

这篇文章主要介绍python实现超级马里奥的案例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

主代码

importpygameaspg
fromsource.mainimportmain

if__name__=='__main__':
main()
pg.quit()

main

__author__='marble_xu'

importpygameaspg
from.importsetup,tools
from.importconstantsasc
from.statesimportmain_menu,load_screen,level

defmain():
game=tools.Control()
state_dict={c.MAIN_MENU:main_menu.Menu(),
c.LOAD_SCREEN:load_screen.LoadScreen(),
c.LEVEL:level.Level(),
c.GAME_OVER:load_screen.GameOver(),
c.TIME_OUT:load_screen.TimeOut()}
game.setup_states(state_dict,c.MAIN_MENU)
game.main()

setup

__author__='marble_xu'

importos
importpygameaspg
from.importconstantsasc
from.importtools

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()

GFX=tools.load_all_gfx(os.path.join("resources","graphics"))

tools

__author__='marble_xu'

importos
importpygameaspg
fromabcimportABC,abstractmethod

keybinding={
'action':pg.K_s,
'jump':pg.K_a,
'left':pg.K_LEFT,
'right':pg.K_RIGHT,
'down':pg.K_DOWN
}

classState():
def__init__(self):
self.start_time=0.0
self.current_time=0.0
self.done=False
self.next=None
self.persist={}

@abstractmethod
defstartup(self,current_time,persist):
'''abstractmethod'''

defcleanup(self):
self.done=False
returnself.persist

@abstractmethod
defupdate(sefl,surface,keys,current_time):
'''abstractmethod'''

classControl():
def__init__(self):
self.screen=pg.display.get_surface()
self.done=False
self.clock=pg.time.Clock()
self.fps=60
self.current_time=0.0
self.keys=pg.key.get_pressed()
self.state_dict={}
self.state_name=None
self.state=None

defsetup_states(self,state_dict,start_state):
self.state_dict=state_dict
self.state_name=start_state
self.state=self.state_dict[self.state_name]

defupdate(self):
self.current_time=pg.time.get_ticks()
ifself.state.done:
self.flip_state()
self.state.update(self.screen,self.keys,self.current_time)

defflip_state(self):
previous,self.state_name=self.state_name,self.state.next
persist=self.state.cleanup()
self.state=self.state_dict[self.state_name]
self.state.startup(self.current_time,persist)

defevent_loop(self):
foreventinpg.event.get():
ifevent.type==pg.QUIT:
self.done=True
elifevent.type==pg.KEYDOWN:
self.keys=pg.key.get_pressed()
elifevent.type==pg.KEYUP:
self.keys=pg.key.get_pressed()

defmain(self):
whilenotself.done:
self.event_loop()
self.update()
pg.display.update()
self.clock.tick(self.fps)

defget_image(sheet,x,y,width,height,colorkey,scale):
image=pg.Surface([width,height])
rect=image.get_rect()

image.blit(sheet,(0,0),(x,y,width,height))
image.set_colorkey(colorkey)
image=pg.transform.scale(image,
(int(rect.width*scale),
int(rect.height*scale)))
returnimage

defload_all_gfx(directory,colorkey=(255,0,255),accept=('.png','.jpg','.bmp','.gif')):
graphics={}
forpicinos.listdir(directory):
name,ext=os.path.splitext(pic)
ifext.lower()inaccept:
img=pg.image.load(os.path.join(directory,pic))
ifimg.get_alpha():
img=img.convert_alpha()
else:
img=img.convert()
img.set_colorkey(colorkey)
graphics[name]=img
returngraphics

运行成果

python实现超级马里奥的案例

python实现超级马里奥的案例

python实现超级马里奥的案例

以上是“python实现超级马里奥的案例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2021-03-24 01:19:50
收藏
分享
海报
0 条评论
162
上一篇:Python如何实现鼠标自动在屏幕上随机移动功能 下一篇:Python如何实现对adb命令封装
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码