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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Niko Partners:2022年亞洲遊戲市場達到359 億美元
- 當國風遊戲遇上團扇手作,傳說中的合合島大玩國風“花活”
- 遊戲社會學研究:社會空間結構理論
- 提高Python執行效率的5個技巧!
- Python中insert用法及實戰案例!
- 前《極限競速:地平線》開發者再創業,正開發一款開放世界遊戲
- 《自在西遊》公測雙週流水破億!解讀2022年遊戲行業黑馬的發行秘訣!
- TapTap 2022 年度報告:從“玩什麼遊戲”到“怎樣玩遊戲”
- 華納雲:選擇香港遊戲伺服器的注意事項
- 如何用python語言獲得商品詳情資料API 返回值說明
- 中國音數協:2022 年中國遊戲市場收入2658.8 億元 同比下降10.33%
- Sensor Tower:2022年12月全球熱門移動遊戲下載量TOP10
- 打造“紙嫁衣宇宙”後,心動互娛開始探索“西式恐怖”的遊戲化表達方式
- [TIL] 如何使用 hex editor (二進位制編輯器)修改遊戲存檔
- 聞其聲而知雅意,基於Pytorch(mps/cpu/cuda)的人工智慧AI本地語音識別庫Whisper(Python3.10)
- 新發布丨一個關於遊戲開發的加速計劃
- 1月16日—1月22日共有17款遊戲開測|GameRes