Seite anzeigenÄltere VersionenLinks hierherNach oben Diese Seite ist nicht editierbar. Sie können den Quelltext sehen, jedoch nicht verändern. Kontaktieren Sie den Administrator, wenn Sie glauben, dass hier ein Fehler vorliegt. <sxh python> import pygame # Pygame initialisieren pygame.init() # Bildschirmgröße WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Plattform- und Spieler-Interaktion") # Farben WHITE = (255, 255, 255) BLUE = (0, 0, 255) GREEN = (0, 255, 0) # FPS und Clock FPS = 60 clock = pygame.time.Clock() # Spieler-Eigenschaften player = pygame.Rect(200, 200, 50, 50) player_color = BLUE player_speed = 5 gravity = 1 velocity_y = 0 on_ground = False # Plattform-Eigenschaften platform = pygame.Rect(200, 400, 400, 20) platform_color = GREEN # Spiel-Loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Spieler-Bewegung keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.x -= player_speed if keys[pygame.K_RIGHT]: player.x += player_speed if keys[pygame.K_SPACE] and on_ground: velocity_y = -15 # Spieler springt # Schwerkraft anwenden velocity_y += gravity player.y += velocity_y # Plattform-Kollision if player.colliderect(platform) and velocity_y >= 0: player.y = platform.y - player.height velocity_y = 0 on_ground = True else: on_ground = False # Bildschirm füllen und Objekte zeichnen screen.fill(WHITE) pygame.draw.rect(screen, platform_color, platform) pygame.draw.rect(screen, player_color, player) # Bildschirm aktualisieren pygame.display.flip() clock.tick(FPS) pygame.quit() </sxh> ef/pygame/example3.txt Zuletzt geändert: 2025/01/21 14:14von lehmannr