main
Сергей Ванюшкин 2023-12-10 22:46:45 +03:00
parent 95378c520a
commit 584f7c56df
5 changed files with 116 additions and 19 deletions

View File

@ -66,7 +66,48 @@ files = [
{file = "pygame-2.5.2.tar.gz", hash = "sha256:c1b89eb5d539e7ac5cf75513125fb5f2f0a2d918b1fd6e981f23bf0ac1b1c24a"}, {file = "pygame-2.5.2.tar.gz", hash = "sha256:c1b89eb5d539e7ac5cf75513125fb5f2f0a2d918b1fd6e981f23bf0ac1b1c24a"},
] ]
[[package]]
name = "pygame-menu"
version = "4.4.3"
description = "A menu for pygame. Simple, and easy to use"
optional = false
python-versions = ">=3.6, <4"
files = [
{file = "pygame-menu-4.4.3.tar.gz", hash = "sha256:02a99e557601f356ce2cc82a10e98bcb581d539a41c666c4ee614fae366c3647"},
{file = "pygame_menu-4.4.3-py3-none-any.whl", hash = "sha256:d81f50acc854701c6b8d08b36a0b4f3a69f9ee63d3ae7591d063239812e75a0e"},
]
[package.dependencies]
pygame = ">=1.9.3"
pyperclip = "*"
typing-extensions = "*"
[package.extras]
docs = ["sphinx", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
test = ["nose2[coverage-plugin]"]
[[package]]
name = "pyperclip"
version = "1.8.2"
description = "A cross-platform clipboard module for Python. (Only handles plain text for now.)"
optional = false
python-versions = "*"
files = [
{file = "pyperclip-1.8.2.tar.gz", hash = "sha256:105254a8b04934f0bc84e9c24eb360a591aaf6535c9def5f29d92af107a9bf57"},
]
[[package]]
name = "typing-extensions"
version = "4.9.0"
description = "Backported and Experimental Type Hints for Python 3.8+"
optional = false
python-versions = ">=3.8"
files = [
{file = "typing_extensions-4.9.0-py3-none-any.whl", hash = "sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd"},
{file = "typing_extensions-4.9.0.tar.gz", hash = "sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783"},
]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "d455cccde21b87371775f4fd09e19655f080c2160a8fc3c68e2593db9f61738f" content-hash = "daa7d8a25daf865c7fecc14f541825fed526983d327dcd142eb997cb5caa05ee"

View File

@ -9,6 +9,7 @@ readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.10" python = "^3.10"
pygame = "^2.5.2" pygame = "^2.5.2"
pygame-menu = "^4.4.3"
[build-system] [build-system]

View File

@ -11,8 +11,8 @@ class Food:
) )
def __init__(self) -> None: def __init__(self) -> None:
self.x = random.randint(0, 26) * 30 self.x = random.randint(0, 25) * 30
self.y = random.randint(0, 20) * 30 self.y = random.randint(0, 19) * 30
self.type = random.choice(self.FOOD) self.type = random.choice(self.FOOD)

View File

