104 lines
No EOL
2 KiB
Python
104 lines
No EOL
2 KiB
Python
import pygame
|
|
|
|
# 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
|
|
|
|
|
|
velocity = [0, 0]
|
|
|
|
walls = [
|
|
pygame.Rect(640, 260, 600, 30)
|
|
]
|
|
|
|
player_rect = pygame.Rect(player_pos.x, player_pos.y, 40, 40)
|
|
|
|
player_rect.center = player_pos
|
|
|
|
while running:
|
|
|
|
dt = clock.tick(60) / 1000
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
# fill the screen with a color to wipe away anything from last frame
|
|
screen.fill("purple")
|
|
|
|
for wall in walls:
|
|
pygame.draw.rect(screen, "red", wall)
|
|
|
|
pygame.draw.rect(screen, "blue", player_rect)
|
|
|
|
pygame.draw.line(screen, "yellow", (0, 0), player_pos)
|
|
|
|
|
|
velocity = [0, 0]
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
|
|
direction = pygame.Vector2(
|
|
keys[pygame.K_d] - keys[pygame.K_a],
|
|
keys[pygame.K_s] - keys[pygame.K_w]
|
|
)
|
|
|
|
movement = direction * speed * dt
|
|
|
|
player_pos += movement
|
|
|
|
# player_rect.center = round(player_pos)
|
|
player_rect.centerx = round(player_pos.x)
|
|
|
|
for wall in walls:
|
|
if player_rect.colliderect(wall):
|
|
if movement.x > 0:
|
|
player_rect.right = wall.left
|
|
|
|
elif movement.x < 0:
|
|
player_rect.left = wall.right
|
|
|
|
player_pos.x = player_rect.centerx
|
|
|
|
player_rect.centery = round(player_pos.y)
|
|
|
|
for wall in walls:
|
|
if player_rect.colliderect(wall):
|
|
if movement.y > 0:
|
|
player_rect.bottom = wall.top
|
|
|
|
elif movement.y < 0:
|
|
player_rect.top = wall.bottom
|
|
|
|
player_pos.y = player_rect.centery
|
|
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
pygame.quit() |