python怎么做超级玛丽_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修改

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值