@ -3,6 +3,7 @@ import time
from random import choice from random import choice
import pygame as pg import pygame as pg
import pygame_menu as pgm
from .snake import Snake from .snake import Snake
from .food import Food from .food import Food
@ -13,39 +14,72 @@ class Game:
self.screen_hight = 600 self.screen_hight = 600
self.clock = pg.time.Clock() self.clock = pg.time.Clock()
pg.font.init() pg.font.init()
self.font = pg.font.SysFont(choice(pg.font.get_fonts()), 72) self.font = pg.font.SysFont('arial', 72)
self.score = 0 self.score = 0
self.paused = True
self.title = 'Snakessss' self.title = 'Snakessss'
self.done = False self.done = False # Флаг главного цикла
self.need_reset = False
self.bg = pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'bg.png')) self.bg = pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'bg.png'))
self.screen = pg.display.set_mode((self.screen_widht, self.screen_hight)) self.screen = pg.display.set_mode((self.screen_widht, self.screen_hight))
self.fps = 60 self.fps = 60
self.set_title()
self.menu = None
def game_init(self): def game_init(self):
pg.init() pg.init()
def get_menu(self):
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)
self.menu.add.button('Exit', pgm.pygame_menu.events.EXIT)
self.menu.mainloop(self.screen)
def disable_menu(self):
self.menu.disable()
def set_title(self): def set_title(self):
pg.display.set_caption(self.title) pg.display.set_caption(self.title)
def set_bg(self): def set_bg(self):
self.screen.blit(self.bg, (0, 0)) self.screen.blit(self.bg, (0, 0))
def start_new_game(self):
self.paused = False
self.disable_menu()
self.need_reset = True
def restore_game(self):
self.disable_menu()
self.paused = False
def save_game(self):
pass
def load_game(self):
pass
def gameover(self): def gameover(self):
self.done = True
surf = self.font.render('Game over', True, (20, 20, 20)) surf = self.font.render('Game over', True, (20, 20, 20))
rect = surf.get_rect() rect = surf.get_rect()
rect.midtop = (390, 250) rect.midtop = (390, 250)
self.screen.blit(surf, rect) self.screen.blit(surf, rect)
pg.display.flip() pg.display.flip()
time.sleep(5) time.sleep(3)
pg.quit()
self.get_menu()
def mainloop(): def mainloop():
game = Game() game = Game()
snake =Snake(90, 300) game.game_init()
game.set_title()
game.get_menu()
snake =Snake()
foods_array = [] foods_array = []
def update_game_window(): def update_game_window():
while len(foods_array) < 2: while len(foods_array) < 2:
food = Food() food = Food()
@ -68,10 +102,20 @@ def mainloop():
game.gameover() game.gameover()
while not game.done: while not game.done:
if game.need_reset:
snake.reset()
game.start_new_game()
game.need_reset = False
for event in pg.event.get(): for event in pg.event.get():
if event.type == pg.QUIT: if event.type == pg.QUIT:
game.done = True game.done = True
pressed = pg.key.get_pressed() 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 pressed[pg.K_UP]:
if snake.direction != 'down': if snake.direction != 'down':
snake.direction = 'up' snake.direction = 'up'
@ -83,7 +127,8 @@ def mainloop():
snake.direction = 'left' snake.direction = 'left'
if pressed[pg.K_RIGHT]: if pressed[pg.K_RIGHT]:
if snake.direction != 'left': if snake.direction != 'left':
snake.direction = 'right' snake.direction = 'right'
update_game_window() update_game_window()
game.clock.tick(game.fps) game.clock.tick(game.fps)

View File

@ -4,9 +4,9 @@ import pygame as pg
class Snake: class Snake:
def __init__(self, x, y) -> None: def __init__(self, **kwargs) -> None:
self.x = x self.x = kwargs.get('x', 90)
self.y = y self.y = kwargs.get('y', 300)
self.snake_head = { self.snake_head = {
'left': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_left.png')), 'left': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_left.png')),
'right': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_right.png')), 'right': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_right.png')),
@ -24,11 +24,21 @@ class Snake:
self.direction = 'right' self.direction = 'right'
self.direction_prev = 'right' self.direction_prev = 'right'
self.length = 2 self.length = 10
self.speed = 10 # скорость движения змейки чем меньше, тем быстрее self.speed = 40 # скорость движения змейки чем меньше, тем быстрее
self.counter = 0 # Просто счетчик, для регулировки скорости нужен self.counter = 0 # Просто счетчик, для регулировки скорости нужен
self.body = [('h', 60, 300), ('h', 30, 300)] self.body = kwargs.get('body', [('h', 60, 300), ('h', 30, 300)])
self.impacted = False self.impacted = False
def reset(self):
self.x = 90
self.y = 300
self.body = [('h', 60, 300), ('h', 30, 300)]
self.direction = 'right'
self.direction_prev = 'right'
self.impacted = False
self.length = 2
self.speed = 40
def get_head_coords(self): def get_head_coords(self):
return self.x, self.y return self.x, self.y