#!/usr/bin/env python # -*- coding: utf-8 -*- # pygame import pygame from pygame import * # WIN_WIDTH = 800 # WIN_HEIGHT = 640 # DISPLAY = (WIN_WIDTH, WIN_HEIGHT) # BACKGROUND_COLOR = "#004400" def main(): pygame.init() # PyGame, screen = pygame.display.set_mode(DISPLAY) # pygame.display.set_caption("Super Mario Boy") # bg = Surface((WIN_WIDTH,WIN_HEIGHT)) # # bg.fill(Color(BACKGROUND_COLOR)) # while 1: # for e in pygame.event.get(): # if e.type == QUIT: raise SystemExit, "QUIT" screen.blit(bg, (0,0)) # pygame.display.update() # if __name__ == "__main__": main()
PLATFORM_WIDTH = 32 PLATFORM_HEIGHT = 32 PLATFORM_COLOR = "#FF6262"
level = [ "-------------------------", "- -", "- -", "- -", "- -- -", "- -", "-- -", "- -", "- --- -", "- -", "- -", "- --- -", "- -", "- ----------- -", "- -", "- - -", "- -- -", "- -", "- -", "-------------------------"]
x=y=0 # for row in level: # for col in row: # if col == "-": # , pf = Surface((PLATFORM_WIDTH,PLATFORM_HEIGHT)) pf.fill(Color(PLATFORM_COLOR)) screen.blit(pf,(x,y)) x += PLATFORM_WIDTH # y += PLATFORM_HEIGHT # x = 0 #
#!/usr/bin/env python # -*- coding: utf-8 -*- from pygame import * MOVE_SPEED = 7 WIDTH = 22 HEIGHT = 32 COLOR = "#888888" class Player(sprite.Sprite): def __init__(self, x, y): sprite.Sprite.__init__(self) self.xvel = 0 # . 0 - self.startX = x # , self.startY = y self.image = Surface((WIDTH,HEIGHT)) self.image.fill(Color(COLOR)) self.rect = Rect(x, y, WIDTH, HEIGHT) # def update(self, left, right): if left: self.xvel = -MOVE_SPEED # = x- n if right: self.xvel = MOVE_SPEED # = x + n if not(left or right): # , self.xvel = 0 self.rect.x += self.xvel # xvel def draw(self, screen): # screen.blit(self.image, (self.rect.x,self.rect.y))
hero = Player(55,55) # (x,y) left = right = False # —
if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYUP and e.key == K_LEFT: left = False
hero.update(left, right) # hero.draw(screen) #
timer = pygame.time.Clock()
timer.tick(60)
JUMP_POWER = 10 GRAVITY = 0.35 # ,
self.yvel = 0 # self.onGround = False # ?
if up: if self.onGround: # , self.yvel = -JUMP_POWER
if not self.onGround: self.yvel += GRAVITY self.onGround = False; # , (( self.rect.y += self.yvel
up = false
if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYUP and e.key == K_UP: up = False
hero.update(left, right, up)
PLATFORM_WIDTH = 32 PLATFORM_HEIGHT = 32 PLATFORM_COLOR = "#FF6262"
class Platform(sprite.Sprite): def __init__(self, x, y): sprite.Sprite.__init__(self) self.image = Surface((PLATFORM_WIDTH, PLATFORM_HEIGHT)) self.image.fill(Color(PLATFORM_COLOR)) self.rect = Rect(x, y, PLATFORM_WIDTH, PLATFORM_HEIGHT)
entities = pygame.sprite.Group() # platforms = [] # , entities.add(hero)
if col == "-": # , pf = Surface((PLATFORM_WIDTH,PLATFORM_HEIGHT)) pf.fill(Color(PLATFORM_COLOR)) screen.blit(pf,(x,y))
if col == "-": pf = Platform(x,y) entities.add(pf) platforms.append(pf)
entities.draw(screen) #
def collide(self, xvel, yvel, platforms): for p in platforms: if sprite.collide_rect(self, p): # if xvel > 0: # self.rect.right = p.rect.left # if xvel < 0: # self.rect.left = p.rect.right # if yvel > 0: # self.rect.bottom = p.rect.top # self.onGround = True # - self.yvel = 0 # if yvel < 0: # self.rect.top = p.rect.bottom # self.yvel = 0 #
update(self, left, right, up, platforms)
self.rect.y += self.yvel self.rect.x += self.xvel # xvel
self.rect.y += self.yvel self.collide(0, self.yvel, platforms) self.rect.x += self.xvel # xvel self.collide(self.xvel, 0, platforms)
self.image = image.load("blocks/platform.png")
ANIMATION_DELAY = 0.1 # ANIMATION_RIGHT = [('mario/r1.png'), ('mario/r2.png'), ('mario/r3.png'), ('mario/r4.png'), ('mario/r5.png')] ANIMATION_LEFT = [('mario/l1.png'), ('mario/l2.png'), ('mario/l3.png'), ('mario/l4.png'), ('mario/l5.png')] ANIMATION_JUMP_LEFT = [('mario/jl.png', 0.1)] ANIMATION_JUMP_RIGHT = [('mario/jr.png', 0.1)] ANIMATION_JUMP = [('mario/j.png', 0.1)] ANIMATION_STAY = [('mario/0.png', 0.1)]
self.image.set_colorkey(Color(COLOR)) # # boltAnim = [] for anim in ANIMATION_RIGHT: boltAnim.append((anim, ANIMATION_DELAY)) self.boltAnimRight = pyganim.PygAnimation(boltAnim) self.boltAnimRight.play() # boltAnim = [] for anim in ANIMATION_LEFT: boltAnim.append((anim, ANIMATION_DELAY)) self.boltAnimLeft = pyganim.PygAnimation(boltAnim) self.boltAnimLeft.play() self.boltAnimStay = pyganim.PygAnimation(ANIMATION_STAY) self.boltAnimStay.play() self.boltAnimStay.blit(self.image, (0, 0)) # -, self.boltAnimJumpLeft= pyganim.PygAnimation(ANIMATION_JUMP_LEFT) self.boltAnimJumpLeft.play() self.boltAnimJumpRight= pyganim.PygAnimation(ANIMATION_JUMP_RIGHT) self.boltAnimJumpRight.play() self.boltAnimJump= pyganim.PygAnimation(ANIMATION_JUMP) self.boltAnimJump.play()
for anim in ANIMATION_LEFT: boltAnim.append((anim, ANIMATION_DELAY
))
if up: if self.onGround: # , self.yvel = -JUMP_POWER self.image.fill(Color(COLOR)) self.boltAnimJump.blit(self.image, (0, 0)) if left: self.xvel = -MOVE_SPEED # = x- n self.image.fill(Color(COLOR)) if up: # self.boltAnimJumpLeft.blit(self.image, (0, 0)) else: self.boltAnimLeft.blit(self.image, (0, 0)) if right: self.xvel = MOVE_SPEED # = x + n self.image.fill(Color(COLOR)) if up: self.boltAnimJumpRight.blit(self.image, (0, 0)) else: self.boltAnimRight.blit(self.image, (0, 0)) if not(left or right): # , self.xvel = 0 if not up: self.image.fill(Color(COLOR)) self.boltAnimStay.blit(self.image, (0, 0))
class Camera(object): def __init__(self, camera_func, width, height): self.camera_func = camera_func self.state = Rect(0, 0, width, height) def apply(self, target): return target.rect.move(self.state.topleft) def update(self, target): self.state = self.camera_func(self.state, target.rect)
def camera_configure(camera, target_rect): l, t, _, _ = target_rect _, _, w, h = camera l, t = -l+WIN_WIDTH / 2, -t+WIN_HEIGHT / 2 l = min(0, l) # l = max(-(camera.width-WIN_WIDTH), l) # t = max(-(camera.height-WIN_HEIGHT), t) # t = min(0, t) # return Rect(l, t, w, h)
total_level_width = len(level[0])*PLATFORM_WIDTH # total_level_height = len(level)*PLATFORM_HEIGHT # camera = Camera(camera_configure, total_level_width, total_level_height)
total_level_width = len(level[0])*PLATFORM_WIDTH # total_level_height = len(level)*PLATFORM_HEIGHT #
for e in entities: screen.blit(e.image, camera.apply(e))
camera.update(hero) #
level = [ "----------------------------------", "- -", "- -- -", "- -", "- -- -", "- -", "-- -", "- -", "- ---- --- -", "- -", "-- -", "- -", "- --- -", "- -", "- -", "- --- -", "- -", "- ------- ---- -", "- -", "- - -", "- -- -", "- -", "- -", "----------------------------------"]
Source: https://habr.com/ru/post/193888/