|
|
|
@ -2,7 +2,7 @@ from ..components import info
|
|
|
|
|
import pygame
|
|
|
|
|
from .. import tools, setup
|
|
|
|
|
from .. import constants as C
|
|
|
|
|
from .. components import player
|
|
|
|
|
from .. components import player,stuff
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
@ -15,6 +15,7 @@ class Level:
|
|
|
|
|
self.setup_background()
|
|
|
|
|
self.setup_start_positions()
|
|
|
|
|
self.setup_player()
|
|
|
|
|
self.setup_ground_items()
|
|
|
|
|
|
|
|
|
|
def load_map_data(self):
|
|
|
|
|
file_name = 'level_1.json'
|
|
|
|
@ -46,6 +47,12 @@ class Level:
|
|
|
|
|
self.player.rect.x = self.game_window.x + self.player_x
|
|
|
|
|
self.player.rect.bottom = self.player_y
|
|
|
|
|
|
|
|
|
|
def setup_ground_items(self):
|
|
|
|
|
self.ground_items_group = pygame.sprite.Group() # 组存放多个精灵类,方便处理
|
|
|
|
|
for name in ['ground','pipe','step']:
|
|
|
|
|
for item in self.map_data[name]:
|
|
|
|
|
self.ground_items_group.add(stuff.Item(item['x'],item['y'],item['width'],item['height'],name))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update(self, surface, keys):
|
|
|
|
|
self.player.update(keys)
|
|
|
|
@ -55,12 +62,55 @@ class Level:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_player_position(self):
|
|
|
|
|
|
|
|
|
|
# x判定
|
|
|
|
|
self.player.rect.x += self.player.x_vel
|
|
|
|
|
if self.player.rect.x < self.start_x:
|
|
|
|
|
self.player.rect.x = self.start_x
|
|
|
|
|
elif self.player.rect.right > self.end_x:
|
|
|
|
|
self.player.rect.right = self.end_x
|
|
|
|
|
self.check_x_collisions()
|
|
|
|
|
|
|
|
|
|
# y判定
|
|
|
|
|
self.player.rect.y += self.player.y_vel
|
|
|
|
|
self.check_y_collisions()
|
|
|
|
|
|
|
|
|
|
def check_x_collisions(self): # x方向
|
|
|
|
|
ground_item = pygame.sprite.spritecollideany(self.player, self.ground_items_group) # 检测碰撞
|
|
|
|
|
if ground_item:
|
|
|
|
|
self.adjust_player_x(ground_item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_y_collisions(self): # y方向
|
|
|
|
|
ground_item = pygame.sprite.spritecollideany(self.player, self.ground_items_group) # 检测碰撞
|
|
|
|
|
if ground_item:
|
|
|
|
|
self.adjust_player_y(ground_item)
|
|
|
|
|
self.check_will_fall(self.player) # 坠落检测
|
|
|
|
|
|
|
|
|
|
def adjust_player_x(self,sprite):
|
|
|
|
|
if self.player.rect.x < sprite.rect.x:
|
|
|
|
|
self.player.rect.right = sprite.rect.left # 重置位置为最左边
|
|
|
|
|
else:
|
|
|
|
|
self.player.rect.left = sprite.rect.right
|
|
|
|
|
self.player.x_vel = 0
|
|
|
|
|
|
|
|
|
|
def adjust_player_y(self,sprite):
|
|
|
|
|
if self.player.rect.bottom < sprite.rect.bottom:
|
|
|
|
|
self.player.y_vel = 0
|
|
|
|
|
self.player.rect.bottom = sprite.rect.top
|
|
|
|
|
self.player.state = 'walk'
|
|
|
|
|
else:
|
|
|
|
|
self.player.y_vel = 7
|
|
|
|
|
self.player.rect.top = sprite.rect.bottom
|
|
|
|
|
self.player.state = 'fall'
|
|
|
|
|
|
|
|
|
|
def check_will_fall(self,sprite):
|
|
|
|
|
sprite.rect.y += 1 # 下落一个像素
|
|
|
|
|
check_ground = pygame.sprite.Group(self.ground_items_group) # 检测是否碰撞
|
|
|
|
|
collided = pygame.sprite.spritecollideany(sprite, check_ground)
|
|
|
|
|
if not collided and sprite.state != 'jump' : # 如果没有或者处于跳起则不坠落
|
|
|
|
|
sprite.state = 'fall'
|
|
|
|
|
sprite.rect.y -= 1
|
|
|
|
|
|
|
|
|
|
def update_game_window(self):
|
|
|
|
|
third = self.game_window.x + self.game_window.width/3
|
|
|
|
|