From f95ca8856c70c04d7b1aa5ec0d190731bd1b50ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E9=93=AD?= <2436077353@qq.com> Date: Thu, 30 May 2024 16:28:47 +0800 Subject: [PATCH] first commit --- .../__pycache__/player.cpython-312.pyc | Bin 12151 -> 11883 bytes .../__pycache__/stuff.cpython-312.pyc | Bin 0 -> 1045 bytes pythonProject1/source/components/player.py | 6 +- pythonProject1/source/components/stuff.py | 11 ++++ .../states/__pycache__/level.cpython-312.pyc | Bin 6547 -> 10138 bytes pythonProject1/source/states/level.py | 52 +++++++++++++++++- 6 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 pythonProject1/source/components/__pycache__/stuff.cpython-312.pyc diff --git a/pythonProject1/source/components/__pycache__/player.cpython-312.pyc b/pythonProject1/source/components/__pycache__/player.cpython-312.pyc index f4de60dd7f7ec58d2ce433ff879e65c9bdfb24db..a7ea98128dacec5b11cdf2e24effa00893fd549d 100644 GIT binary patch delta 249 zcmew!_d166G%qg~0}ve5k4Tf-$opKKX%6FLMKS5gf*N65j3pc(IUtz)ky~`~0V&qW zCpD!e=kwSrr!cN&fk@Rb#Ir$I3=EYFHAs34)ll?Ij#1-PMN(SHpvmkP;-<+})DF~N zWC|i|K!nBQ7A;T4w#nzT)L6I}cmyWvXm4k{KlzFFdB#VZx9Bi2GQOC+U3U{>&*Ut< zBsNag2*ytmlP~G{0R|P delta 461 zcmaDI^F5CDG%qg~0}$}Ygr{BE$opKK(PgrNn9O8Bjj+kvxkV>`;uhg7;Q^@tg2|4u zqWsw|3=BoO$YSD?cW{ds#p_LDN>!hM$?mV*W_=SYI+pNWf zJv%+G@W|YdlyPb>z0Rw6kyr66 J14sm{0RZyqaq0j7 diff --git a/pythonProject1/source/components/__pycache__/stuff.cpython-312.pyc b/pythonProject1/source/components/__pycache__/stuff.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..31004729f35caa6829159c5878bb87b0f48f3307 GIT binary patch literal 1045 zcma)4&ubG=5T3VrNj6PmRg&7;V!f2I3KffZDAtPJAf`rf|>~4N6*=+iDqg~m9 zhaOU>l%8_URr+^$@)RnhJbUxxO^9B4^35hM+l$~FX1@34n>XLi&evRS3WR-m^tR#< zfFF2}mK=lOmH=Ha!6XeRf&wqWR1U#RSYRuX97mhRSfaDU2o)0n(dbV+Y#TC(QS^>% zcw2-nSWqM;6cv*kf~Zln1gEcj%P~@89tP}#?+({Rbr*cRi|l_@*=bo3#}LlOS_t@3 z71(ObkSI)qw4zA1ot5`-JR8Fh^oi~e?CF>xkv$_(9IsVRx`#y6-;6mD=^2SaK%NT&-HZ&Md>{H1gztlYrBvcuq@k>RF8n!|!7#oRjyQ)YvTnJsisY$gVbDte4(+ z7V}C6PH87FYGvl^FxM8ZdZkw2*WBj#__*Y`ZDv>{!*yD2(`pKW=eH}B)mFf83?Fj1 zDXUU9Zi!bD?ju~PH%@X#xq-UaR~HBBa$jBUncvihmxYDy-sxWNb#K47d0u!hD6I7h zYv+Y09eO-E${c5}!~9%_e##6*FWA;77o)=& zh|x{yy5pMd2I6U5-*1;2qf8|DGq71v!V2h6Zsc&YJzUU37%Hjc{D^~TjWV1Z!z9Di z7!}<`G~ literal 0 HcmV?d00001 diff --git a/pythonProject1/source/components/player.py b/pythonProject1/source/components/player.py index 695cbd9..b637fe8 100644 --- a/pythonProject1/source/components/player.py +++ b/pythonProject1/source/components/player.py @@ -212,11 +212,7 @@ class Player(pygame.sprite.Sprite): self.y_vel = self.calc_vel(self.y_vel,self.gravity,self.max_y_vel) - # TODO WORKAROUND - if self.rect.bottom > C.GROUND_HEIGHT: - self.rect.bottom = C.GROUND_HEIGHT - self.y_vel = 0 - self.state = 'walk' + if keys[pygame.K_RIGHT]: self.x_vel = self.calc_vel(self.x_vel,self.x_accel,self.max_x_vel,True) diff --git a/pythonProject1/source/components/stuff.py b/pythonProject1/source/components/stuff.py index e69de29..2a4c633 100644 --- a/pythonProject1/source/components/stuff.py +++ b/pythonProject1/source/components/stuff.py @@ -0,0 +1,11 @@ +import pygame + +class Item(pygame.sprite.Sprite): + def __init__(self,x,y,w,h,name): + pygame.sprite.Sprite.__init__(self) + self.image = pygame.Surface((w,h)).convert() # 添加一个空的图层使其各自有一个宽高 + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y + self.name = name + diff --git a/pythonProject1/source/states/__pycache__/level.cpython-312.pyc b/pythonProject1/source/states/__pycache__/level.cpython-312.pyc index 557c5bb56a04960b00bdec4e0b1e751d1c234e6b..98139136a5bed450fe0c4ea5735dc057f901263c 100644 GIT binary patch delta 3677 zcmb7HYitwQ6`nhuaVDAZBerAb;SeW4g7e^2Qd$M}5m;z9X-HQTNCN9M9)~>a*n91i zI1Mtb_z||WY;-FD`v=lLSwy9RqHg)qic0%qt5zxliR_(4D*LNiRjL+KscO5@_M92V zIDu}}k$mRPJ@=e@=R4mybIiW>-VysBY_=*6u7B@7G5Vf+!Cp(o7TR8Jk5>*?5>Dk+ zORQ?R%DBtJGS406)QSt7D&6AIh~tKpg-hLSylzd#!kH-5#Z-EFbTncrO-YImPe&I% zJ+Mu!x&TJ14AQ15AnmFRq(ilXbgB-J)fs27nr$PEX!d1*OG5_gON^VOb_2!bE8{NC(`LeoO)pVe^`tNfs5q`Fw2?L>1uXd`O@f-xt<&jP=vUx2v6rP)2lCe~WzW)8dLo#R5NGcJ`J zqzJ^yc8S#19jtY;GwwbFrZooSc^#u)TTQX@o`u&UBZG!ecHYzkV6VVMfYVH1O3`0jx<0@T36 zY;8HN<(}~fsW}kmxkoMAmbwItv_MnIQ8A^R@o#wrjQ< zs#e=k;D{|j-WBprPtMtIR z`$MPhzd5Q6ow+|2d)OIYbxu98pob?G^i7TMmO=){Vf!F~eq;NKN$ZN#G#w2`;Qc0} znN(`s`+X1JPch$bItk>Aev{?xubNEcf5bDbreS%|ahnha<|ZNRRZoX7LnfSTQ3(%EK4Aa6llW*Wr|vAFxUv6I1_?=G;%~2>G+wkw2>;hD>4?1OonDc zkwh#uo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@W#|bI zMdx$~#_L?8a<TPh8o&9D!B7b~z)8VI`B(b}z9>_d%z2o#%q>PCTGG{u&WVqZkD zouK$#rEj6YT1YYHi~!N&IKq8yat5)@e+)9tqQE9IH3;GYSKuYFrQi|7V8JelDk*pc zF*`^Kc8j>PP$h^77z$!T!LGnEzeU_$Pz15PporooG-xY$te|xY;`XPY2$;L!9zmO4 z6-mUCi79|SMGaUwU|#1aeg&u%1=5`EL}3IB&qe)Q8^{0-jRXG>M~IH`691C+#S`ju hEcz0?z)sdR*8iDn{e-Lhgp>avqzMTWIF!cv{{WBXh$a94 delta 1157 zcmZ8gO=uHQ5Poly&3{6gCYm<>tV&V>T7Rm7T9s6=^#}D}K}#iUvuhg7pOcMkB2o`t zdTV(%!Hc3GA{2rb4<5aE^`~qRFM5+A=|S-1%-dG13;EvnX5P$vZ|3FEWib?a9}Wi? ze&3eIZ{7KAG>SeuU zLcy^*$k#V(Ba1)Igj<1Z&{i*+cg=Ev3q2a=A4HRX6t|QJ_lcIGi&dr0uZXQ#o>WraX_StKoa4

M;+Ag`ONwid?8q8Bs*EZ?Kb! zJ?0{?dqA1x;?AzEVo8e&CEhDe$GziTOd!7&gR@Aoe(>P}Qt26FwmO@~naMubDwK-T zmTZU3TejMguK({aCcejaMW}I&C4>iT*%uP85_`n4#7$gxs-JxQAQg7*Ys-9&S_D)J z_7TYXmxz%F+gmWJmI@w<-CeUNQ4CrK(Yz$)UIL z3ITOR;@Lq#&h{AX_a}^rGqX#YL|32&C=VCNURstEq0pqaNr+-Rj^PZZ#*GVzMDSP^eYvwJJ8X0rDbQ zwA&0+T);y>ZvqAhIEsTs|(c?$CA;Or2J${_pc;+%hB+gDil ZE8B+T3)5DZe}x5qxkotHx{OdJ`U5^%0vZ4S diff --git a/pythonProject1/source/states/level.py b/pythonProject1/source/states/level.py index c6197c1..0392de0 100644 --- a/pythonProject1/source/states/level.py +++ b/pythonProject1/source/states/level.py @@ -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