私の問題のコード例を追加しました。しかし、このコードでは、プレイヤーは数秒後に消えるので、私はプラットフォームとの衝突を処理する方法でやらなければならないと思いますが、今のところこれを修正する方法もわかりません: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()
に、私が私のポストに編集した – PRMoureu
私たちはその部分が必要だと思う 'player.vel'に結びついているように見えます自分自身が画面から外れるのは、私がet go K_d。 – wjatscheslaw
の一部スプライトが遅く動いている奇妙な動きを、あまりにも – wjatscheslaw