2026-11-11 16:46:48 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-08-28 16:46:44 +00:00
|
|
|
import pygame
|
2026-11-11 16:46:48 +00:00
|
|
|
from player import Player
|
2026-08-28 16:46:44 +00:00
|
|
|
|
|
|
|
|
pygame.init()
|
|
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
WINDOW_WIDTH = 640
|
|
|
|
|
WINDOW_HEIGHT = 580
|
|
|
|
|
CENTER_X = WINDOW_WIDTH / 2
|
|
|
|
|
CENTER_Y = WINDOW_HEIGHT / 2
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
|
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
speed = 200
|
|
|
|
|
radius = 50
|
2026-09-04 16:34:35 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
player_pos = pygame.Vector2(CENTER_X - 50, CENTER_Y - 50)
|
|
|
|
|
player = Player("white", pygame.image.load(Path("./testen/player.png")), player_pos)
|
|
|
|
|
obstacles = [pygame.Rect((CENTER_X + 100, CENTER_Y - 100), (200, 200))]
|
2026-09-04 16:34:35 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
running = True
|
2026-09-04 16:34:35 +00:00
|
|
|
|
2026-08-28 16:46:44 +00:00
|
|
|
while running:
|
2026-11-11 16:46:48 +00:00
|
|
|
dt = clock.tick(60) / 1000.0
|
2026-08-28 16:46:44 +00:00
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
|
running = False
|
2026-11-11 16:46:48 +00:00
|
|
|
player.update(obstacles)
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
movement = dt * speed
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
movement_x = 0
|
|
|
|
|
movement_y = 0
|
2026-08-28 16:46:44 +00:00
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
2026-11-11 16:46:48 +00:00
|
|
|
if keys[pygame.K_w]:
|
|
|
|
|
movement_y -= movement
|
|
|
|
|
if keys[pygame.K_s]:
|
|
|
|
|
movement_y += movement
|
|
|
|
|
if keys[pygame.K_a]:
|
|
|
|
|
movement_x -= movement
|
|
|
|
|
if keys[pygame.K_d]:
|
|
|
|
|
movement_x += movement
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
steps = int(movement) + 1
|
|
|
|
|
step_x = movement_x / steps
|
|
|
|
|
step_y = movement_y / steps
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
for step in range(steps):
|
|
|
|
|
previous_x = player.pos.x
|
|
|
|
|
player.pos.x += step_x
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
if player.pos.x - player.rect.width/2 < 0:
|
|
|
|
|
player.pos.x = player.rect.width/2
|
|
|
|
|
if player.pos.x + player.rect.width/2 > WINDOW_WIDTH:
|
|
|
|
|
player.pos.x = WINDOW_WIDTH - player.rect.width/2
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
if player.overlaps_rect(obstacle):
|
|
|
|
|
player.pos.x = previous_x
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
previous_y = player.pos.y
|
|
|
|
|
player.pos.y += step_y
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
if player.pos.y - player.rect.height/2 < 0:
|
|
|
|
|
player.pos.y = player.rect.height/2
|
|
|
|
|
if player.pos.y + player.rect.height/2 > WINDOW_HEIGHT:
|
|
|
|
|
player.pos.y = WINDOW_HEIGHT - player.rect.height/2
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
if player.overlaps_rect(obstacle):
|
|
|
|
|
player.pos.y = previous_y
|
2026-08-28 16:46:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
window.fill(player.color)
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
pygame.draw.rect(window, "grey", obstacle)
|
|
|
|
|
player.render(window)
|
2026-08-28 16:46:44 +00:00
|
|
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
pygame.quit()
|
|
|
|
|
|
2026-09-04 16:34:35 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
"""
|
|
|
|
|
Hausaufgabe: Pixelgenaue Kollision zwischen dem Spielerbild und der Wand ohne ruckeln, geschmeidig + MASKE D:
|
2026-08-28 16:46:44 +00:00
|
|
|
|
2026-11-11 16:46:48 +00:00
|
|
|
'Die Hausaufgabe ist, eine pixelgenaue Kollision zwischen dem Spielerbild und der Wand hinzubekommen, ohne zu ruckeln,
|
|
|
|
|
sodass du dich geschmeidig entlang der Wand bewegen kannst und eine Maske verwendest.'
|
|
|
|
|
|
|
|
|
|
"""
|