龙空技术网

Python实现超级玛丽游戏系列教程01玛丽登场

豆约翰老师 140

前言:

今天朋友们对“超级玛丽的代码是什么”可能比较看重,你们都需要分析一些“超级玛丽的代码是什么”的相关内容。那么小编在网络上网罗了一些关于“超级玛丽的代码是什么””的相关内容,希望同学们能喜欢,姐妹们快快来了解一下吧!

项目代码

最终效果

搭建项目结构

定义游戏常量

SCREEN_HEIGHT = 600SCREEN_WIDTH = 800SCREEN_SIZE = (SCREEN_WIDTH,SCREEN_HEIGHT)ORIGINAL_CAPTION = "SuperMario"GFX = None## COLORS ###            R    G    BGRAY         = (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 = WHITESIZE_MULTIPLIER = 2.5
初始化游戏窗口

tools.py

import pygame as pgfrom . import constants as cclass 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 sysimport pygame as pgfrom data import toolsfrom data import constants as cif __name__=='__main__':    control = tools.Control(c.ORIGINAL_CAPTION)    control.main()    pg.quit()    sys.exit()
玛丽登场

level1.py

import pygame as pgfrom .. import constants as cfrom .. components import marioclass 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 pgfrom .. import constants as cclass 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修改

标签: #超级玛丽的代码是什么