python實現超級瑪麗小遊戲(動圖演示+原始碼分享)
import os
import pygame as pg
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 = pg.display.get_surface()
self.done = False
self.clock = pg.time.Clock()
self.caption = caption
self.fps = 60
self.show_fps = False
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.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, self.state.next
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 pg.event.get():
if event.type == pg.QUIT:
self.done = True
elif 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_fps
if 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.0
self.current_time = 0.0
self.done = False
self.quit = False
self.next = None
self.previous = None
self.persist = {}
def get_event(self, event):
pass
def startup(self, current_time, persistant):
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))
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
img.set_colorkey(colorkey)
graphics[name]=img
return graphics
def load_all_music(directory, accept=('.wav', '.mp3', '.ogg', '.mdi')):
songs = {}
for song in os.listdir(directory):
name,ext = 跟單網gendan5.comos.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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2932351/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python實現超級瑪麗遊戲系列教程01瑪麗登場Python遊戲
- Scratch(五):Scratch小遊戲之超級瑪麗遊戲
- 用python實現植物大戰殭屍(遊戲截圖+動態演示+原始碼分享)Python遊戲原始碼
- JAVA 實現《超級瑪麗升級版》遊戲Java遊戲
- 如何用 Python 實現超級瑪麗的介面和狀態機?Python
- thinkphp實現超級外賣訂餐系統原始碼演示下載PHP原始碼
- python tkinter 超級瑪麗原始碼下載顏色碰撞檢測試程式.pyPython原始碼
- jQuery實現省市區三級聯動完整版原始碼演示下載jQuery原始碼
- 演算法提高 超級瑪麗演算法
- 超級炫酷的3D旋轉動態圖——Python程式碼實現3DPython
- PHP自定義實現微信分享朋友圈原始碼演示下載PHP原始碼
- NOI1.1題庫——超級瑪麗
- 如何讓你的機器學習玩超級瑪麗機器學習
- 100行Python程式碼實現貪吃蛇小遊戲(超詳細)Python遊戲
- 超級瑪麗啟示錄:曾風靡全球的FC小遊戲,為何能征服億萬人的心?遊戲
- Python程式碼實現“FlappyBird”小遊戲PythonAPP遊戲
- [藍橋杯][演算法提高VIP]超級瑪麗演算法
- JavaScript實現垃圾分類小遊戲教程,附原始碼!JavaScript遊戲原始碼
- Python魂鬥羅小遊戲原始碼Python遊戲原始碼
- 通達信超級捉妖指標公式原始碼副圖指標公式原始碼
- 自動升級系統的設計與實現(原始碼)原始碼
- 《css大法》之使用偽元素實現超實用的圖示庫(附原始碼)CSS原始碼
- (完整原始碼)貪吃蛇小遊戲——HTML+CSS+JavaScript實現原始碼遊戲HTMLCSSJavaScript
- 手機直播原始碼,實現圖片瀑布流式滑動效果原始碼
- Canvas drag 實現拖拽拼圖小遊戲Canvas遊戲
- 8個超炫酷的HTML5動畫演示及原始碼HTML動畫原始碼
- 直播平臺原始碼,純JS實現左右滑動輪播圖原始碼JS
- 動態圖片演示,原始碼直析:手把手講解IPC框架,最新整理原始碼框架
- FPGA -- SPI 時序實現(超級靈活,超級好用)FPGA
- 直播原始碼網站,直播間小遊戲java遞迴的實現方式原始碼網站遊戲Java遞迴
- 手動實現一個promise(原始碼)Promise原始碼
- Python實現三子棋小遊戲Python遊戲
- Python實現快遞分揀小程式(附原始碼和超詳細註釋)Python原始碼
- 【Python】Python抓取分享頁面的原始碼示例Python原始碼
- 分享一個圖片驗證碼功能的實現
- iOS超級簽名系統演示(免費體驗)iOS
- Express原始碼級實現の路由全解析(下闋)Express原始碼路由
- myBatis原始碼解析-二級快取的實現方式MyBatis原始碼快取