python實(shí)現(xiàn)超級(jí)馬里奧
本文實(shí)例為大家分享了Python寫超級(jí)馬里奧的具體代碼,供大家參考,具體內(nèi)容如下
完整代碼和素材戳我
主代碼
import pygame as pg from source.main import main if __name__=='__main__': main() pg.quit()
main
__author__ = 'marble_xu'
import pygame as pg
from . import setup, tools
from . import constants as c
from .states import main_menu, load_screen, level
def 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 os
import pygame as pg
from . import constants as c
from . import tools
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'
import os
import pygame as pg
from abc import ABC, abstractmethod
keybinding = {
'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 image
def 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
運(yùn)行成果



好了,被忘了在GitHub里面點(diǎn)star喔。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html的步驟
sphinx主要用于編寫 reStructuredText 和 Markdown 格式技術(shù)文檔,編寫此類技術(shù)文檔時(shí)Sphinx工具可將其轉(zhuǎn)為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫Rst文檔轉(zhuǎn)html,需要的朋友可以參考下2023-03-03
Python入門學(xué)習(xí)之Python流處理過(guò)程
本篇文章屬于Python入門篇,本文主要教大家學(xué)習(xí)Python流處理過(guò)程,通過(guò)Faust流處理庫(kù)來(lái)為大家詳細(xì)講解,有需要的朋友可以借鑒參考下2021-09-09
linux 下實(shí)現(xiàn)python多版本安裝實(shí)踐
這篇文章主要介紹了linux 下實(shí)現(xiàn)python多版本安裝實(shí)踐,需要的朋友可以參考下2014-11-11
Python面向?qū)ο笾蓄悾╟lass)的簡(jiǎn)單理解與用法分析
這篇文章主要介紹了Python面向?qū)ο笾蓄悾╟lass)的簡(jiǎn)單理解與用法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類(class)的基本概念、原理、定義與使用方法,需要的朋友可以參考下2020-02-02
python正常時(shí)間和unix時(shí)間戳相互轉(zhuǎn)換的方法
這篇文章主要介紹了python正常時(shí)間和unix時(shí)間戳相互轉(zhuǎn)換的方法,涉及時(shí)間字符串與Unix時(shí)間戳的實(shí)現(xiàn)與轉(zhuǎn)換技巧,需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)的簡(jiǎn)單讀寫csv文件操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單讀寫csv文件操作,結(jié)合實(shí)例形式分析了Python使用csv模塊針對(duì)csv文件進(jìn)行讀寫操作的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-07-07
python實(shí)現(xiàn)百萬(wàn)答題自動(dòng)百度搜索答案
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)百萬(wàn)答題自動(dòng)百度搜索答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

