Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- #Настройки окна
- WIDTH = 500
- HEIGHT = 500
- FPS = 60
- #Настройка цвета
- BLACK = (0,0,0)
- WHITE = (255,255,255)
- RED = (255,0,0)
- BLUE = (0,0,255)
- #Инициализация
- pygame.init()
- screen = pygame.display.set_mode((WIDTH,HEIGHT))
- clock = pygame.time.Clock()
- #Время
- lastTime = 0
- currentTime = 0
- # Персонаж
- x = WIDTH // 2
- y = HEIGHT // 2
- hero = pygame.Rect(x, y, 60, 50)
- heroImg = pygame.image.load('razorinv.png')
- # Противники
- enemies = []
- enemycd = 5
- enemyImage = pygame.image.load('invaderinv.png').convert()
- enemyRect = enemyImage.get_rect()
- we = enemyRect.width
- he = enemyRect.height
- points = 0
- # Звезд
- stars = []
- starcd = 10
- starImg = pygame.image.load('starinv.png')
- starRect = starImg.get_rect()
- ws = enemyRect.width
- hs = enemyRect.height
- # Пули
- wb = 2
- hb = 5
- bulletImg = pygame.image.load("bullet.png")
- bullets = []
- isShot = False
- # Шрифты
- pointsT = pygame.font.SysFont('comic sans ms', 14)
- gameover = pygame.font.SysFont('comic sans ms', 60)
- moving = ''
- GO = False
- running = True
- while running:
- screen.fill(BLACK)
- 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):
- points += 1
- bullets.remove(bullet)
- enemies.remove(enemy)
- # Отрисовка счета
- points_text = pointsT.render('Очки: ' + str(points), 1, WHITE)
- screen.blit(points_text, (10,10))
- # ПУЛИ
- # Создание пуль
- if isShot:
- bulRect = pygame.Rect(hero.left + 33, hero.top + 5, wb, hb)
- bullets.append(bulRect)
- isShot = False
- # Отрисовка пуль
- for bullet in bullets:
- screen.blit(bulletImg, (bullet.left, bullet.top))
- bullet.top -= 5
- #Удаление пуль
- index_bul = 0
- for b in bullets:
- if b.bottom < -5:
- bullets.pop(index_bul)
- index_bul += 1
- # ЗАХВАТЧИКИ
- currentTime = pygame.time.get_ticks()
- # Создание противников
- if currentTime - lastTime > enemycd:
- x_enemy = random.randint(we, WIDTH - we)
- enemies.append(pygame.Rect(x_enemy, -he, we, he))
- lastTime = currentTime
- enemycd = random.randint(100, 5000)
- # Отрисовка противников
- for enemy in enemies:
- screen.blit(enemyImage, (enemy.left, enemy.top))
- enemy.top += 2
- index_enemy = 0
- # Удаление противников
- for enemy in enemies:
- if enemy.top > HEIGHT:
- del enemies[index_enemy]
- # ЗВЕЗДЫ
- starcd -= 1
- if starcd < 0:
- x_star = random.randint(0,WIDTH-ws)
- star = pygame.Rect(x_star, -hs , ws, hs)
- stars.append(star)
- starcd = random.randint(20, 40)
- for star in stars:
- screen.blit(starImg, (star.left, star.top))
- star.top += 4
- if star.top > HEIGHT:
- stars.remove(star)
- #Отрисовка персонажа
- screen.blit(heroImg, (hero.left, hero.top))
- if GO:
- running = False
- pygame.display.update()
- clock.tick(FPS)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement