Fertige Animationen
This commit is contained in:
parent
ca0c93b9d9
commit
d36c44a7b2
5 changed files with 74 additions and 19 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -218,3 +218,5 @@ __marimo__/
|
|||
# Data
|
||||
data/
|
||||
der_test.py
|
||||
spickzettel.md
|
||||
.vscode
|
||||
18
game.py
18
game.py
|
|
@ -1,9 +1,10 @@
|
|||
import pygame
|
||||
import sys
|
||||
from scripts.entities import *
|
||||
from scripts.entities import PhysicsEntity, Player
|
||||
from scripts.utils import *
|
||||
from scripts.tilemap import *
|
||||
from scripts.clouds import Clouds
|
||||
from scripts.animation import Animation
|
||||
|
||||
class Game:
|
||||
def __init__(self):
|
||||
|
|
@ -19,20 +20,27 @@ class Game:
|
|||
self.clock = pygame.time.Clock()
|
||||
self.running = True
|
||||
|
||||
self.player = PhysicsEntity(self, 'player', (50, 50), (8, 15))
|
||||
|
||||
self.collision_area = pygame.Rect(50, 50, 300, 50)
|
||||
self.movement = [False, False]
|
||||
|
||||
self.assets = {
|
||||
"player":load_image("entities/player.png"),
|
||||
# Andere Dinge
|
||||
"background":load_image("background.png"),
|
||||
"decor": load_images("tiles/decor"),
|
||||
"grass": load_images("tiles/grass"),
|
||||
"large_decor": load_images("tiles/large_decor"),
|
||||
"stone": load_images("tiles/stone"),
|
||||
"clouds": load_images("clouds")
|
||||
"clouds": load_images("clouds"),
|
||||
|
||||
# Spieler
|
||||
"player/idle":Animation(load_images("entities/player/idle"), image_duration=6),
|
||||
"player/run":Animation(load_images("entities/player/run"), image_duration=4),
|
||||
"player/jump":Animation(load_images("entities/player/jump")),
|
||||
"player/slide":Animation(load_images("entities/player/slide")),
|
||||
"player/wall_slide":Animation(load_images("entities/player/wall_slide")),
|
||||
}
|
||||
self.player = Player(self, (50, 50), (8, 15))
|
||||
|
||||
self.tilemap = Tilemap(self, 16)
|
||||
self.clouds = Clouds(self.assets['clouds'], count=16)
|
||||
self.isJumping = False
|
||||
|
|
|
|||
|
|
@ -62,7 +62,8 @@ class Animation:
|
|||
frame 4,5 -> images[2]
|
||||
"""
|
||||
# TODO: Implementieren
|
||||
pass
|
||||
""" Gibt das aktuelle Bild der Animation zurück. """
|
||||
return self.images[int(self.frame / self.image_duration)]
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import pygame
|
||||
class PhysicsEntity:
|
||||
|
||||
def __init__(self, game, e_type, pos:tuple, size):
|
||||
self.game = game
|
||||
self.e_type = e_type
|
||||
|
|
@ -9,6 +10,12 @@ class PhysicsEntity:
|
|||
self.collisions = {"top":False, "bottom":False, "left":False, "right":False}
|
||||
self.speed = 3
|
||||
|
||||
# Animationen
|
||||
self.action = ""
|
||||
self.flip = False
|
||||
self.animation_offset = (-3, -3)
|
||||
self.set_action('idle')
|
||||
|
||||
def update(self, tilemap, movement:tuple=(0,0)):
|
||||
self.collisions = {"top":False, "bottom":False, "left":False, "right":False}
|
||||
frame_movement = ((movement[0] + self.velocity[0])*self.speed, movement[1] + self.velocity[1])
|
||||
|
|
@ -48,14 +55,53 @@ class PhysicsEntity:
|
|||
|
||||
self.pos[1] = entity_rect.y
|
||||
|
||||
if movement[0] > 0:
|
||||
self.flip = False
|
||||
if movement[0] < 0:
|
||||
self.flip = True
|
||||
|
||||
if self.collisions['bottom'] or self.collisions['top']:
|
||||
self.velocity[1] = 0
|
||||
|
||||
|
||||
self.velocity[1] = min(5, self.velocity[1] + 0.1)
|
||||
|
||||
self.animation.update()
|
||||
def render(self, surface:pygame.Surface, offset:tuple=(0,0)):
|
||||
surface.blit(self.game.assets["player"], (self.pos[0]-offset[0], self.pos[1]-offset[1]))
|
||||
surface.blit(pygame.transform.flip(self.animation.img(), self.flip, False), (self.pos[0] - offset[0] + self.animation_offset[0],self.pos[1] - offset[1] + self.animation_offset[1]))
|
||||
|
||||
|
||||
def rect(self):
|
||||
return pygame.Rect(self.pos[0], self.pos[1], self.size[0], self.size[1])
|
||||
|
||||
def set_action(self, action):
|
||||
if action != self.action:
|
||||
self.action = action
|
||||
image = self.e_type + "/" + self.action
|
||||
self.animation = self.game.assets[image].copy()
|
||||
|
||||
|
||||
class Player(PhysicsEntity):
|
||||
def __init__(self, game, pos, size):
|
||||
super().__init__(game, "player", pos, size)
|
||||
self.air_time = 0 # Neu! Zählt, wie lange wir fallen/springe
|
||||
|
||||
def update(self, tilemap, movement = (0, 0)):
|
||||
super().update(tilemap, movement)
|
||||
|
||||
self.air_time += 1
|
||||
|
||||
if self.collisions["bottom"]:
|
||||
self.air_time = 0
|
||||
# Wenn wir länger als 4 Frames in der Luft sind -> Springen!
|
||||
if self.air_time > 4:
|
||||
self.set_action('jump')
|
||||
|
||||
# Wenn wir nicht in der Luft sind, aber uns bewegen -> Rennen!
|
||||
elif movement[0] != 0:
|
||||
self.set_action('run')
|
||||
|
||||
# Sonst -> Rumstehen!
|
||||
else:
|
||||
self.set_action('idle')
|
||||
|
||||
|
|
@ -2,14 +2,12 @@ import pygame
|
|||
import os
|
||||
BASE_IMG_PATH = 'data/images/'
|
||||
def load_image(path):
|
||||
image = pygame.image.load(BASE_IMG_PATH + path).convert()
|
||||
image.set_colorkey((0,0,0))
|
||||
return image
|
||||
img = pygame.image.load(BASE_IMG_PATH + path).convert()
|
||||
img.set_colorkey((0, 0, 0))
|
||||
return img
|
||||
|
||||
def load_images(path):
|
||||
images = []
|
||||
bilder = os.listdir(BASE_IMG_PATH + path)
|
||||
for image in bilder:
|
||||
images.append(load_image(path +"/" + image))
|
||||
|
||||
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
|
||||
images.append(load_image(path + '/' + img_name))
|
||||
return images
|
||||
|
||||
Loading…
Reference in a new issue