Update Gaame with menu

main
Artjoms Marians Ņečajevs 2024-03-02 16:17:13 +00:00
parent c0d65c7c1a
commit a40b5b84a6
1 changed files with 30 additions and 7 deletions

View File

@ -113,7 +113,7 @@ class PlayerBullet(pygame.sprite.Sprite):
if not screen.get_rect().colliderect(self.rect):
self.kill()
if pygame.sprite.spritecollideany(self, walls):
self.kill() # Удаляем снаряд, если он столкнулся со стеной
self.kill() # Удаляем снаряд, если он столкнулся со стеной
# Определение класса снаряда бота
class BotBullet(pygame.sprite.Sprite):
@ -130,7 +130,7 @@ class BotBullet(pygame.sprite.Sprite):
if not screen.get_rect().colliderect(self.rect):
self.kill()
if pygame.sprite.spritecollideany(self, walls):
self.kill() # Удаляем снаряд, если он столкнулся со стеной
self.kill() # Удаляем снаряд, если он столкнулся со стеной
# Определение класса для бота
class Bot(pygame.sprite.Sprite):
@ -261,6 +261,8 @@ bot = Bot(3 * SCREEN_WIDTH // 4, SCREEN_HEIGHT // 2)
all_sprites.add(bot)
tanks.add(bot)
music_enabled = True
def draw_menu():
screen.blit(menu_background, (0, 0))
# Рисуем текст меню
@ -274,9 +276,31 @@ def draw_menu():
quit_rect = quit_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50))
screen.blit(quit_text, quit_rect)
# Рисуем кнопку включения/выключения музыки
music_text = font.render("Music: " + ("On" if music_enabled else "Off"), True, WHITE)
music_rect = music_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 150))
screen.blit(music_text, music_rect)
pygame.display.flip()
return quit_rect # Возвращаем прямоугольник для кнопки "Выйти"
return quit_rect, music_rect # Возвращаем прямоугольники для кнопок "Выйти" и "Музыка"
# Главный цикл программы
menu_active = True
while menu_active:
quit_rect, music_rect = draw_menu()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Левая кнопка мыши
if quit_rect.collidepoint(event.pos):
pygame.quit()
quit()
elif music_rect.collidepoint(event.pos): # Проверяем, нажата ли кнопка музыки
music_enabled = not music_enabled # Инвертируем состояние звука
if music_enabled:
pygame.mixer.music.unpause() # Включаем музыку
else:
pygame.mixer.music.pause() # Выключаем музыку
@ -340,7 +364,7 @@ while running:
# Обновление всех спрайтов только в случае, если игра не на паузе
all_sprites.update()
# Проверка столкновений пуль с танками
# Проверка столкновений пуль с танками
for bullet in bullets:
if isinstance(bullet, PlayerBullet):
tank_hit = pygame.sprite.spritecollideany(bullet, tanks, pygame.sprite.collide_mask)
@ -400,7 +424,7 @@ while running:
# Обновление всех спрайтов
all_sprites.update()
# Проверка столкновений пуль с танками
# Проверка столкновений пуль с танками
for bullet in bullets:
if isinstance(bullet, PlayerBullet):
tank_hit = pygame.sprite.spritecollideany(bullet, tanks, pygame.sprite.collide_mask)
@ -503,10 +527,9 @@ while running:
screen.blit(stenki, (1600, 1030))
screen.blit(stenki, (1800, 1030))
pygame.display.flip()
clock.tick(FPS)
# Завершение работы Pygame
pygame.quit()
# token: 8f195a885b18a96da6577884cc731f850f33a9e2 переделай код так чтоби игру мозно било приостанавливат кнопкой escape