python实现超级马里奥


Posted in Python onMarch 18, 2020

本文实例为大家分享了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

运行成果

python实现超级马里奥

python实现超级马里奥

python实现超级马里奥

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

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

Python 相关文章推荐
python正则分组的应用
Nov 10 Python
python之DataFrame实现excel合并单元格
Feb 22 Python
Flask框架WTForm表单用法示例
Jul 20 Python
Python多线程原理与用法详解
Aug 20 Python
python 遍历列表提取下标和值的实例
Dec 25 Python
python抓取搜狗微信公众号文章
Apr 01 Python
将python文件打包成EXE应用程序的方法
May 22 Python
Python 中Django验证码功能的实现代码
Jun 20 Python
python二进制读写及特殊码同步实现详解
Oct 11 Python
关于TensorFlow新旧版本函数接口变化详解
Feb 10 Python
对python中arange()和linspace()的区别说明
May 03 Python
Python超简单容易上手的画图工具库推荐
May 10 Python
Python开发企业微信机器人每天定时发消息实例
Mar 17 #Python
10个python3常用排序算法详细说明与实例(快速排序,冒泡排序,桶排序,基数排序,堆排序,希尔排序,归并排序,计数排序)
Mar 17 #Python
Python Selenium安装及环境配置的实现
Mar 17 #Python
详解python环境安装selenium和手动下载安装selenium的方法
Mar 17 #Python
使用Python+selenium实现第一个自动化测试脚本
Mar 17 #Python
python中的selenium安装的步骤(浏览器自动化测试框架)
Mar 17 #Python
利用python在excel中画图的实现方法
Mar 17 #Python
You might like
joomla jce editor 解决上传中文名文件失败问题
2013/06/09 PHP
php安装swoole扩展的方法
2015/03/19 PHP
购物车实现的几种方式优缺点对比
2018/05/02 PHP
PHP使用redis位图bitMap 实现签到功能
2019/10/08 PHP
PHP设计模式(七)组合模式Composite实例详解【结构型】
2020/05/02 PHP
经常用到的JavasScript事件的翻译
2007/04/09 Javascript
关于IE7 IE8弹出窗口顶上
2008/12/22 Javascript
JavaScript对象链式操作代码(jquery)
2010/07/04 Javascript
jquery 图片上传按比例预览插件集合
2011/05/28 Javascript
jquery中的事件处理详细介绍
2013/06/24 Javascript
代码分析jQuery四种静态方法使用
2015/07/23 Javascript
基于nodejs+express(4.x+)实现文件上传功能
2015/11/23 NodeJs
SublimeText自带格式化代码功能之reindent
2015/12/27 Javascript
bootstrap-wysiwyg结合ajax实现图片上传实时刷新功能
2016/05/27 Javascript
JS传参及动态修改页面布局
2017/04/13 Javascript
jQuery获取随机颜色的实例代码
2018/05/21 jQuery
详解小程序input框失焦事件在提交事件前的处理
2019/05/05 Javascript
vue把输入框的内容添加到页面的实例讲解
2019/11/11 Javascript
django认证系统 Authentication使用详解
2019/07/22 Python
从pandas一个单元格的字符串中提取字符串方式
2019/12/17 Python
python3光学字符识别模块tesserocr与pytesseract的使用详解
2020/02/26 Python
python3利用Axes3D库画3D模型图
2020/03/25 Python
keras 自定义loss层+接受输入实例
2020/06/28 Python
Python Pygame实现俄罗斯方块
2021/02/19 Python
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
连卡佛中国官网:Lane Crawford中文站
2018/01/27 全球购物
联想台湾官网:Lenovo TW
2018/05/09 全球购物
办公室文秘岗位职责
2013/11/15 职场文书
办公设备采购方案
2014/03/16 职场文书
大学生党员自我剖析材料
2014/10/06 职场文书
业务员岗位职责范本
2015/04/03 职场文书
检讨书格式
2015/05/07 职场文书
天那边观后感
2015/06/09 职场文书
2015秋季运动会通讯稿
2015/07/18 职场文书
java中用float时,数字后面加f,这样是为什么你知道吗
2021/09/04 Java/Android
我的收音机情缘
2022/04/05 无线电