示例#1
0
 def __init__(self):
     pygame.init()
     self.cell_size = pt.Point(16, 16)
     self.world = World(
         'assets/map.txt',
         {
             0: None,
             1: pygame.image.load('assets/grass.png'),
             2: pygame.image.load('assets/dirt.png'),
         },
         self.cell_size,
     )
     pygame.display.set_icon(pygame.image.load('assets/grass.png'))
     pygame.display.set_caption('Platformer test')
     real_size = pt.Point(1000, 600)
     size = pt.Point(real_size.x // 4, real_size.y // 4)
     super().__init__(pygame.display.set_mode(real_size), real_size, size)
     pygame.key.set_repeat(1000 // self.frame_rate)
     self.player = Player(pt.Point(128, 65))
     self.blocks = self.world.blocks
     self.camera_offset = pt.Point(5, 5)
     self.scroll_speed = 15
     self.bg1_rect = Rect(20, 20, 100, 100)
     self.bg1 = pygame.Surface(self.bg1_rect.size)
     self.bg1.fill((12, 150, 12))
     self.bg2_rect = Rect(50, 50, 200, 100)
     self.bg2 = pygame.Surface(self.bg2_rect.size)
     self.bg2.fill((12, 200, 12))
示例#2
0
 def update(self):
     self.screen.fill('skyblue')
     self.update_camera_offset()
     self.draw_paralax(self.bg1, self.bg1_rect, pt.Point(20, 20))
     self.draw_paralax(self.bg2, self.bg2_rect, pt.Point(2, 2))
     self.world.draw_blocks(self.screen, offset=self.get_int_offset())
     self.player.draw(self.screen, self.cell_size, self.get_int_offset())
     self.player.update(self.blocks, 0.2)
     self.keyboard_input()
示例#3
0
 def __init__(self, pos: pt.Point):
     super().__init__()
     self.surface = pygame.image.load('assets/player.png')
     self.rect = self.surface.get_rect()
     self.rect.topleft = pos
     self.velocity = pt.Point(0, 0)
     self.can_jump = True
示例#4
0
 def draw_blocks(self,
                 screen: pygame.Surface,
                 blocks: [(pygame.Surface, Rect)] = None,
                 offset: pt.Point = pt.Point(0, 0)):
     if not blocks:
         blocks = self.blocks
     for block in blocks:
         screen.blit(block.image,
                     ((block.rect.x + offset.x, block.rect.y + offset.y),
                      block.rect.size))
示例#5
0
 def update(self, blocks: [(pygame.Surface, Rect)], gravity: float):
     self.rect.x += self.velocity.x
     if self.velocity.y > 0:
         self.can_jump = False
     for index in self.get_rect().collidelistall(
         [block.rect for block in blocks]):
         if self.velocity.x > 0:  # hit wall on right
             self.rect.right = blocks.sprites()[index].rect.left
         elif self.velocity.x < 0:  # hit wall on left
             self.rect.left = blocks.sprites()[index].rect.right
     self.rect.y += self.velocity.y
     for index in self.get_rect().collidelistall(
         [block.rect for block in blocks]):
         if self.velocity.y > 0:  # hit floor
             self.rect.bottom = blocks.sprites()[index].rect.top
             self.velocity = self.velocity._replace(y=0)
             self.can_jump = True
         elif self.velocity.y < 0:  # hit ceiling
             self.rect.top = blocks.sprites()[index].rect.bottom
             self.velocity = self.velocity._replace(y=0)
     self.velocity = pt.Point(0, self.velocity.y + gravity)
     if self.velocity.y > 6:
         self.velocity = self.velocity._replace(y=6)
示例#6
0
 def get_int_offset(self):
     return pt.Point(int(self.camera_offset.x), int(self.camera_offset.y))