OtsoSilver

Untitled

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