Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- #Настройки окна
- WIDTH = 900
- HEIGHT = 500
- FPS = 60
- # Цвета
- YELLOW = (255, 255, 0)
- SKY = (133, 193, 233)
- GREEN = (14, 209, 69)
- WHITE = (255, 255, 255)
- #Инициализация
- pygame.init()
- screen = pygame.display.set_mode((WIDTH,HEIGHT))
- clock = pygame.time.Clock()
- lastTime = 0
- currentTime = 0
- hero = pygame.Rect(75,200,100,100)
- heroImg = pygame.transform.scale(heroImg, (100, 100))
- wb = 2
- hb = 5
- bulletImg = pygame.image.load("bullet.png")
- bullets = []
- isShot = False
- enemies = []
- enemycd = 5
- enemyImage = pygame.image.load('cartoon-zombie-pixel-design_61878-721.jpg').convert()
- enemyImage = pygame.transform.scale(enemyImage, (100, 100))
- enemyRect = enemyImage.get_rect()
- we = enemyRect.width
- he = enemyRect.height
- moving = ''
- GO = False
- running = True
- while running:
- screen.fill(GREEN)
- for i in pygame.event.get():
- if i.type == pygame.QUIT:
- running = False
- if i.type == pygame.KEYDOWN:
- if i.key == pygame.K_LEFT:
- moving = 'LEFT'
- if i.key == pygame.K_RIGHT:
- moving = 'RIGHT'
- if i.key == pygame.K_UP:
- moving = 'UP'
- if i.key == pygame.K_DOWN:
- moving = 'DOWN'
- if i.key == pygame.K_SPACE:
- isShot = True
- if i.type == pygame.KEYUP:
- if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT or i.key == pygame.K_UP or i.key == pygame.K_DOWN:
- moving = 'STOP'
- if moving == 'LEFT' and hero.left > 0:
- hero.left -= 5
- if moving == 'RIGHT' and hero.right < WIDTH:
- hero.left += 5
- if moving == 'UP' and hero.top > 0:
- hero.top -= 5
- if moving == 'DOWN' and hero.bottom < HEIGHT:
- hero.top += 5
- for enemy in enemies:
- if hero.colliderect(enemy):
- GO = True
- for bullet in bullets:
- for enemy in enemies:
- if bullet.colliderect(enemy):
- bullets.remove(bullet)
- enemies.remove(enemy)
- currentTime = pygame.time.get_ticks()
- if currentTime - lastTime > enemycd:
- y_enemy = random.randint(0, HEIGHT-we)
- enemies.append(pygame.Rect(WIDTH + he, y_enemy, he, we))
- lastTime = currentTime
- enemycd = random.randint(100, 500)
- if isShot:
- bulRect = pygame.Rect(hero.left + 33, hero.top + 55, wb, hb)
- bullets.append(bulRect)
- isShot = False
- for bullet in bullets:
- screen.blit(bulletImg, (bullet.left, bullet.top))
- bullet.right += 5
- index_bul = 0
- for b in bullets:
- if b.bottom < -5:
- bullets.pop(index_bul)
- index_bul += 1
- for enemy in enemies:
- screen.blit(enemyImage, (enemy.left, enemy.top))
- enemy.left -= 2
- print(enemy.top)
- index_enemy = 0
- for enemy in enemies:
- if enemy.top > HEIGHT:
- del enemies[index_enemy]
- screen.blit(heroImg, (hero.left, hero.top))
- pygame.display.update()
- clock.tick(FPS)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment