配套视频教程
项目代码
最终效果
搭建项目结构
定义游戏常量
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修改
网友评论