importere pygame
import sys
SVART =(0, 0, 0)
HVIT =(255, 255, 255)
GRØNN =(0, 255, 0)
RØD =(255, 0, 0)
SCREEN_WIDTH =800
SCREEN_HEIGHT =600
skjerm =pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
spiller =pygame.sprite.Sprite()
player.image =pygame.image.load("spongebob.png")
player.rect =player.image.get_rect()
vegger =pygame.sprite.Group()
for i i området(5):
vegg =pygame.sprite.Sprite()
wall.image =pygame.Surface((100, 100))
wall.image.fill(GRØNN)
wall.rect =wall.image.get_rect()
wall.rect.x =i * 100
wall.rect.y =i * 100
walls.add(wall)
mens sant:
# Se etter hendelser
for hendelse i pygame.event.get():
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
# Flytt spilleren
keys =pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.rect.x -=5
if keys[pygame.K_RIGHT]:
player.rect.x +=5
if keys[pygame.K_UP]:
player.rect.y -=5
if keys[pygame.K_DOWN]:
player.rect.y +=5
# Se etter kollisjoner
if pygame.sprite.spritecollideany(spiller, vegger):
player.rect.x -=5
player.rect.y -=5
# Tegn skjermen
screen.fill(SVART)
screen.blit(player.image, player.rect)
walls.draw(skjerm)
# Oppdater skjermen
pygame.display.flip()
```