Python 精灵以不同的速度移动

Python 精灵以不同的速度移动,python,pygame,sprite,Python,Pygame,Sprite,我在2D游戏中遇到了一个问题,但我不知道为什么。 我想在玩家到达游戏屏幕中间时移动精灵,但当精灵离开屏幕1倍时,它们开始以较慢的速度移动(这对我来说没有意义…) 这是我的代码中唯一的一部分,我在其中定义了精灵的这种移动: if self.player.rect.centerx >= WIDTH/2: # Ensuring the player stays in the middle of the screen self.pla

我在2D游戏中遇到了一个问题,但我不知道为什么。 我想在玩家到达游戏屏幕中间时移动精灵,但当精灵离开屏幕1倍时,它们开始以较慢的速度移动(这对我来说没有意义…)

这是我的代码中唯一的一部分,我在其中定义了精灵的这种移动:

        if self.player.rect.centerx >= WIDTH/2:
            # Ensuring the player stays in the middle of the screen
            self.player.pos.x -= abs(self.player.vel.x)
            for obj in self.movables:
                # Relocating Sprite when Player moves
                obj.rect.x -= abs(self.player.vel.x)
                # Killin Sprite when it is out of the screen
                if obj.rect.x < 0 - obj.rect.width:
                    obj.kill()
                # Reseting Location of the Ground
                if self.ground.rect.centerx <= 0:
                    self.ground.rect.x = 0
如果self.player.rect.centerx>=宽度/2:
确保玩家保持在屏幕中央
self.player.pos.x-=abs(self.player.vel.x)
对于自动产中的obj:
#玩家移动时重新定位精灵
object.rect.x-=abs(self.player.vel.x)
#当精灵离开屏幕时杀死它
如果obj.rect.x<0-obj.rect.WITH:
对象kill()
#地面的重置位置

如果self.ground.rect.centerx我添加了一个问题的代码示例。但是在这段代码中,播放器在几秒钟后消失了,我想这是我处理与平台碰撞的方式造成的,但现在我也不知道如何解决这个问题:D

