From fe3e202b5c89d707ae12fc5fb1efbf5206472af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E9=93=AD?= <2436077353@qq.com> Date: Tue, 28 May 2024 23:19:31 +0800 Subject: [PATCH] first commit --- .../states/__pycache__/level.cpython-312.pyc | Bin 3823 -> 6547 bytes pythonProject1/source/states/level.py | 48 ++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/pythonProject1/source/states/__pycache__/level.cpython-312.pyc b/pythonProject1/source/states/__pycache__/level.cpython-312.pyc index fe48f5b571b63feeb3df193244fe7293c04a7b33..557c5bb56a04960b00bdec4e0b1e751d1c234e6b 100644 GIT binary patch literal 6547 zcmbtYZ%kX)6~E7Z51z3NG$D-G5c8)_uzkA%rIBrZ{w#<7dDQw&{J3 zfxSfOhiU?~6Nxm5qD=y(Nf1Tdl27fVNlT?l>$DH!fwPBRsa9#*hkoneL#2G$Iq&&B z+w3{&w2L|K+&}N$`_4JPbI!egal0!Blz(o&HTF{{A^*UNQP>=_CjhfZ1R^k#B*6q3 zh6pynK4}ZuppBihC)gmH;DQ{q6M=(zS+HE=%V0boEcG?^3WdaAZTqpy|<5MIFGc9!Jpt8y6tVHqi zRdxXMq`D_lVIhJ%w(BBR9usYC0*Xhx8WE8I?j)DKRF+K!d7FZ`Kuf zAG9t;C&xsTMdQ<+bFYrRB}S<@I-3|BNk=BaG%-R`@n}R6N2k)#L@Jr99v>A`(=-wt zh3TcJII3;9eJZW;p%83Y3WYM2n)cd_I-0yV0_0t?UDq(je&F7&se^}Or>1Uk;NHN8 z<6AXHvA)f_6y78Ao-ALl@eMNHkmY?E-z@XZS^jyAKP>ZypYYy2n3EJ4391_(xinPCn7UrW5_kaz+alo+OLrr28tu3lK@K(!PZpASZp#HN`@2B9IL&~jEU%e z8lliyU}!av%t2j`Mdo%8I>Z4Wpv;=)InZJ4V*kDV#i4sc8%-x34s6wQL!Gg11fC zNaj58BgT&M&u7URWVR4{5si&cNNQDn!rW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9K8M()pU)`C69m+2*SjT=Q_mP!9R! zL;jWM`cLQMJSX=3Da=D7gAlJ&^y!N zNxg@D4>qjb=}|n;Q#?{pOsy$?4#l8G5xbfw?i0=A_52%MDMf<5L_7}UJ+f;n=jwM4 zuv}oblIH|wx0dBbnB7V{*SYIrIqz;|8F1As*S-f;9YER~u-S@8DlwHxf@2Z2D{UHp zDOExdQ`!v)#i&i&k)SP74-&i^(q^Ee$l*y`+ul?DwEn@}6wgoSCB>sa;PA#UUy_C| di2LuP9+)pl;}^v71#y1M_A*T4EDIA delta 1579 zcmZuxOK2NM7@k?}u2$OBO12)cUa@3|taTkHCJ>yaHgy}PlsKw!97>vlkiDBketTAK zEMq4rthDpc*$TB!CHA5%vbVk@kSP<}c zQj8h(V8{n?TJp;UFRhHO1vu)%@9}=I(LMt(8o(8=A7A1n&+}fP|MmKkZ-Tp*#luoP zOA#nc#&nKr?`q@OPQ`;s{kg^UEVlCBCSLU;9 zT}cXLGbT5YL;0zEI{`8Omm9}1SS+HAhBZVkTZZ1n^0VU~P zIP>~oUtcEswkZwt_nzw;%$`2mKlCPQvmA?-hMzRFq<^e8c;_h~B>?aY>|e-XnGz6M zynLVY`&3weHQ=rxEwvHS25@u0kMD;>2N-qZdz>FqI{Wc}?7`>u`K~%mc6dBrKoOGF zo?$LOk-Mnbn9}Z|4kC6ahz^nc3k;;jdAAK1oi_@uev*i5U{mmxrINHXw4AMEKRvrH zytJ!1UpZe5ri@@ZwaVTV*TibN%Sd;v3&+S)MJKK-m*l0(8$z=+=^4d^hnfyt4U)VT z=MiNQqH~LA$E*_9faPmH4D^jfR5-~`PY`h()EJJ7)ntx)xh6ZfWKD8#(VE0_hqrwW zE@21L5XEh}N2VsGrzW+@f{rWL@j>BK{FxnV3>% p$F&~xD$!Bq==+KI!vP`O0`Z@K=MjjJ%@#=f$);eU28giY{{!VjbqW9g diff --git a/pythonProject1/source/states/level.py b/pythonProject1/source/states/level.py index 4fadd68..c6197c1 100644 --- a/pythonProject1/source/states/level.py +++ b/pythonProject1/source/states/level.py @@ -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) \ No newline at end of file