add menu, oop full

main
Сергей Ванюшкин 2023-12-11 22:19:34 +03:00
parent 584f7c56df
commit f9ac8c343b
3 changed files with 77 additions and 66 deletions

View File

@ -1,4 +1,9 @@
from snake import game
from snake.game import Game
if __name__ == "__main__":
game = Game()
game.game_init()
game.set_title()
game.get_menu()
game.mainloop()

View File

@ -1,7 +0,0 @@
class GameConfig:
pass
class SnakeConfig:
pass

View File

@ -1,8 +1,8 @@
import os
import time
from random import choice
import pygame as pg
from pygame.key import name
import pygame_menu as pgm
from .snake import Snake
@ -17,6 +17,7 @@ class Game:
self.font = pg.font.SysFont('arial', 72)
self.score = 0
self.paused = True
self.started = False
self.title = 'Snakessss'
self.done = False # Флаг главного цикла
self.need_reset = False
@ -25,6 +26,8 @@ class Game:
self.fps = 60
self.set_title()
self.menu = None
self.snake = Snake()
self.foods_array = []
def game_init(self):
pg.init()
@ -33,13 +36,19 @@ class Game:
self.set_bg()
self.menu = pgm.Menu('Snakessss game', 400, 300, theme=pgm.pygame_menu.themes.THEME_DARK)
self.menu.add.button('Start new game', self.start_new_game)
self.menu.add.button('Restore game', self.restore_game)
if self.started:
self.menu.add.button('Restore game', self.restore_game)
self.menu.add.selector('Speed', [('Slow', 40), ('Medium', 25), ('Fast', 10)], onchange=self.set_speed)
self.menu.add.button('Exit', pgm.pygame_menu.events.EXIT)
self.menu.mainloop(self.screen)
def disable_menu(self):
self.menu.disable()
def set_speed(self, value, difficulty):
self.snake.speed = difficulty
def set_title(self):
pg.display.set_caption(self.title)
@ -47,6 +56,7 @@ class Game:
self.screen.blit(self.bg, (0, 0))
def start_new_game(self):
self.started = True
self.paused = False
self.disable_menu()
self.need_reset = True
@ -71,64 +81,67 @@ class Game:
self.get_menu()
def update_game_window(self):
while len(self.foods_array) < 2:
food = Food()
if food.get_coords() not in self.snake.get_coords():
self.foods_array.append(food)
def mainloop():
for f in self.foods_array:
if self.snake.get_head_coords() == f.get_coords():
self.foods_array.remove(f)
self.snake.length += 1
self.set_bg()
self.snake.draw(self.screen)
for f in self.foods_array:
f.draw(self.screen)
pg.display.update()
if self.snake.impacted:
self.gameover()
self.started = False
def mainloop(self):
while not self.done:
if self.need_reset:
self.snake.reset()
self.start_new_game()
self.need_reset = False
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
pressed = pg.key.get_pressed()
if pressed[pg.K_ESCAPE]:
self.paused = False if self.paused else True
if self.paused:
self.get_menu()
if pressed[pg.K_UP]:
if self.snake.direction != 'down':
self.snake.direction = 'up'
if pressed[pg.K_DOWN]:
if self.snake.direction != 'up':
self.snake.direction = 'down'
if pressed[pg.K_LEFT]:
if self.snake.direction != 'right':
self.snake.direction = 'left'
if pressed[pg.K_RIGHT]:
if self.snake.direction != 'left':
self.snake.direction = 'right'
self.update_game_window()
self.clock.tick(self.fps)
if __name__ == "__main__":
game = Game()
game.game_init()
game.set_title()
game.get_menu()
snake =Snake()
foods_array = []
game.mainloop()
def update_game_window():
while len(foods_array) < 2:
food = Food()
if food.get_coords() not in snake.get_coords():
foods_array.append(food)
for f in foods_array:
if snake.get_head_coords() == f.get_coords():
foods_array.remove(f)
snake.length += 1
game.set_bg()
snake.draw(game.screen)
for f in foods_array:
f.draw(game.screen)
pg.display.update()
if snake.impacted:
game.gameover()
while not game.done:
if game.need_reset:
snake.reset()
game.start_new_game()
game.need_reset = False
for event in pg.event.get():
if event.type == pg.QUIT:
game.done = True
pressed = pg.key.get_pressed()
if pressed[pg.K_ESCAPE]:
game.paused = False if game.paused else True
if game.paused:
game.get_menu()
if pressed[pg.K_UP]:
if snake.direction != 'down':
snake.direction = 'up'
if pressed[pg.K_DOWN]:
if snake.direction != 'up':
snake.direction = 'down'
if pressed[pg.K_LEFT]:
if snake.direction != 'right':
snake.direction = 'left'
if pressed[pg.K_RIGHT]:
if snake.direction != 'left':
snake.direction = 'right'
update_game_window()
game.clock.tick(game.fps)