|
|
|
@ -11,6 +11,10 @@ class Player:
|
|
|
|
pygame.image.load(settings.player.fred.olli).convert_alpha(),
|
|
|
|
pygame.image.load(settings.player.fred.olli).convert_alpha(),
|
|
|
|
settings.player.size
|
|
|
|
settings.player.size
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.image_left = pygame.transform.scale(
|
|
|
|
|
|
|
|
pygame.image.load(settings.player.fred.left).convert_alpha(),
|
|
|
|
|
|
|
|
settings.player.size
|
|
|
|
|
|
|
|
)
|
|
|
|
self.image = self.image_normal # Anfangszustand
|
|
|
|
self.image = self.image_normal # Anfangszustand
|
|
|
|
self.rect = self.image.get_rect(topleft=(x, y))
|
|
|
|
self.rect = self.image.get_rect(topleft=(x, y))
|
|
|
|
self.velocity_y = 0
|
|
|
|
self.velocity_y = 0
|
|
|
|
@ -19,25 +23,33 @@ class Player:
|
|
|
|
self.jump_strength = settings.player.jump_strength
|
|
|
|
self.jump_strength = settings.player.jump_strength
|
|
|
|
self.on_ground = False
|
|
|
|
self.on_ground = False
|
|
|
|
|
|
|
|
|
|
|
|
def update(self, ground_y):
|
|
|
|
def update(self, ground_y, obstacles=[]):
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
was_on_ground = self.on_ground
|
|
|
|
|
|
|
|
|
|
|
|
# Bewegung seitlich
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
self.rect.x -= self.speed
|
|
|
|
self.rect.x -= self.speed
|
|
|
|
if keys[pygame.K_RIGHT]:
|
|
|
|
if keys[pygame.K_RIGHT]:
|
|
|
|
self.rect.x += self.speed
|
|
|
|
self.rect.x += self.speed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Sprung
|
|
|
|
if keys[pygame.K_SPACE] and self.on_ground:
|
|
|
|
if keys[pygame.K_SPACE] and self.on_ground:
|
|
|
|
self.velocity_y = self.jump_strength
|
|
|
|
self.velocity_y = self.jump_strength
|
|
|
|
self.on_ground = False
|
|
|
|
self.on_ground = False
|
|
|
|
if self.on_ground and self.image != self.image_normal:
|
|
|
|
|
|
|
|
self.image = self.image_normal
|
|
|
|
|
|
|
|
elif not self.on_ground and self.image != self.image_jump:
|
|
|
|
|
|
|
|
self.image = self.image_jump
|
|
|
|
|
|
|
|
if not self.on_ground and was_on_ground and settings.DEBUG:
|
|
|
|
|
|
|
|
print("Fred springt!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Gravitation
|
|
|
|
self.velocity_y += self.gravity
|
|
|
|
self.velocity_y += self.gravity
|
|
|
|
self.rect.y += self.velocity_y
|
|
|
|
self.rect.y += self.velocity_y
|
|
|
|
|
|
|
|
self.on_ground = False # zurücksetzen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Plattform-Kollision prüfen
|
|
|
|
|
|
|
|
for obs in obstacles:
|
|
|
|
|
|
|
|
if self.rect.colliderect(obs.get_rect()):
|
|
|
|
|
|
|
|
# nur wenn Spieler von oben kommt
|
|
|
|
|
|
|
|
if self.velocity_y > 0 and self.rect.bottom <= obs.get_rect().top + 20:
|
|
|
|
|
|
|
|
self.rect.bottom = obs.get_rect().top
|
|
|
|
|
|
|
|
self.velocity_y = 0
|
|
|
|
|
|
|
|
self.on_ground = True
|
|
|
|
|
|
|
|
|
|
|
|
# Boden-Kollision
|
|
|
|
# Boden-Kollision
|
|
|
|
if self.rect.bottom >= ground_y:
|
|
|
|
if self.rect.bottom >= ground_y:
|
|
|
|
@ -45,5 +57,15 @@ class Player:
|
|
|
|
self.velocity_y = 0
|
|
|
|
self.velocity_y = 0
|
|
|
|
self.on_ground = True
|
|
|
|
self.on_ground = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Bildlogik
|
|
|
|
|
|
|
|
if not self.on_ground:
|
|
|
|
|
|
|
|
self.image = self.image_jump
|
|
|
|
|
|
|
|
elif keys[pygame.K_LEFT]:
|
|
|
|
|
|
|
|
self.image = self.image_left
|
|
|
|
|
|
|
|
elif keys[pygame.K_RIGHT]:
|
|
|
|
|
|
|
|
self.image = self.image_normal
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.image = self.image_normal
|
|
|
|
|
|
|
|
|
|
|
|
def draw(self, screen):
|
|
|
|
def draw(self, screen):
|
|
|
|
screen.blit(self.image, self.rect)
|
|
|
|
screen.blit(self.image, self.rect)
|