Advertisement
OtsoSilver

Untitled

Sep 4th, 2021
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 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. # Пули
  31. # Ширина пули для Rect
  32. wb = 2
  33. # Высота пули для Rect
  34. hb = 5
  35. # Изображение пули
  36. bulletImg = pygame.image.load("bullet.png")
  37. # Список, где будут хранится пули ввиде объектов Rect
  38. bullets = []
  39. # Переменная, которая будет меняться, когда игрок будет нажмить пробел
  40. isShot = False
  41.  
  42.  
  43. moving = ''
  44. running = True
  45. while running:
  46.     screen.fill(BLACK)
  47.     for i in pygame.event.get():
  48.         if i.type == pygame.QUIT:
  49.             running = False
  50.         if i.type == pygame.KEYDOWN:
  51.             if i.key == pygame.K_LEFT:
  52.                 moving = 'LEFT'
  53.             if i.key == pygame.K_RIGHT:
  54.                 moving = 'RIGHT'
  55.             if i.key == pygame.K_UP:
  56.                 moving = 'UP'
  57.             if i.key == pygame.K_DOWN:
  58.                 moving = 'DOWN'
  59.             if i.key == pygame.K_DOWN:
  60.                 isShot = True
  61.         if i.type == pygame.KEYUP:
  62.             if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
  63.                 moving = 'STOP'
  64.    
  65.     # Передвижение персонажа
  66.     if moving == 'LEFT' and hero.left > 0:
  67.         hero.left -= 5
  68.     if moving == 'RIGHT' and hero.right < WIDTH:
  69.         hero.left += 5
  70.     if moving == 'UP' and hero.top > 100:
  71.         hero.top -= 5
  72.     if moving == 'DOWN' and hero.bottom < HEIGHT:
  73.         hero.top += 5
  74.     if isShot:
  75.         bullets.append(pygame.Rect(hero.left + 33, hero.top + 5, wb, hb))
  76.        
  77.    
  78.     #Отрисовка персонажа
  79.     screen.blit(heroImg, (hero.left, hero.top))
  80.    
  81.     pygame.display.update()
  82.     clock.tick(FPS)
  83. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement