Pygame Ball Game

Hier werden alle anderen GUI-Toolkits sowie Spezial-Toolkits wie Spiele-Engines behandelt.
Antworten
DC3609
User
Beiträge: 1
Registriert: Montag 28. August 2023, 18:16

Moin,

hab das Forum hier gefunden und dachte ich frag mal ob mir hier wer mit meinen Problem weiter helfen kann.

Ich bin grade dabei mich mit Pyhton vertraut zu machen und habe deswegen einen kleinen Plattformer mit ein Gummiball geschrieben.

Mein Problem ist jetzt das ich beim versuch den bug in der kollisionserkennung zu fixen festhänge. Der Ball soll eigentlich von den Plattformen seitlich abprallen aber er bugt sich dann auf die Plattform oben Drauf.
Vl kann mir wer weiter helfen und mir sagen wiso das passiert.

MFG DC

Code: Alles auswählen

import pygame
import sys

# Initialisierung
pygame.init()

# Bildschirmgröße
screen_width = 1600
screen_height = 900

# Farben
white = (255, 255, 255)
blue = (0, 0, 255)

# Erstellen des Bildschirms
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Flubber")

# Ball Eigenschaften
ball_radius = 20
ball_color = (255, 0, 0)
ball_x = screen_width // 2
ball_y = screen_height - ball_radius * 2
ball_speed_y = 0
gravity = 0.5
bounce_factor = 0.7
jump_strength = -12
is_jumping = False

# Liste von Kartenbereichen
map_areas = [
    {
        "id": 0,
        "platforms": [
            pygame.Rect(300, screen_height - 100, 800, 100),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(500, screen_height - 300, 200, 10),
            pygame.Rect(100, screen_height - 400, 200, 10),
            pygame.Rect(500, screen_height - 500, 200, 10),
            pygame.Rect(800, screen_height - 400, 20, 300)
        ],
        "left": 1,  # Geändert auf 1
        "right": 2,  # Hinzugefügt den Wert 2
        "up": 8,
        "down": 0
    },
    {
        "id": 1,
        "platforms": [
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 200, 250, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 2,  # Hinzugefügt den Wert 2
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 2,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 400, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 3,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 600, 250, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 4,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 100, 150, 10),
            pygame.Rect(300, screen_height - 400, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 5,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 6,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 7,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 000, 800, 10),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 1,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 0
    },
    {
        "id": 8,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(50, screen_height - 100, 400, 10),
            pygame.Rect(250, screen_height - 400, 400, 10),
            pygame.Rect(150, screen_height - 500, 100, 10),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 0,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 9,
        "down": 0
    },
    {
        "id": 9,
        "platforms": [  # Beispielhafte Plattformen für Bereich mit ID 2
            pygame.Rect(300, screen_height - 100, 400, 10),
            pygame.Rect(250, screen_height - 400, 400, 10),
            pygame.Rect(150, screen_height - 500, 100, 10),
            pygame.Rect(300, screen_height - 200, 150, 10),
            pygame.Rect(600, screen_height - 300, 200, 10)
        ],
        "left": 0,  # Geändert auf 1
        "right": 0,  # Geändert auf 0
        "up": 0,
        "down": 8
    },
    # Weitere Bereiche...
]

current_area = 0  # Index des aktuellen Kartenbereichs
platforms = map_areas[current_area]["platforms"]  # Plattformen im aktuellen Bereich

clock = pygame.time.Clock()

# Doppel-Sprung-Eigenschaften
double_jump_available = False  # Startwert auf False gesetzt
double_jump_strength = -10  # Stärke des Doppel-Sprungs

# Flag, um zu überprüfen, ob der erste Sprung bereits gemacht wurde
first_jump = False

# Auto Jump Eigenschaften
auto_jump_timer = pygame.time.get_ticks()  # Timer für den Auto Jump
auto_jump_interval = 1000  # Zeitintervall in Millisekunden (1 Sekunde = 1000 Millisekunden)

# ...

# Auto Jump Eigenschaften
auto_jump_timer = pygame.time.get_ticks()  # Timer für den Auto Jump
auto_jump_interval = 500  # Zeitintervall in Millisekunden (500 Millisekunden = 0,5 Sekunden)

# Spiel-Schleife
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    keys = pygame.key.get_pressed()

    # Horizontale Bewegung (nur für den Ball)
    if keys[pygame.K_LEFT]:
        ball_x -= 5
        if ball_x < 0:
            current_area = map_areas[current_area]["left"]
            ball_x = screen_width - ball_radius * 2
            platforms = map_areas[current_area]["platforms"]
    if keys[pygame.K_RIGHT]:
        ball_x += 5
        if ball_x > screen_width - ball_radius * 2:
            current_area = map_areas[current_area]["right"]
            ball_x = 0
            platforms = map_areas[current_area]["platforms"]

    # Vertikale Bewegung (nur für den Ball)
    ball_speed_y += gravity
    ball_y += ball_speed_y

    # Kollision mit Plattformen im aktuellen Bereich
    on_platform = False
    for platform in platforms:
        if platform.colliderect(pygame.Rect(ball_x, ball_y, ball_radius * 2, ball_radius * 2)):
            if ball_speed_y > 0:  # Kollision von oben
                ball_y = platform.top - ball_radius * 2
                ball_speed_y = 0
                on_platform = True
                is_jumping = False
                double_jump_available = True
            elif ball_speed_y < 0:  # Kollision von unten
                ball_y = platform.bottom + ball_radius
                ball_speed_y = 0

            if ball_x + ball_radius < platform.left:  # Kollision von rechts
                ball_x = platform.left - ball_radius * 2
            elif ball_x - ball_radius > platform.right:  # Kollision von links
                ball_x = platform.right

            if ball_y + ball_radius * 2 < platform.top:  # Kollision von unten
                ball_y = platform.top - ball_radius * 2
                ball_speed_y = 0
            elif ball_y > platform.bottom:  # Kollision von oben
                ball_y = platform.bottom + ball_radius
                ball_speed_y = 0

    # Kollision mit den Wänden (links und rechts)
    if ball_x < 0:
        ball_x = 0
        ball_speed_x *= -bounce_factor  # Richtung umkehren und Geschwindigkeit dämpfen
    elif ball_x > screen_width - ball_radius * 2:
        ball_x = screen_width - ball_radius * 2
        ball_speed_x *= -bounce_factor  # Richtung umkehren und Geschwindigkeit dämpfen

    if not on_platform:  # Wenn der Ball nicht auf einer Plattform ist
        if ball_y > screen_height:
            next_area = map_areas[current_area]["down"]
            if next_area != -1:
                current_area = next_area
                ball_y = 0
                platforms = map_areas[current_area]["platforms"]
                double_jump_available = True
            else:
                ball_y = screen_height - ball_radius * 2
                ball_speed_y *= -bounce_factor  # Abpralleffekt am Boden
        elif ball_y < 0:
            current_area = map_areas[current_area]["up"]
            ball_y = screen_height - ball_radius * 2
            platforms = map_areas[current_area]["platforms"]

    # Springen
    if keys[pygame.K_SPACE] and not is_jumping:
        ball_speed_y = jump_strength
        is_jumping = True
        if not first_jump:
            double_jump_available = True
            first_jump = True
    else:
        first_jump = False

    # Doppel-Sprung
    if keys[pygame.K_SPACE] and (is_jumping or not on_platform) and double_jump_available:
        ball_speed_y = double_jump_strength
        double_jump_available = False

    # Auto Jump
    current_time = pygame.time.get_ticks()
    if current_time - auto_jump_timer >= auto_jump_interval and not is_jumping:
        auto_jump_timer = current_time
        ball_speed_y = jump_strength
        is_jumping = True

    # Zeichnen
    screen.fill(white)
    for platform in platforms:
        pygame.draw.rect(screen, blue, platform)
    pygame.draw.circle(screen, ball_color, (ball_x + ball_radius, int(ball_y + ball_radius)), ball_radius)
    pygame.display.flip()

    clock.tick(60)
Antworten