OtsoSilver

Untitled

Oct 16th, 2021
530
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import random
  3.  
  4. #Настройки окна
  5. WIDTH = 900
  6. HEIGHT = 500
  7. FPS = 60
  8.  
  9. # Цвета
  10. YELLOW = (255, 255, 0)
  11. SKY = (133, 193, 233)
  12. GREEN = (14, 209, 69)
  13. WHITE = (255, 255, 255)
  14.  
  15. #Инициализация
  16. pygame.init()
  17. screen = pygame.display.set_mode((WIDTH,HEIGHT))
  18. clock = pygame.time.Clock()
  19.  
  20. lastTime = 0
  21. currentTime = 0
  22.  
  23. hero = pygame.Rect(75,200,100,100)
  24. heroImg = pygame.image.load('20117210.48e3100b.160x160o.eca69732ef13@2x.png').convert()
  25.  
  26. heroImg = pygame.transform.scale(heroImg, (100, 100))
  27.  
  28. wb = 2
  29. hb = 5
  30. bulletImg = pygame.image.load("bullet.png")
  31. bullets = []
  32. isShot = False
  33.  
  34. enemies = []
  35. enemycd = 5
  36. enemyImage = pygame.image.load('cartoon-zombie-pixel-design_61878-721.jpg').convert()
  37. enemyImage = pygame.transform.scale(enemyImage, (100, 100))
  38. enemyRect = enemyImage.get_rect()
  39. we = enemyRect.width
  40. he = enemyRect.height
  41.  
  42. moving = ''
  43. GO = False
  44. running = True
  45. while running:
  46.     screen.fill(GREEN)
  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_SPACE:
  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 > 0:
  71.         hero.top -= 5
  72.     if moving == 'DOWN' and hero.bottom < HEIGHT:
  73.         hero.top += 5
  74.        
  75.     for enemy in enemies:
  76.         if hero.colliderect(enemy):
  77.             GO = True
  78.            
  79.     for bullet in bullets:
  80.        
  81.         for enemy in enemies:
  82.             if bullet.colliderect(enemy):
  83.                
  84.                 bullets.remove(bullet)
  85.                 enemies.remove(enemy)
  86.        
  87.     currentTime = pygame.time.get_ticks()
  88.    
  89.     if currentTime - lastTime > enemycd:
  90.         y_enemy = random.randint(0,  HEIGHT-we)
  91.         enemies.append(pygame.Rect(WIDTH + he, y_enemy, he, we))
  92.         lastTime = currentTime
  93.         enemycd = random.randint(100, 500)
  94.        
  95.        
  96.     if isShot:
  97.         bulRect = pygame.Rect(hero.left + 33, hero.top + 55, wb, hb)
  98.         bullets.append(bulRect)
  99.         isShot = False
  100.        
  101.     for bullet in bullets:
  102.         screen.blit(bulletImg, (bullet.left, bullet.top))
  103.         bullet.right += 5
  104.        
  105.     index_bul = 0
  106.     for b in bullets:
  107.         if b.bottom < -5:
  108.             bullets.pop(index_bul)
  109.         index_bul += 1
  110.        
  111.     for enemy in enemies:
  112.         screen.blit(enemyImage, (enemy.left, enemy.top))
  113.         enemy.left -= 2
  114.         print(enemy.top)
  115.  
  116.     index_enemy = 0
  117.    
  118.     for enemy in enemies:
  119.         if enemy.top > HEIGHT:
  120.             del enemies[index_enemy]
  121.  
  122.     screen.blit(heroImg, (hero.left, hero.top))
  123.     pygame.display.update()
  124.     clock.tick(FPS)
  125. pygame.quit()
RAW Paste Data