Skip to content

Instantly share code, notes, and snippets.

@bilhox
Created June 22, 2024 14:41
Show Gist options
  • Save bilhox/ce024b994e536338a750a760024b5939 to your computer and use it in GitHub Desktop.
Save bilhox/ce024b994e536338a750a760024b5939 to your computer and use it in GitHub Desktop.
Consider using an image that fits the window size perfectly.
import pygame
pygame.init()
window_size = pygame.Vector2(1920, 1080) * 2/3
screen = pygame.display.set_mode(window_size)
surf = pygame.image.load("image_path").convert_alpha()
surf = pygame.transform.scale_by(surf, 2/3)
running = True
clock = pygame.Clock()
while running:
dt = clock.tick() / 1000
screen.fill("black")
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] or keys[pygame.K_LEFT] or keys[pygame.K_UP] or keys[pygame.K_DOWN]:
dx, dy = 0, 0
if keys[pygame.K_RIGHT]:
dx += 2
elif keys[pygame.K_LEFT]:
dx -= 2
if keys[pygame.K_DOWN]:
dy += 2
elif keys[pygame.K_UP]:
dy -= 2
"""
w, h = surf.width, surf.height
a = surf.subsurface(pygame.Rect(0, 0, (w - dx) % w, h)).copy()
b = surf.subsurface(pygame.Rect((w - dx) % w, 0, dx % w, h)).copy()
surf.blit(a, (dx % w, 0))
surf.blit(b, (0, 0))
c = surf.subsurface(pygame.Rect(0, 0, w, (h - dy) % h)).copy()
d = surf.subsurface(pygame.Rect(0, (h - dy) % h, w, dy % h)).copy()
surf.blit(c, (0, dy % h))
surf.blit(d, (0, 0))
Into this :
"""
surf.scroll(dx, dy, pygame.SCROLL_REPEAT)
screen.blit(surf, (0, 0))
pygame.display.flip()
pygame.display.set_caption(f"FPS : {round(clock.get_fps(), 2)}")
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
running = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment