def start_menu(surface): """ Показ основного меню """ TEXT_SPACING = 35 RIGHT_INDENT = 380 intro_text = ["Начать", "Правила игры", "Выйти"] font_text = pygame.font.Font(None, 75) font_name = pygame.font.Font(None, 45) bg = pygame.transform.scale(load_image('bg.jpg'), (1000, 700)) name = font_text.render("Alian attack", True, (200, 50, 0)) arrow = load_image('arrow.png') arrow = pygame.transform.scale(arrow, (70, 35)) y_arr = 220 counter_arr = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: if counter_arr != 2: counter_arr += 1 if event.key == pygame.K_UP: if counter_arr != 0: counter_arr -= 1 if event.key == pygame.K_RETURN: if counter_arr == 0: return if counter_arr == 1: start_rules(surface) if counter_arr == 2: quit() surface.blit(bg, (0, 0)) text_coord = 190 for line in intro_text: string_rendered = font_name.render(line, True, (200, 200, 200)) intro_rect = string_rendered.get_rect() text_coord += TEXT_SPACING intro_rect.top = text_coord intro_rect.x = RIGHT_INDENT text_coord += intro_rect.height surface.blit(string_rendered, intro_rect) surface.blit(name, (RIGHT_INDENT - 10, 110)) surface.blit(arrow, (RIGHT_INDENT - 75, y_arr + 65 * counter_arr)) pygame.display.flip()
def start_rules(surface): """ Показ правил """ TEXT_SPACING = 20 RIGHT_INDENT = 25 intro_text = [ 'Вам будут доступны средства, на которые нужно покупать башни.', 'Башни будут атаковать приходящих монстров.', 'При смерти монстры будут давать вам деньги, чтобы покупать новые башни.', 'Башни можно установить только на специальных зонах с квадратами.', 'ГЛАВНОЕ, ЧТОБЫ МОНСТРЫ НЕ ПРОШЛИ К ВАМ НА БАЗУ!', 'Для этого нужно продержаться 2 минуты, удачи' ] font = pygame.font.Font(None, 27) bg = pygame.transform.scale(load_image('bg.jpg'), (1000, 700)) surface.blit(bg, (0, 0)) text_coord = 150 for line in intro_text: string_rendered = font.render(line, True, (200, 200, 200)) intro_rect = string_rendered.get_rect() text_coord += TEXT_SPACING intro_rect.top = text_coord intro_rect.x = RIGHT_INDENT text_coord += intro_rect.height surface.blit(string_rendered, intro_rect) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: return pygame.display.flip()
pygame.mixer.music.play(loops=-1) start_menu(screen) screen.fill((0, 0, 0)) # генерация уровня lvl = load_level('level2.txt') generate_level(lvl, Tile) FPS = 60 SPAWN_GROUP = pygame.USEREVENT + 1 COUNT_TIME = pygame.USEREVENT + 2 SPAWN_ENEMY = pygame.USEREVENT + 3 player_money = 170 money_image = load_image('money.jpg') money_image.set_colorkey((255, 255, 255)) money_image = pygame.transform.scale(money_image, (100, 100)) base_hp = 5 base_image = load_image('heart.png') base_image = pygame.transform.scale(base_image, (80, 60)) light = load_image('light.png') light = pygame.transform.scale(light, (160, 150)) light.set_alpha(200) is_fade_out = True bullet_turret = pygame.transform.scale(turret_images['bullet'], (60, 75)) laser_turret = pygame.transform.scale(turret_images['laser'], (60, 75)) rocket_turret = pygame.transform.scale(turret_images['rocket'], (60, 75))
from technical_func import load_image import pygame tile_images = { 'grass': load_image('grass.png'), 'trail': load_image('trail.png'), 'base': load_image('turret_base.png'), } TILE_WIDTH = TILE_HEIGHT = 50 tiles_group = pygame.sprite.Group() class Tile(pygame.sprite.Sprite): def __init__(self, tile_type, pos_x, pos_y): super().__init__(tiles_group) self.image = tile_images[tile_type] self.image = pygame.transform.scale(self.image, (TILE_WIDTH, TILE_HEIGHT)) self.tile_type = tile_type self.rect = self.image.get_rect().move(TILE_WIDTH * pos_x, TILE_HEIGHT * pos_y)
import pygame from technical_func import load_image from ground import TILE_WIDTH from pygame.math import Vector2 from math import sqrt from character import Enemy, enemy_group WIDTH = 50 HEIGHT = 60 turret_group = pygame.sprite.Group() turret_images = { 'rocket': load_image('rocket_turret.png'), 'bullet': load_image('bullet_turret.png'), 'laser': load_image('laser.png') } shooting_events = {} def get_cell(height, width, mouse_pos): """ Определение нажатой клетки """ for i in range(height): for j in range(width): x1 = TILE_WIDTH * j x2 = x1 + TILE_WIDTH y1 = TILE_WIDTH * i y2 = y1 + TILE_WIDTH if mouse_pos[0] in range(x1, x2 + 1) and mouse_pos[1] in range( y1, y2 + 1): return i, j
import pygame from technical_func import load_image from ground import TILE_WIDTH import random pygame.init() enemy_group = pygame.sprite.Group() enemy_images = { 'usual': load_image('usual_enemy.png'), 'sprinter': load_image('sprinter_enemy.png') } class Enemy(pygame.sprite.Sprite): def __init__(self, enemy_type): super().__init__(enemy_group) self.ORIG_HP = 28 if enemy_type == 'sprinter' else 40 self.hp = self.ORIG_HP self.award = 10 if enemy_type == 'sprinter' else 6 self.image = enemy_images[enemy_type] self.image = pygame.transform.scale(self.image, (TILE_WIDTH, TILE_WIDTH)) self.orig_image = self.image self.rect = self.image.get_rect() self.rect.x = 0 self.rect.y = random.randrange(330, 355) self.dx = 3 if enemy_type == 'sprinter' else 2 self.dy = 0 self.control_positions = { (self.rect.x + 252, self.rect.y): (0, self.dx, -90),