Compare commits
2 Commits
996c9f61c6
...
b76966421e
| Author | SHA1 | Date |
|---|---|---|
|
|
b76966421e | 8 months ago |
|
|
b9770a4281 | 8 months ago |
|
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 1.4 MiB |
|
Before Width: | Height: | Size: 1.4 MiB After Width: | Height: | Size: 1.4 MiB |
@ -0,0 +1,49 @@
|
|||||||
|
import pygame
|
||||||
|
import settings
|
||||||
|
|
||||||
|
class Player:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.image_normal = pygame.transform.scale(
|
||||||
|
pygame.image.load(settings.player.fred.normal).convert_alpha(),
|
||||||
|
settings.player.size
|
||||||
|
)
|
||||||
|
self.image_jump = pygame.transform.scale(
|
||||||
|
pygame.image.load(settings.player.fred.olli).convert_alpha(),
|
||||||
|
settings.player.size
|
||||||
|
)
|
||||||
|
self.image = self.image_normal # Anfangszustand
|
||||||
|
self.rect = self.image.get_rect(topleft=(x, y))
|
||||||
|
self.velocity_y = 0
|
||||||
|
self.speed = 5
|
||||||
|
self.gravity = 1
|
||||||
|
self.jump_strength = -12
|
||||||
|
self.on_ground = False
|
||||||
|
|
||||||
|
def update(self, ground_y):
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
was_on_ground = self.on_ground
|
||||||
|
if keys[pygame.K_LEFT]:
|
||||||
|
self.rect.x -= self.speed
|
||||||
|
if keys[pygame.K_RIGHT]:
|
||||||
|
self.rect.x += self.speed
|
||||||
|
if keys[pygame.K_SPACE] and self.on_ground:
|
||||||
|
self.velocity_y = self.jump_strength
|
||||||
|
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!")
|
||||||
|
|
||||||
|
self.velocity_y += self.gravity
|
||||||
|
self.rect.y += self.velocity_y
|
||||||
|
|
||||||
|
# Boden-Kollision
|
||||||
|
if self.rect.bottom >= ground_y:
|
||||||
|
self.rect.bottom = ground_y
|
||||||
|
self.velocity_y = 0
|
||||||
|
self.on_ground = True
|
||||||
|
|
||||||
|
def draw(self, screen):
|
||||||
|
screen.blit(self.image, self.rect)
|
||||||
Loading…
Reference in new issue