importovať pygame
import sys
ČIERNA =(0, 0, 0)
BIELA =(255, 255, 255)
ZELENÁ =(0, 255, 0)
ČERVENÁ =(255, 0, 0)
SCREEN_WIDTH =800
SCREEN_HEIGHT =600
obrazovka =pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
hráč =pygame.sprite.Sprite()
player.image =pygame.image.load("spongebob.png")
player.rect =player.image.get_rect()
steny =pygame.sprite.Group()
pre i v rozsahu (5):
stena =pygame.sprite.Sprite()
wall.image =pygame.Surface((100, 100))
wall.image.fill(ZELENÁ)
wall.rect =wall.image.get_rect()
stena.obdĺžnik.x =i * 100
stena.priam.y =i * 100
steny.pridať (stena)
kým pravda:
# Skontrolujte udalosti
pre udalosť v pygame.event.get():
if event.type ==pygame.QUIT:
pygame.quit()
sys.exit()
# Presuňte prehrávač
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
# Skontrolujte kolízie
if pygame.sprite.spritecollideany(hráč, steny):
player.rect.x -=5
player.rect.y -=5
# Nakreslite obrazovku
screen.fill (ČIERNA)
screen.blit(player.image, player.rect)
steny.kresliť (obrazovka)
# Aktualizujte obrazovku
pygame.display.flip()
```