python實(shí)現(xiàn)超級(jí)馬里奧的案例-創(chuàng)新互聯(lián)

這篇文章主要介紹python實(shí)現(xiàn)超級(jí)馬里奧的案例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)主營(yíng)扶余網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),扶余h5微信小程序開發(fā)搭建,扶余網(wǎng)站營(yíng)銷推廣歡迎扶余等地區(qū)企業(yè)咨詢

主代碼

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)行成果

python實(shí)現(xiàn)超級(jí)馬里奧的案例

python實(shí)現(xiàn)超級(jí)馬里奧的案例

python實(shí)現(xiàn)超級(jí)馬里奧的案例

以上是“python實(shí)現(xiàn)超級(jí)馬里奧的案例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:python實(shí)現(xiàn)超級(jí)馬里奧的案例-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article22/cdogjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、定制網(wǎng)站、網(wǎng)站建設(shè)Google、網(wǎng)站排名、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司