diff --git a/pythonProject1/source/components/__pycache__/player.cpython-312.pyc b/pythonProject1/source/components/__pycache__/player.cpython-312.pyc index f4de60d..a7ea981 100644 Binary files a/pythonProject1/source/components/__pycache__/player.cpython-312.pyc and b/pythonProject1/source/components/__pycache__/player.cpython-312.pyc differ 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 0000000..3100472 Binary files /dev/null and b/pythonProject1/source/components/__pycache__/stuff.cpython-312.pyc differ 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 557c5bb..9813913 100644 Binary files a/pythonProject1/source/states/__pycache__/level.cpython-312.pyc and b/pythonProject1/source/states/__pycache__/level.cpython-312.pyc differ 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