zoukankan      html  css  js  c++  java
  • Python实现超级玛丽游戏系列教程01玛丽登场

    配套视频教程

    配套视频教程

    项目代码

    项目代码

    最终效果

    搭建项目结构

    定义游戏常量

    SCREEN_HEIGHT = 600
    SCREEN_WIDTH = 800
    
    SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)
    
    ORIGINAL_CAPTION = "SuperMario"
    
    GFX = None
    
    ## COLORS ##
    
    #            R    G    B
    GRAY         = (100, 100, 100)
    NAVYBLUE     = ( 60,  60, 100)
    WHITE        = (255, 255, 255)
    RED          = (255,   0,   0)
    GREEN        = (  0, 255,   0)
    FOREST_GREEN = ( 31, 162,  35)
    BLUE         = (  0,   0, 255)
    YELLOW       = (255, 255,   0)
    ORANGE       = (255, 128,   0)
    PURPLE       = (255,   0, 255)
    CYAN         = (  0, 255, 255)
    BLACK        = (  0,   0,   0)
    NEAR_BLACK    = ( 19,  15,  48)
    COMBLUE      = (233, 232, 255)
    GOLD         = (255, 215,   0)
    
    BGCOLOR = WHITE
    
    SIZE_MULTIPLIER = 2.5
    
    

    初始化游戏窗口

    tools.py

    import pygame as pg
    from . import constants as c
    
    class Control(object):
        def __init__(self, caption):
            pg.init()
            pg.display.set_caption(c.ORIGINAL_CAPTION)
            self.screen = pg.display.set_mode(c.SCREEN_SIZE)
            self.done = False
            self.clock = pg.time.Clock()
            self.caption = caption
            self.fps = 60
            self.show_fps = True
            self.keys = pg.key.get_pressed()
    
        def update(self):
            pg.display.get_surface().fill(c.BGCOLOR)
    
        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()
    
        def toggle_show_fps(self, key):
            if key == pg.K_F5:
                self.show_fps = not self.show_fps
    
        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)
    
    

    mario_level_1.py

    import sys
    import pygame as pg
    
    from data import tools
    from data import constants as c
    
    if __name__=='__main__':
        control = tools.Control(c.ORIGINAL_CAPTION)
        control.main()
        pg.quit()
        sys.exit()
    

    玛丽登场

    level1.py

    import pygame as pg
    from .. import constants as c
    from .. components import mario
    
    class Level1:
        def __init__(self):
            self.startup()
    
        def startup(self):
            self.mario = mario.Mario()
            self.setup_mario_location()
            self.all_sprites = pg.sprite.Group(self.mario)
    
        def setup_mario_location(self):
            self.mario.rect.x = 80
            self.mario.rect.bottom = c.SCREEN_HEIGHT - self.mario.rect.height
    
        def update(self, surface):
            """Updates level"""
            pg.display.get_surface().fill(c.BGCOLOR)
            self.all_sprites.draw(surface)
    

    mario.py

    import pygame as pg
    from .. import constants as c
    
    class Mario(pg.sprite.Sprite):
        def __init__(self):
            pg.sprite.Sprite.__init__(self)
            self.sprite_sheet = c.GFX['mario_bros']
    
            self.right_frames = []
            self.left_frames = []
            self.frame_index = 0
            self.load_from_sheet()
            self.image = self.right_frames[self.frame_index]
            self.rect = self.image.get_rect()
    
        def get_image(self, x, y, width, height):
            image = pg.Surface([width, height]).convert()
            rect = image.get_rect()
    
            image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
            image.set_colorkey(c.BLACK)
            image = pg.transform.scale(image,
                                       (int(rect.width * c.SIZE_MULTIPLIER),
                                        int(rect.height * c.SIZE_MULTIPLIER)))
            return image
    
        def load_from_sheet(self):
            self.right_frames.append(
                self.get_image(178, 32, 12, 16))  # right
    

    tools.py修改

  • 相关阅读:
    日期时间工具类
    jQuery 使用attr()方式设置 checked 失效原因及解决方法
    vue总结
    【笔记】golang中使用protocol buffers的底层库直接解码二进制数据
    【笔记】对golang的大量小对象的管理真的是无语了……
    【记录一个问题】thanos receiver在更换tsdb文件后,内存并未显著下降
    【笔记】论文阅读:《Gorilla: 一个快速, 可扩展的, 内存式时序数据库》
    【分享】thanos receiver的grafana报表配置
    【采坑小计】thanos receiver的官方文档中,并未说明tsdb落盘的配置方式
    【记录一个问题】thanos receiver在tsdb切换期间,导致remote write接口失败增加
  • 原文地址:https://www.cnblogs.com/neuedu/p/python-super-mario-from-scratch.html
Copyright © 2011-2022 走看看