120 lines
2.5 KiB
Python
120 lines
2.5 KiB
Python
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
import pygame
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class Collisions:
|
||
|
|
top: bool = False
|
||
|
|
right: bool = False
|
||
|
|
bottom: bool = False
|
||
|
|
left: bool = False
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# pygame setup
|
||
|
|
pygame.init()
|
||
|
|
screen = pygame.display.set_mode((1280, 720))
|
||
|
|
clock = pygame.time.Clock()
|
||
|
|
running = True
|
||
|
|
dt = 0
|
||
|
|
|
||
|
|
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)
|
||
|
|
|
||
|
|
speed = 300
|
||
|
|
|
||
|
|
movement = [False, False, False, False]
|
||
|
|
|
||
|
|
velocity = [0, 0]
|
||
|
|
|
||
|
|
while running:
|
||
|
|
# poll for events
|
||
|
|
# pygame.QUIT event means the user clicked X to close your window
|
||
|
|
|
||
|
|
for event in pygame.event.get():
|
||
|
|
if event.type == pygame.QUIT:
|
||
|
|
running = False
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
wall_rect = pygame.Rect(640, 260, 600, 30)
|
||
|
|
player_rect = pygame.Rect(player_pos.x, player_pos.y, 40, 40)
|
||
|
|
|
||
|
|
# fill the screen with a color to wipe away anything from last frame
|
||
|
|
screen.fill("purple")
|
||
|
|
pygame.draw.rect(screen, "blue", player_rect)
|
||
|
|
pygame.draw.rect(screen, "red", wall_rect)
|
||
|
|
|
||
|
|
velocity = speed * dt
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
keys = pygame.key.get_pressed()
|
||
|
|
|
||
|
|
if keys[pygame.K_w]:
|
||
|
|
velocity -= speed * dt
|
||
|
|
movement[0] = True
|
||
|
|
|
||
|
|
if keys[pygame.K_s]:
|
||
|
|
velocity += speed * dt
|
||
|
|
movement[2] = True
|
||
|
|
|
||
|
|
if keys[pygame.K_a]:
|
||
|
|
velocity -= speed * dt
|
||
|
|
movement[3] = True
|
||
|
|
|
||
|
|
if keys[pygame.K_d]:
|
||
|
|
velocity += speed * dt
|
||
|
|
movement[1] = True
|
||
|
|
|
||
|
|
collisions = Collisions()
|
||
|
|
|
||
|
|
frame_movement: tuple[float, float] = (
|
||
|
|
(movement[0] + velocity[0]),
|
||
|
|
movement[1] + velocity[1],
|
||
|
|
)
|
||
|
|
|
||
|
|
entity_rect = player_rect
|
||
|
|
if entity_rect.colliderect(wall_rect):
|
||
|
|
if frame_movement[0] > 0:
|
||
|
|
entity_rect.right = wall_rect.left
|
||
|
|
collisions.right = True
|
||
|
|
|
||
|
|
if frame_movement[0] < 0:
|
||
|
|
entity_rect.left = wall_rect.right
|
||
|
|
collisions.left = True
|
||
|
|
|
||
|
|
player_pos.x = entity_rect.x
|
||
|
|
|
||
|
|
player_pos[1] += frame_movement[1]
|
||
|
|
|
||
|
|
entity_rect = player_rect
|
||
|
|
|
||
|
|
|
||
|
|
if entity_rect.colliderect(wall_rect):
|
||
|
|
if frame_movement[1] > 0:
|
||
|
|
entity_rect.bottom = wall_rect.top
|
||
|
|
collisions.bottom = True
|
||
|
|
|
||
|
|
if frame_movement[1] < 0:
|
||
|
|
entity_rect.top = wall_rect.bottom
|
||
|
|
collisions.top = True
|
||
|
|
|
||
|
|
player_pos[1] = entity_rect.y
|
||
|
|
|
||
|
|
movement = [False, False, False, False] # NORDEN OSTEN SÜDEN WESTEN
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# flip() the display to put your work on screen
|
||
|
|
pygame.display.flip()
|
||
|
|
|
||
|
|
# limits FPS to 60
|
||
|
|
# dt is delta time in seconds since last frame, used for framerate-
|
||
|
|
# independent physics.
|
||
|
|
dt = clock.tick(60) / 1000
|
||
|
|
|
||
|
|
pygame.quit()
|