要清楚地看到移动精灵的问题:继续按“d”,当平台离开屏幕一半时,不要按“d”键,您可以看到精灵相对于屏幕上其他精灵的移动速度慢

            import pygame
            WIDTH = 800
            HEIGHT = 600
            BGCOLOR = (181, 45, 76)
            BLACK = (0, 0, 0)
            # Player Properties
            PLAYER_ACC = 0.5
            PLAYER_FRICTION = -0.12
            PLAYER_GRAV = 0.5
            PLAYER_JUMP = -20


            class Platform(pygame.sprite.Sprite):

                def __init__(self, game, x, y, w, h):
                    self._layer = 1
                    self.groups = game.all_sprites, game.movables, game.platforms
                    pygame.sprite.Sprite.__init__(self, self.groups)
                    self.game = game
                    self.image = pygame.Surface((w, h))
                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

            class Ground(pygame.sprite.Sprite):

                def __init__(self, game, x, y, w, h):
                    self._layer = 1
                    self.groups = game.all_sprites, game.movables, game.platforms
                    pygame.sprite.Sprite.__init__(self, self.groups)
                    self.game = game
                    self.image = pygame.Surface((w, h))
                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

            vec = pygame.math.Vector2

            class Player(pygame.sprite.Sprite):

                def __init__(self, game):
                self._layer = 2
                self.groups = game.all_sprites
                pygame.sprite.Sprite.__init__(self, self.groups)
                self.game = game
                self.image = pygame.Surface((30, 40))
                self.rect = self.image.get_rect()
                self.rect.center = (WIDTH/2, HEIGHT/2)
                self.pos = (WIDTH/2, HEIGHT/2)

                self.vel = vec(0, 0)
                self.acc = vec(0, 0)
                self.jumping = False

                def event(self):
                keys = pygame.key.get_pressed()
                if keys[pygame.K_LEFT] or keys[pygame.K_a]:
                    self.acc.x = -PLAYER_ACC
                if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
                    self.acc.x = PLAYER_ACC


                def update(self):
                self.acc = vec(0, PLAYER_GRAV)
                self.event()
                self.acc.x += self.vel.x * PLAYER_FRICTION
                self.vel += self.acc
                # Die Vel auf null setzen wenn sie ~0 ist
                if abs(self.vel.x) < 0.1:
                    self.vel.x = 0
                # Physik
                self.pos += self.vel + 0.5 * self.acc
                self.rect.midbottom = self.pos

                def jump(self):
                # Checking if the Player can Jump
                self.rect.y += 1
                hits = pygame.sprite.spritecollide(self.game.player, self.game.platforms, False)
                self.rect.y -= 1
                if hits:
                    if not self.jumping:
                    self.jumping = True
                    # Dieser Teil des Codes wird nie erreicht!!!
                    self.vel.y = PLAYER_JUMP

            class Game:
                def __init__(self):
                self.running = True
                pygame.init()
                self.clock = pygame.time.Clock()
                self.screen = pygame.display.set_mode((800, 600))
                pygame.display.set_caption("Moving Bug")

                def new(self):
                self.all_sprites = pygame.sprite.LayeredUpdates()
                self.movables = pygame.sprite.Group()
                self.platforms = pygame.sprite.Group()
                self.ground = Ground(self, 0, HEIGHT-20, WIDTH*2, 20)
                self.plat_1 = Platform(self, 400, 300, 100, 20)
                self.plat_2 = Platform(self, 800, 400, 100, 20)
                self.plat_3 = Platform(self, 100, 40, 100, 20)
                self.player = Player(self)
                self.run()

                def run(self):
                self.playing = True
                while self.playing:
                    self.clock.tick(60)
                    self.event()
                    self.update()
                    self.draw()

                def event(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                    if self.playing:
                        self.playing = False
                    self.running = False
                    if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        self.player.jump()

                def update(self):
                self.all_sprites.update()
                # Only checking for collision if the player is jumping
                if self.player.vel.y > 0:
                    hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
                    if hits:
                    self.player.vel.y = 0
                    self.player.rect.bottom = hits[0].rect.top
                    self.player.jumping = False
                # Left side of the screen
                if self.player.rect.x < 0:
                    self.player.vel.x = 0
                    self.player.rect.x = 0
                # Centering the player in the middle:
                if self.player.rect.centerx >= WIDTH/2:
                    # Ensuring the player stays in the middle of the screen
                    self.player.pos.x -= abs(self.player.vel.x)
                    for obj in self.movables:
                    # Relocating Sprite when Player moves
                    obj.rect.x -= abs(self.player.vel.x)
                    # Killin Sprite when it is out of the screen
                    if obj.rect.x < 0 - obj.rect.width:
                        obj.rect.x = WIDTH
                    # Reseting Location of the Ground
                    if self.ground.rect.centerx <= 0:
                        self.ground.rect.x = 0

                def draw(self):
                self.screen.fill(BGCOLOR)
                self.all_sprites.draw(self.screen)
                pygame.display.flip()

            g = Game()
            while g.running:
                g.new()
                g.run()
            pygame.quit()
导入pygame
宽度=800
高度=600
BGCOLOR=(181,45,76)
黑色=(0,0,0)
#玩家属性
玩家_ACC=0.5
游戏者摩擦=-0.12
玩家_GRAV=0.5
运动员跳跃=-20
类平台(pygame.sprite.sprite):
定义初始(自我,游戏,x,y,w,h):
自身层=1
self.groups=game.all_精灵、game.movables、game.platforms
pygame.sprite.sprite.\uuuu init\uuuu(self,self.groups)
self.game=游戏
self.image=pygame.Surface((w,h))
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
班级场地(pygame.sprite.sprite):
定义初始(自我,游戏,x,y,w,h):
自身层=1
self.groups=game.all_精灵、game.movables、game.platforms
pygame.sprite.sprite.\uuuu init\uuuu(self,self.groups)
self.game=游戏
self.image=pygame.Surface((w,h))
self.rect=self.image.get_rect()
self.rect.x=x
自校正y=y
vec=pygame.math.Vector2
职业玩家(pygame.sprite.sprite):
定义初始(自我,游戏):
自我保护层=2
self.groups=game.all\u精灵
pygame.sprite.sprite.\uuuu init\uuuu(self,self.groups)
self.game=游戏
self.image=pygame.Surface((30,40))
self.rect=self.image.get_rect()
self.rect.center=(宽度/2,高度/2)
self.pos=(宽度/2,高度/2)
self.vel=vec(0,0)
self.acc=vec(0,0)
自我跳跃=错误
def事件(自):
keys=pygame.key.get_pressed()
如果键[pygame.K_LEFT]或键[pygame.K_a]:
self.acc.x=-PLAYER\u acc
如果键[pygame.K_RIGHT]或键[pygame.K_d]:
self.acc.x=玩家
def更新(自我):
self.acc=vec(0,玩家重力)
self.event()
自加速x+=自加速x*玩家摩擦
self.vel+=self.acc
#级别为空,设置为0
如果abs(自身等级x)<0.1:
self.vel.x=0
#Physik
self.pos+=self.vel+0.5*self.acc
self.rect.midbottom=self.pos
def跳转(自):
#检查玩家是否能跳
自校正y+=1
hits=pygame.sprite.spritecollide(self.game.player,self.game.platforms,False)
自校正y-=1
如果点击:
如果不是自我跳跃:
self.jumping=True
#请告诉我你的密码!!!
self.vel.y=玩家跳
班级游戏:
定义初始化(自):
self.running=True
pygame.init()
self.clock=pygame.time.clock()
self.screen=pygame.display.set_模式((800600))
pygame.display.set_标题(“移动Bug”)
def新(自我):
self.all_sprites=pygame.sprite.LayeredUpdates()
self.movables=pygame.sprite.Group()
self.platforms=pygame.sprite.Group()
self.ground=ground(self,0,高-20,宽*2,20)
self.plat_1=平台(self,40030010020)
self.plat_2=平台(self,80040010020)
self.plat_3=平台(self,100,40,100,20)
self.player=player(self)
self.run()
def运行(自):
自弹=真
自娱自乐时:
自我时钟滴答(60)
self.event()
self.update()
self.draw()
def事件(自):
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
如果自己玩:
自我表现=错误
self.running=False
如果event.type==pygame.KEYDOWN:
if event.key==
            import pygame
            WIDTH = 800
            HEIGHT = 600
            BGCOLOR = (181, 45, 76)
            BLACK = (0, 0, 0)
            # Player Properties
            PLAYER_ACC = 0.5
            PLAYER_FRICTION = -0.12
            PLAYER_GRAV = 0.5
            PLAYER_JUMP = -20


            class Platform(pygame.sprite.Sprite):

                def __init__(self, game, x, y, w, h):
                    self._layer = 1
                    self.groups = game.all_sprites, game.movables, game.platforms
                    pygame.sprite.Sprite.__init__(self, self.groups)
                    self.game = game
                    self.image = pygame.Surface((w, h))
                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

            class Ground(pygame.sprite.Sprite):

                def __init__(self, game, x, y, w, h):
                    self._layer = 1
                    self.groups = game.all_sprites, game.movables, game.platforms
                    pygame.sprite.Sprite.__init__(self, self.groups)
                    self.game = game
                    self.image = pygame.Surface((w, h))
                    self.rect = self.image.get_rect()
                    self.rect.x = x
                    self.rect.y = y

            vec = pygame.math.Vector2

            class Player(pygame.sprite.Sprite):

                def __init__(self, game):
                self._layer = 2
                self.groups = game.all_sprites
                pygame.sprite.Sprite.__init__(self, self.groups)
                self.game = game
                self.image = pygame.Surface((30, 40))
                self.rect = self.image.get_rect()
                self.rect.center = (WIDTH/2, HEIGHT/2)
                self.pos = (WIDTH/2, HEIGHT/2)

                self.vel = vec(0, 0)
                self.acc = vec(0, 0)
                self.jumping = False

                def event(self):
                keys = pygame.key.get_pressed()
                if keys[pygame.K_LEFT] or keys[pygame.K_a]:
                    self.acc.x = -PLAYER_ACC
                if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
                    self.acc.x = PLAYER_ACC


                def update(self):
                self.acc = vec(0, PLAYER_GRAV)
                self.event()
                self.acc.x += self.vel.x * PLAYER_FRICTION
                self.vel += self.acc
                # Die Vel auf null setzen wenn sie ~0 ist
                if abs(self.vel.x) < 0.1:
                    self.vel.x = 0
                # Physik
                self.pos += self.vel + 0.5 * self.acc
                self.rect.midbottom = self.pos

                def jump(self):
                # Checking if the Player can Jump
                self.rect.y += 1
                hits = pygame.sprite.spritecollide(self.game.player, self.game.platforms, False)
                self.rect.y -= 1
                if hits:
                    if not self.jumping:
                    self.jumping = True
                    # Dieser Teil des Codes wird nie erreicht!!!
                    self.vel.y = PLAYER_JUMP

            class Game:
                def __init__(self):
                self.running = True
                pygame.init()
                self.clock = pygame.time.Clock()
                self.screen = pygame.display.set_mode((800, 600))
                pygame.display.set_caption("Moving Bug")

                def new(self):
                self.all_sprites = pygame.sprite.LayeredUpdates()
                self.movables = pygame.sprite.Group()
                self.platforms = pygame.sprite.Group()
                self.ground = Ground(self, 0, HEIGHT-20, WIDTH*2, 20)
                self.plat_1 = Platform(self, 400, 300, 100, 20)
                self.plat_2 = Platform(self, 800, 400, 100, 20)
                self.plat_3 = Platform(self, 100, 40, 100, 20)
                self.player = Player(self)
                self.run()

                def run(self):
                self.playing = True
                while self.playing:
                    self.clock.tick(60)
                    self.event()
                    self.update()
                    self.draw()

                def event(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                    if self.playing:
                        self.playing = False
                    self.running = False
                    if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        self.player.jump()

                def update(self):
                self.all_sprites.update()
                # Only checking for collision if the player is jumping
                if self.player.vel.y > 0:
                    hits = pygame.sprite.spritecollide(self.player, self.platforms, False)
                    if hits:
                    self.player.vel.y = 0
                    self.player.rect.bottom = hits[0].rect.top
                    self.player.jumping = False
                # Left side of the screen
                if self.player.rect.x < 0:
                    self.player.vel.x = 0
                    self.player.rect.x = 0
                # Centering the player in the middle:
                if self.player.rect.centerx >= WIDTH/2:
                    # Ensuring the player stays in the middle of the screen
                    self.player.pos.x -= abs(self.player.vel.x)
                    for obj in self.movables:
                    # Relocating Sprite when Player moves
                    obj.rect.x -= abs(self.player.vel.x)
                    # Killin Sprite when it is out of the screen
                    if obj.rect.x < 0 - obj.rect.width:
                        obj.rect.x = WIDTH
                    # Reseting Location of the Ground
                    if self.ground.rect.centerx <= 0:
                        self.ground.rect.x = 0

                def draw(self):
                self.screen.fill(BGCOLOR)
                self.all_sprites.draw(self.screen)
                pygame.display.flip()

            g = Game()
            while g.running:
                g.new()
                g.run()
            pygame.quit()