为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Python

配套视频教程

www.bilibili.com/video/BV1ua411c7m3

项目代码

gitee.com/kamiba/python_super_mario_game_code

最终效果

搭建项目结构

定义游戏常量

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修改

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消