Advertisement
OtsoSilver

Untitled

Sep 11th, 2021
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 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. stars = []
  41. starcd = 10
  42. starImg = pygame.image.load('starinv.png')
  43. starRect = starImg.get_rect()
  44. ws = enemyRect.width
  45. hs = enemyRect.height
  46.  
  47. # Пули
  48. wb = 2
  49. hb = 5
  50. bulletImg = pygame.image.load("bullet.png")
  51. bullets = []
  52. isShot = False
  53.  
  54. # Шрифты
  55. pointsT = pygame.font.SysFont('comic sans ms', 14)
  56. gameover = pygame.font.SysFont('comic sans ms', 60)
  57.  
  58. moving = ''
  59. GO = False
  60. running = True
  61. while running:
  62.     screen.fill(BLACK)
  63.     for i in pygame.event.get():
  64.         if i.type == pygame.QUIT:
  65.             running = False
  66.         if i.type == pygame.KEYDOWN:
  67.             if i.key == pygame.K_LEFT:
  68.                 moving = 'LEFT'
  69.             if i.key == pygame.K_RIGHT:
  70.                 moving = 'RIGHT'
  71.             if i.key == pygame.K_UP:
  72.                 moving = 'UP'
  73.             if i.key == pygame.K_DOWN:
  74.                 moving = 'DOWN'
  75.             if i.key == pygame.K_SPACE:
  76.                 isShot = True
  77.         if i.type == pygame.KEYUP:
  78.             if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
  79.                 moving = 'STOP'
  80.    
  81.     # Передвижение персонажа
  82.     if moving == 'LEFT' and hero.left > 0:
  83.         hero.left -= 5
  84.     if moving == 'RIGHT' and hero.right < WIDTH:
  85.         hero.left += 5
  86.     if moving == 'UP' and hero.top > 0:
  87.         hero.top -= 5
  88.     if moving == 'DOWN' and hero.bottom < HEIGHT:
  89.         hero.top += 5
  90.  
  91. # СТОЛКНОВЕНИЕ
  92.     # Противник с героем
  93.     for enemy in enemies:
  94.         if hero.colliderect(enemy):
  95.             GO = True
  96.    
  97.     # Противник с пулей
  98.     for bullet in bullets:
  99.         for enemy in enemies:
  100.             if bullet.colliderect(enemy):
  101.                 points += 1
  102.                 bullets.remove(bullet)
  103.                 enemies.remove(enemy)
  104.    
  105.     # Отрисовка счета
  106.     points_text = pointsT.render('Очки: ' + str(points), 1, WHITE)
  107.     screen.blit(points_text, (10,10))
  108.    
  109. # ПУЛИ
  110.     # Создание пуль
  111.     if isShot:
  112.         bulRect = pygame.Rect(hero.left + 33, hero.top + 5, wb, hb)
  113.         bullets.append(bulRect)
  114.         isShot = False
  115.    
  116.     # Отрисовка пуль
  117.     for bullet in bullets:
  118.         screen.blit(bulletImg, (bullet.left, bullet.top))
  119.         bullet.top -= 5
  120.        
  121.     #Удаление пуль
  122.     index_bul = 0
  123.     for b in bullets:
  124.         if b.bottom < -5:
  125.             bullets.pop(index_bul)
  126.         index_bul += 1
  127.        
  128. # ЗАХВАТЧИКИ
  129.     currentTime = pygame.time.get_ticks()
  130.     # Создание противников
  131.     if currentTime - lastTime > enemycd:
  132.         x_enemy = random.randint(we, WIDTH - we)
  133.         enemies.append(pygame.Rect(x_enemy, -he, we, he))
  134.         lastTime = currentTime
  135.         enemycd = random.randint(100, 5000)
  136.    
  137.     # Отрисовка противников
  138.     for enemy in enemies:
  139.         screen.blit(enemyImage, (enemy.left, enemy.top))
  140.         enemy.top += 2
  141.    
  142.     index_enemy = 0
  143.     # Удаление противников
  144.     for enemy in enemies:
  145.         if enemy.top > HEIGHT:
  146.             del enemies[index_enemy]
  147.  
  148. # ЗВЕЗДЫ
  149.     starcd -= 1
  150.     if starcd < 0:
  151.         x_star = random.randint(0,WIDTH-ws)
  152.         star = pygame.Rect(x_star, -hs , ws, hs)
  153.         stars.append(star)
  154.         starcd = random.randint(20, 40)
  155.    
  156.     for star in stars:
  157.         screen.blit(starImg, (star.left, star.top))
  158.         star.top += 4
  159.         if star.top > HEIGHT:
  160.             stars.remove(star)    
  161.    
  162.     #Отрисовка персонажа
  163.     screen.blit(heroImg, (hero.left, hero.top))
  164.    
  165.     if GO:
  166.         running = False  
  167.    
  168.     pygame.display.update()
  169.     clock.tick(FPS)
  170. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement