时间:2021-05-22
本文实例为大家分享了Python写超级马里奥的具体代码,供大家参考,具体内容如下
完整代码和素材戳我
主代码
import pygame as pgfrom source.main import mainif __name__=='__main__': main() pg.quit()main
__author__ = 'marble_xu'import pygame as pgfrom . import setup, toolsfrom . import constants as cfrom .states import main_menu, load_screen, leveldef main(): 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'import osimport pygame as pgfrom . import constants as cfrom . import toolspg.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'import osimport pygame as pgfrom abc import ABC, abstractmethodkeybinding = { 'action':pg.K_s, 'jump':pg.K_a, 'left':pg.K_LEFT, 'right':pg.K_RIGHT, 'down':pg.K_DOWN}class State(): def __init__(self): self.start_time = 0.0 self.current_time = 0.0 self.done = False self.next = None self.persist = {} @abstractmethod def startup(self, current_time, persist): '''abstract method''' def cleanup(self): self.done = False return self.persist @abstractmethod def update(sefl, surface, keys, current_time): '''abstract method'''class Control(): 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 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 = pg.time.get_ticks() if 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.next persist = self.state.cleanup() self.state = self.state_dict[self.state_name] self.state.startup(self.current_time, persist) def event_loop(self): for event in pg.event.get(): if event.type == pg.QUIT: self.done = True elif event.type == pg.KEYDOWN: self.keys = pg.key.get_pressed() elif event.type == pg.KEYUP: self.keys = pg.key.get_pressed() def main(self): while not self.done: self.event_loop() self.update() pg.display.update() self.clock.tick(self.fps)def get_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))) return imagedef load_all_gfx(directory, colorkey=(255,0,255), accept=('.png', '.jpg', '.bmp', '.gif')): 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] = img return graphics运行成果
好了,被忘了在GitHub里面点star喔。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今年苹果新品的发布会上,马里奥闯进苹果手机,让人们大为震惊。任天堂与苹果的跨界合作,预示着前者在手游领域开拓新市场,也让任天堂的股价大幅上涨。而《超级马里奥Ru
2016年任天堂公布第一款IOS游戏《超级马里奥?酷跑》后,消费者就可以在APPStore里收到这款游戏上市的通知,这样的事情在AppStore是第一次出现。但
12月10日消息今天凌晨,国行Switch将正式开售,售价2099元,预装《新超级马里欧兄弟U》体验版,游戏完整版售价299元。 除了2099元的版本之外
2018年6月2日,奥买家全球购旗下“奥买家全球精品店”全新升级,并在广州番禺奥园广场闪耀开幕。此次升级,奥买家为消费者带来了基于“新零售”的全新购物体验,实现
奥维互动地图如何下载地图?奥维互动地图是一款超级强大的跨平台地图浏览器,内含Google地图、Google卫星图、地形图、百度地图、搜狗地图、全球地图离线下载等