Advertisement
OtsoSilver

Untitled

Sep 11th, 2021
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 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. enemies = []
  32. enemycd = 5
  33. enemyImage = pygame.image.load('invaderinv.png').convert()
  34. enemyRect = enemyImage.get_rect()
  35. we = enemyRect.width
  36. he = enemyRect.height
  37. points = 0
  38.  
  39. # Пули
  40. wb = 2
  41. hb = 5
  42. bulletImg = pygame.image.load("bullet.png")
  43. bullets = []
  44. isShot = False
  45.  
  46. #Переменные движения
  47. moving = ''
  48. GO = False
  49. running = True
  50. while running:
  51.     screen.fill(BLACK)
  52.     for i in pygame.event.get():
  53.         if i.type == pygame.QUIT:
  54.             running = False
  55.         if i.type == pygame.KEYDOWN:
  56.             if i.key == pygame.K_LEFT:
  57.                 moving = 'LEFT'
  58.             if i.key == pygame.K_RIGHT:
  59.                 moving = 'RIGHT'
  60.             if i.key == pygame.K_UP:
  61.                 moving = 'UP'
  62.             if i.key == pygame.K_DOWN:
  63.                 moving = 'DOWN'
  64.             if i.key == pygame.K_SPACE:
  65.                 isShot = True
  66.         if i.type == pygame.KEYUP:
  67.             if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
  68.                 moving = 'STOP'
  69.    
  70.     # Передвижение персонажа
  71.     if moving == 'LEFT' and hero.left > 0:
  72.         hero.left -= 5
  73.     if moving == 'RIGHT' and hero.right < WIDTH:
  74.         hero.left += 5
  75.     if moving == 'UP' and hero.top > 0:
  76.         hero.top -= 5
  77.     if moving == 'DOWN' and hero.bottom < HEIGHT:
  78.         hero.top += 5
  79.        
  80. # ПУЛИ
  81.     # Создание пуль
  82.     if isShot:
  83.         bulRect = pygame.Rect(hero.left + 33, hero.top + 5, wb, hb)
  84.         bullets.append(bulRect)
  85.         isShot = False
  86.    
  87.     # Отрисовка пуль
  88.     for bullet in bullets:
  89.         screen.blit(bulletImg, (bullet.left, bullet.top))
  90.         bullet.top -= 5
  91.        
  92.     #Удаление пуль
  93.     index_bul = 0
  94.     for b in bullets:
  95.         if b.bottom < -5:
  96.             bullets.pop(index_bul)
  97.         index_bul += 1
  98.        
  99. # ЗАХВАТЧИКИ
  100.     currentTime = pygame.time.get_ticks()
  101.     # Создание противников
  102.     if currentTime - lastTime > enemycd:
  103.         x_enemy = random.randint(we, WIDTH - we)
  104.         enemies.append(pygame.Rect(x_enemy, -he, we, he))
  105.         lastTime = currentTime
  106.         enemycd = random.randint(100, 5000)
  107.    
  108.     # Отрисовка противников
  109.     for enemy in enemies:
  110.         screen.blit(enemyImage, (enemy.left, enemy.top))
  111.         enemy.top += 2
  112.    
  113.     index_enemy = 0
  114.     # Удаление противников
  115.     for enemy in enemies:
  116.         if enemy.top > HEIGHT:
  117.             del enemies[index_enemy]
  118.  
  119.     for enemy in enemies:
  120.         if enemy.colliderect(hero):
  121.             GO = True
  122.        
  123.            
  124.     if GO:
  125.         running = False
  126.        
  127.     #Отрисовка персонажа
  128.     screen.blit(heroImg, (hero.left, hero.top))
  129.    
  130.     pygame.display.update()
  131.     clock.tick(FPS)
  132. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement