Advertisement
OtsoSilver

Untitled

Sep 4th, 2021
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. #Настройки окна
  5. WIDTH = 500
  6. HEIGHT = 500
  7. FPS = 60
  8.  
  9. #Настройка цвета
  10. BLACK = (0,0,0)
  11. WHITE = (255,255,255)
  12. RED = (255,0,0)
  13. BLUE = (0,0,255)
  14.  
  15. #Инициализация
  16. pygame.init()
  17. screen = pygame.display.set_mode((WIDTH,HEIGHT))
  18. clock = pygame.time.Clock()
  19.  
  20. #Время
  21. lastTime = 0
  22. currentTime = 0
  23.  
  24. # Персонаж
  25. x = WIDTH // 2
  26. y = HEIGHT // 2
  27. hero = pygame.Rect(x, y, 60, 50)
  28. heroImg = pygame.image.load('razorinv.png')
  29.  
  30. moving = ''
  31. running = True
  32. while running:
  33.     screen.fill(BLACK)
  34.     for i in pygame.event.get():
  35.         if i.type == pygame.QUIT:
  36.             running = False
  37.         if i.type == pygame.KEYDOWN:
  38.             if i.key == pygame.K_LEFT:
  39.                 moving = 'LEFT'
  40.             if i.key == pygame.K_RIGHT:
  41.                 moving = 'RIGHT'
  42.             if i.key == pygame.K_UP:
  43.                 moving = 'UP'
  44.             if i.key == pygame.K_DOWN:
  45.                 moving = 'DOWN'
  46.         if i.type == pygame.KEYUP:
  47.             if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
  48.                 moving = 'STOP'
  49.    
  50.     # Передвижение персонажа
  51.     if moving == 'LEFT' and hero.left > 0:
  52.         hero.left -= 5
  53.     if moving == 'RIGHT' and hero.right < WIDTH:
  54.         hero.left += 5
  55.     if moving == 'UP' and hero.top > 100:
  56.         hero.top -= 5
  57.     if moving == 'DOWN' and hero.bottom < HEIGHT:
  58.         hero.top += 5
  59.        
  60.     #Отрисовка персонажа
  61.     screen.blit(heroImg, (hero.left, hero.top))
  62.    
  63.     pygame.display.update()
  64.     clock.tick(FPS)
  65. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement