218 lines
7.1 KiB
Python
218 lines
7.1 KiB
Python
import pygame
|
||
import sys
|
||
import random
|
||
import math
|
||
import time
|
||
|
||
# Инициализация Pygame
|
||
pygame.init()
|
||
|
||
# Определение шрифта и размера текста для большой надписи "ЛОХ"
|
||
|
||
SCREEN_WIDTH = 1920
|
||
SCREEN_HEIGHT = 1080
|
||
|
||
font_large = pygame.font.Font(None, 800)
|
||
game_over_text_large = font_large.render("ЛОХ", True, (255, 0, 0))
|
||
game_over_text_large_rect = game_over_text_large.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
||
|
||
|
||
# Определение констант
|
||
SCREEN_WIDTH = 1920
|
||
SCREEN_HEIGHT = 1080
|
||
TANK_SIZE = 30 # Увеличенный размер
|
||
ENEMY_SIZE = 20
|
||
FPS = 60
|
||
BULLET_SPEED = 10
|
||
|
||
# Определение цветов
|
||
WHITE = (255, 255, 255)
|
||
BLACK = (0, 0, 0)
|
||
|
||
# Создание игрового окна
|
||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), flags=pygame.FULLSCREEN)
|
||
pygame.display.set_caption("Танчики")
|
||
color = (0, 255, 0)
|
||
screen.fill(color)
|
||
pygame.display.flip()
|
||
|
||
# Загрузка изображения танка, смотрящего вправо
|
||
tank_image = pygame.image.load("ntank-removebg-preview.png")
|
||
|
||
# Увеличение размера изображения танка
|
||
tank_image = pygame.transform.scale(tank_image, (TANK_SIZE, TANK_SIZE))
|
||
|
||
# Определение класса танка
|
||
class Tank(pygame.sprite.Sprite):
|
||
def __init__(self, x, y):
|
||
super().__init__()
|
||
self.original_image = tank_image
|
||
self.image = self.original_image
|
||
self.rect = self.image.get_rect()
|
||
self.rect.center = (x, y)
|
||
self.angle = 0
|
||
|
||
def update(self):
|
||
keys = pygame.key.get_pressed()
|
||
if keys[pygame.K_LEFT]:
|
||
self.angle += 5
|
||
if keys[pygame.K_RIGHT]:
|
||
self.angle -= 5
|
||
|
||
self.image = pygame.transform.rotate(self.original_image, self.angle)
|
||
self.rect = self.image.get_rect(center=self.rect.center)
|
||
|
||
speed = 5
|
||
if keys[pygame.K_UP]:
|
||
self.rect.y -= speed * math.cos(math.radians(self.angle))
|
||
self.rect.x += speed * math.sin(math.radians(self.angle))
|
||
if keys[pygame.K_DOWN]:
|
||
self.rect.y += speed * math.cos(math.radians(self.angle))
|
||
self.rect.x -= speed * math.sin(math.radians(self.angle))
|
||
|
||
def shoot(self):
|
||
bullet = Bullet(self.rect.centerx, self.rect.centery, self.angle + 90) # Изменение угла стрельбы
|
||
all_sprites.add(bullet)
|
||
bullets.add(bullet)
|
||
|
||
# Определение класса вражеского танка
|
||
class EnemyTank(pygame.sprite.Sprite):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.image = pygame.Surface((ENEMY_SIZE, ENEMY_SIZE))
|
||
self.image.fill(WHITE)
|
||
self.rect = self.image.get_rect()
|
||
self.rect.center = (random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT))
|
||
self.angle = 0 # Установка начального угла для того, чтобы смотреть вперед
|
||
|
||
# Определение класса снаряда
|
||
class Bullet(pygame.sprite.Sprite):
|
||
def __init__(self, x, y, angle):
|
||
super().__init__()
|
||
self.image = pygame.Surface((5, 5))
|
||
self.image.fill(WHITE)
|
||
self.rect = self.image.get_rect()
|
||
self.rect.center = (x, y)
|
||
self.angle = angle
|
||
|
||
def update(self):
|
||
self.rect.x += BULLET_SPEED * math.cos(math.radians(self.angle))
|
||
self.rect.y -= BULLET_SPEED * math.sin(math.radians(self.angle))
|
||
if self.rect.bottom < 0 or self.rect.top > SCREEN_HEIGHT or self.rect.right < 0 or self.rect.left > SCREEN_WIDTH:
|
||
self.kill()
|
||
|
||
# Определение класса препятствия
|
||
class Obstacle(pygame.sprite.Sprite):
|
||
def __init__(self, x, y, image):
|
||
super().__init__()
|
||
self.image = pygame.transform.scale(pygame.image.load(image).convert(), (100, 100))
|
||
self.rect = self.image.get_rect()
|
||
self.rect.topleft = (x, y)
|
||
|
||
# Создание групп спрайтов
|
||
all_sprites = pygame.sprite.Group()
|
||
tanks = pygame.sprite.Group()
|
||
enemies = pygame.sprite.Group()
|
||
bullets = pygame.sprite.Group()
|
||
obstacles = pygame.sprite.Group()
|
||
|
||
# Создание игрока
|
||
player = Tank(SCREEN_WIDTH // 4, SCREEN_HEIGHT // 2)
|
||
all_sprites.add(player)
|
||
tanks.add(player)
|
||
|
||
# Создание вражеских танков
|
||
for _ in range(5):
|
||
enemy = EnemyTank()
|
||
all_sprites.add(enemy)
|
||
enemies.add(enemy)
|
||
|
||
# Создание препятствий
|
||
obstacle_positions = [(360, 40), (460, 730)] # Добавьте остальные препятствия, если необходимо
|
||
for pos in obstacle_positions:
|
||
obstacle = Obstacle(pos[0], pos[1], "stena.jpeg")
|
||
all_sprites.add(obstacle)
|
||
obstacles.add(obstacle)
|
||
|
||
# Основной игровой цикл
|
||
clock = pygame.time.Clock()
|
||
running = True
|
||
game_over = False
|
||
game_over_font = pygame.font.SysFont(None, 100)
|
||
game_over_text = game_over_font.render("ЛОХ", True, (255, 0, 0))
|
||
game_over_timer = 0
|
||
|
||
while running:
|
||
for event in pygame.event.get():
|
||
if event.type == pygame.QUIT:
|
||
running = False
|
||
elif event.type == pygame.KEYDOWN:
|
||
if event.key == pygame.K_SPACE:
|
||
player.shoot()
|
||
|
||
# Обновление всех спрайтов
|
||
all_sprites.update()
|
||
|
||
# Обработка столкновений
|
||
hits = pygame.sprite.groupcollide(bullets, enemies, True, True)
|
||
for hit in hits:
|
||
enemy = EnemyTank()
|
||
all_sprites.add(enemy)
|
||
enemies.add(enemy)
|
||
|
||
hits = pygame.sprite.spritecollide(player, enemies, False)
|
||
if hits:
|
||
game_over = True
|
||
|
||
# Проверка столкновений с препятствиями
|
||
obstacle_hits = pygame.sprite.spritecollide(player, obstacles, False)
|
||
if obstacle_hits and not game_over:
|
||
game_over = True
|
||
game_over_timer = pygame.time.get_ticks()
|
||
|
||
# Отрисовка и обновление экрана
|
||
screen.fill(BLACK)
|
||
|
||
# Отрисовка игрового поля
|
||
normal_image = pygame.transform.scale(pygame.image.load("travka_pol.jpeg").convert(), (960, 540))
|
||
screen.blit(normal_image, (0, 0))
|
||
screen.blit(normal_image, (960, 0))
|
||
screen.blit(normal_image, (0, 540))
|
||
screen.blit(normal_image, (960, 540))
|
||
|
||
# Отрисовка препятствий
|
||
for obstacle in obstacles:
|
||
screen.blit(obstacle.image, obstacle.rect)
|
||
|
||
# Отрисовка игроков
|
||
screen.blit(player.image, player.rect)
|
||
|
||
# Отрисовка врагов
|
||
for enemy in enemies:
|
||
screen.blit(enemy.image, enemy.rect)
|
||
|
||
# Отрисовка снарядов
|
||
for bullet in bullets:
|
||
screen.blit(bullet.image, bullet.rect)
|
||
|
||
if game_over:
|
||
screen.blit(game_over_text_large, game_over_text_large_rect)
|
||
|
||
# Подсчёт времени после окончания игры
|
||
current_time = pygame.time.get_ticks()
|
||
if current_time - game_over_timer > 5000:
|
||
running = False
|
||
|
||
time.sleep(5)
|
||
|
||
pygame.display.flip()
|
||
|
||
# Ограничение кадров в секунду
|
||
clock.tick(FPS)
|
||
|
||
|
||
# Завершение работы Pygame
|
||
pygame.quit()
|
||
sys.exit()
|
||
|