python实现超级马里奥

 更新时间:2020年03月18日 08:18:44   作者:直言的伪都鹧  
这篇文章主要为大家详细介绍了python实现超级马里奥,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python写超级马里奥的具体代码,供大家参考,具体内容如下

完整代码和素材戳我

主代码

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

运行成果

好了,被忘了在GitHub里面点star喔。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • 在Python中marshal对象序列化的相关知识

    在Python中marshal对象序列化的相关知识

    这篇文章主要介绍了在Python中marshal对象序列化的相关知识,是Python进阶学习中序列化相关的知识,需要的朋友可以参考下
    2015-07-07
  • Python光学仿真光的偏振编程理解学习

    Python光学仿真光的偏振编程理解学习

    这篇文章主要为大家介绍了通过Python光学仿真来理解光的偏振编程学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-10-10
  • tensorflow构建BP神经网络的方法

    tensorflow构建BP神经网络的方法

    这篇文章主要为大家详细介绍了tensorflow构建BP神经网络的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Python反爬虫技术之防止IP地址被封杀的讲解

    Python反爬虫技术之防止IP地址被封杀的讲解

    今天小编就为大家分享一篇关于Python反爬虫技术之防止IP地址被封杀的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • python去除字符串中的换行符

    python去除字符串中的换行符

    这篇文章主要介绍了python去除字符串中的换行符的相关资料,然后在文章下面给大家补充介绍了python去除空格和换行符的方法,需要的朋友可以参考下
    2017-10-10
  • 10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径

    10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径

    这篇文章主要介绍了10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • Python matplotlib实用绘图技巧汇总

    Python matplotlib实用绘图技巧汇总

    这篇文章主要给大家介绍了关于Python matplotlib实用绘图技巧的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • python3使用print打印带颜色的字符串代码实例

    python3使用print打印带颜色的字符串代码实例

    这篇文章主要介绍了python3使用print打印带颜色的字符串代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python基础之错误和异常处理

    python基础之错误和异常处理

    这篇文章主要介绍了python错误和异常处理,实例分析了Python中返回一个返回值与多个返回值的方法,需要的朋友可以参考下
    2021-10-10
  • python rolling regression. 使用 Python 实现滚动回归操作

    python rolling regression. 使用 Python 实现滚动回归操作

    这篇文章主要介绍了python rolling regression. 使用 Python 实现滚动回归操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06

最新评论