Advertisement
niromru

Перемещение персонажа

Feb 12th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. import urllib.request
  2. import pygame
  3. from time import sleep
  4.  
  5.  
  6. print('Введите имя файла уровня:')
  7. lev = input()
  8. f = open(lev, 'r')
  9. jh, ih = 3, 3
  10. initial = 1
  11. vis = 0
  12. pole = []
  13. for i in f:
  14.     t = i.split()
  15.     vis += 1
  16.     shir = len(t)
  17.     pole.append(t)
  18. f.close()
  19. f, h = urllib.request.urlretrieve('https://yastatic.net/s3/lyceum/content/image/pygame/mar.png')
  20. img_pers = open(f)
  21. f, h = urllib.request.urlretrieve('https://yastatic.net/s3/lyceum/content/image/pygame/box.png')
  22. img_korobka = open(f)
  23. f, h = urllib.request.urlretrieve('https://yastatic.net/s3/lyceum/content/image/pygame/grass.png')
  24. img_trava = open(f)
  25. f, h = urllib.request.urlretrieve('https://i.ibb.co/c6bDyqC/fon.png')
  26. img_zastavka = open(f)
  27. if __name__ == '__main__':
  28.     pygame.init()
  29.     pygame.display.set_caption('Перемещение героя')
  30.     x, y = 0, 0
  31.     size = width, height = shir * 50, vis * 50
  32.     sc = pygame.display.set_mode(size)
  33.     img_pers = pygame.image.load(img_pers)
  34.     img_korobka = pygame.image.load(img_korobka).convert()
  35.     img_trava = pygame.image.load(img_trava).convert()
  36.     img_zastavka = pygame.image.load(img_zastavka).convert()
  37.     img_zastavka = pygame.transform.scale(img_zastavka, (shir * 50, vis * 50))
  38.     sleep(1)
  39.     sc.blit(img_zastavka, (0, 0))
  40.     pygame.display.update()
  41.     while True:
  42.         for i in pygame.event.get():
  43.             if i.type == pygame.QUIT:
  44.                 pygame.quit()
  45.             elif i.type == pygame.KEYDOWN:
  46.                 if initial == 1:
  47.                     for ii in range(len(pole)):
  48.                         for j in range(len(pole[0])):
  49.                             if pole[ii][j] == 'X':
  50.                                 sc.blit(img_korobka, (j * 50, ii * 50))
  51.                             elif pole[ii][j] == '0':
  52.                                 sc.blit(img_trava, (j * 50, ii * 50))
  53.                     sc.blit(img_pers, (jh * 50 + 14, ih * 50 + 10))
  54.                     pygame.display.update()
  55.                     initial = 0
  56.                 if i.key == pygame.K_DOWN:
  57.                     if ih + 1 < len(pole):
  58.                         if pole[ih + 1][jh] == '0':
  59.                             sc.blit(img_trava, (jh * 50, ih * 50))
  60.                             ih += 1
  61.                             sc.blit(img_pers, (jh * 50 + 14, ih * 50 + 10))
  62.                         pygame.display.update()
  63.                 elif i.key == pygame.K_UP:
  64.                     if ih - 1 >= 0:
  65.                         if pole[ih - 1][jh] == '0':
  66.                             sc.blit(img_trava, (jh * 50, ih * 50))
  67.                             ih -= 1
  68.                             sc.blit(img_pers, (jh * 50 + 14, ih * 50 + 10))
  69.                             pygame.display.update()
  70.                 elif i.key == pygame.K_LEFT:
  71.                     if jh - 1 >= 0:
  72.                         if pole[ih][jh - 1] == '0':
  73.                             sc.blit(img_trava, (jh * 50, ih * 50))
  74.                             jh -= 1
  75.                             sc.blit(img_pers, (jh * 50 + 14, ih * 50 + 10))
  76.                             pygame.display.update()
  77.                 elif i.key == pygame.K_RIGHT:
  78.                     if jh + 1 < len(pole[0]):
  79.                         if pole[ih][jh + 1] == '0':
  80.                             sc.blit(img_trava, (jh * 50, ih * 50))
  81.                             jh += 1
  82.                             sc.blit(img_pers, (jh * 50 + 14, ih * 50 + 10))
  83.                             pygame.display.update()
  84.     pygame.quit()
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement