first commit

main
夏铭 6 months ago
parent 14e475406e
commit fe3e202b5c

@ -3,43 +3,73 @@ import pygame
from .. import tools, setup
from .. import constants as C
from .. components import player
import os
import json
class Level:
def __init__(self):
self.finished = False
self.next = None
self.info = info.Info('level')
self.load_map_data()
self.setup_background()
self.setup_start_positions()
self.setup_player()
def load_map_data(self):
file_name = 'level_1.json'
file_path = os.path.join('source/data/maps',file_name)
with open(file_path) as f:
self.map_data = json.load(f)
def setup_background(self):
self.background = setup.GRAPHICS['level_1']
self.image_name = self.map_data['image_name']
self.background = setup.GRAPHICS[self.image_name]
rect = self.background.get_rect()
self.background = pygame.transform.scale(self.background,(int(rect.width * C.BG_MULTI),
int(rect.height * C.BG_MULTI)))
self.background_rect = self.background.get_rect()
self.game_window = setup.SCREEN.get_rect()
self.game_ground = pygame.Surface((self.background_rect.width,self.background_rect.height))
def setup_start_positions(self):
self.positions = []
for data in self.map_data['maps']:
self.positions.append((data['start_x'],data['end_x'],data['player_x'],data['player_y']))
self.start_x,self.end_x,self.player_x,self.player_y = self.positions[0]
def setup_player(self):
self.player = player.Player('mario')
self.player.rect.x = 300
self.player.rect.y = 490
self.player.rect.x = self.game_window.x + self.player_x
self.player.rect.bottom = self.player_y
def update(self, surface, keys):
self.player.update(keys)
self.update_player_position()
self.update_game_window()
self.draw(surface)
def update_player_position(self):
self.player.rect.x += self.player.x_vel
if self.player.rect.x < 0:
self.player.rect.x = 0
if self.player.rect.x > C.SCREEN_W -16*C.PLAYER_MULTI:
self.player.rect.x = C.SCREEN_W -16*C.PLAYER_MULTI
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.player.rect.y += self.player.y_vel
def update_game_window(self):
third = self.game_window.x + self.game_window.width/3
if self.player.x_vel > 0 and self.player.rect.centerx > third and self.game_window.right < self.end_x: # 判断移动
self.game_window.x += self.player.x_vel # 用窗口更新代替主角移动
self.start_x = self.game_window.x
def draw(self, surface):
surface.blit(self.background,(0,0))
surface.blit(self.player.image, self.player.rect)
self.game_ground.blit(self.background,self.game_window,self.game_window)
self.game_ground.blit(self.player.image,self.player.rect) # 将背景与人物化境
surface.blit(self.game_ground,(0,0), self.game_window) # 渲染
self.info.draw(surface)
Loading…
Cancel
Save