Compare commits
2 Commits
f9ac8c343b
...
fd9792aa95
Author | SHA1 | Date |
---|---|---|
Сергей Ванюшкин | fd9792aa95 | |
Сергей Ванюшкин | 33fdd19ff5 |
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 6.3 KiB |
|
@ -2,8 +2,7 @@ import os
|
|||
import time
|
||||
|
||||
import pygame as pg
|
||||
from pygame.key import name
|
||||
import pygame_menu as pgm
|
||||
import pygame_menu
|
||||
|
||||
from .snake import Snake
|
||||
from .food import Food
|
||||
|
@ -28,26 +27,29 @@ class Game:
|
|||
self.menu = None
|
||||
self.snake = Snake()
|
||||
self.foods_array = []
|
||||
self.game_speed = 0
|
||||
|
||||
def game_init(self):
|
||||
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 = pygame_menu.Menu('Snakessss game', 400, 300, theme=pygame_menu.themes.THEME_BLUE)
|
||||
self.menu.add.button('Start new game', self.start_new_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.add.selector('Speed', [('Slow', 0), ('Medium', 1), ('Fast', 2)], onchange=self.set_speed, onreturn=self.restore_game)
|
||||
self.menu._widgets[-1].set_value(self.game_speed)
|
||||
self.menu.add.button('Exit', 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
|
||||
if self.menu is not None:
|
||||
self.menu.disable()
|
||||
|
||||
def set_speed(self, tuple_celected, difficulty):
|
||||
self.game_speed = difficulty
|
||||
self.snake.set_speed(speed=self.game_speed)
|
||||
|
||||
def set_title(self):
|
||||
pg.display.set_caption(self.title)
|
||||
|
@ -56,14 +58,19 @@ class Game:
|
|||
self.screen.blit(self.bg, (0, 0))
|
||||
|
||||
def start_new_game(self):
|
||||
print(self.game_speed, self.snake.speed)
|
||||
self.started = True
|
||||
self.paused = False
|
||||
self.disable_menu()
|
||||
self.need_reset = True
|
||||
|
||||
def restore_game(self):
|
||||
self.disable_menu()
|
||||
self.paused = False
|
||||
def restore_game(self, *args, **kwargs):
|
||||
if self.started:
|
||||
self.disable_menu()
|
||||
self.paused = False
|
||||
else:
|
||||
self.start_new_game()
|
||||
|
||||
|
||||
def save_game(self):
|
||||
pass
|
||||
|
@ -72,10 +79,14 @@ class Game:
|
|||
pass
|
||||
|
||||
def gameover(self):
|
||||
surf = self.font.render('Game over', True, (20, 20, 20))
|
||||
surf = self.font.render('GAME OVER', True, (20, 20, 20))
|
||||
rect = surf.get_rect()
|
||||
score = self.font.render(f'Score: {self.score}', True, (20, 20, 20))
|
||||
rect2 = score.get_rect()
|
||||
rect.midtop = (390, 250)
|
||||
rect2.midtop = (390, 320)
|
||||
self.screen.blit(surf, rect)
|
||||
self.screen.blit(score, rect2)
|
||||
pg.display.flip()
|
||||
time.sleep(3)
|
||||
|
||||
|
@ -89,6 +100,7 @@ class Game:
|
|||
|
||||
for f in self.foods_array:
|
||||
if self.snake.get_head_coords() == f.get_coords():
|
||||
self.score += 5 * self.snake.length
|
||||
self.foods_array.remove(f)
|
||||
self.snake.length += 1
|
||||
|
||||
|
@ -99,23 +111,24 @@ class Game:
|
|||
|
||||
pg.display.update()
|
||||
|
||||
|
||||
if self.snake.impacted:
|
||||
self.gameover()
|
||||
self.started = False
|
||||
self.gameover()
|
||||
|
||||
|
||||
def mainloop(self):
|
||||
while not self.done:
|
||||
if self.need_reset:
|
||||
self.snake.reset()
|
||||
self.snake.reset(speed=self.game_speed)
|
||||
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:
|
||||
|
@ -124,12 +137,15 @@ class Game:
|
|||
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'
|
Before Width: | Height: | Size: 1013 KiB After Width: | Height: | Size: 1013 KiB |
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 5.3 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
Before Width: | Height: | Size: 5.8 KiB After Width: | Height: | Size: 5.8 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
|
@ -24,13 +24,16 @@ class Snake:
|
|||
|
||||
self.direction = 'right'
|
||||
self.direction_prev = 'right'
|
||||
self.length = 10
|
||||
self.speed = 40 # скорость движения змейки чем меньше, тем быстрее
|
||||
self.length = 2
|
||||
self.speed = 30 # скорость движения змейки чем меньше, тем быстрее
|
||||
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
|
||||
self.body = kwargs.get('body', [('h', 60, 300), ('h', 30, 300)])
|
||||
self.impacted = False
|
||||
|
||||
def reset(self):
|
||||
def set_speed(self, **kwargs):
|
||||
self.speed = (30, 20, 10)[kwargs.get('speed', 30)]
|
||||
|
||||
def reset(self, **kwargs):
|
||||
self.x = 90
|
||||
self.y = 300
|
||||
self.body = [('h', 60, 300), ('h', 30, 300)]
|
||||
|
@ -38,7 +41,7 @@ class Snake:
|
|||
self.direction_prev = 'right'
|
||||
self.impacted = False
|
||||
self.length = 2
|
||||
self.speed = 40
|
||||
self.set_speed(**kwargs)
|
||||
|
||||
def get_head_coords(self):
|
||||
return self.x, self.y
|
1326
information_system_and_programming/term_5/db_management_and_automation/poetry.lock
generated
Normal file
|
@ -0,0 +1,25 @@
|
|||
[tool.poetry]
|
||||
name = "servicemanager"
|
||||
version = "0.0.1"
|
||||
description = "Computer equipment prevention and maintenance management system"
|
||||
authors = ["Sergey Vanyushkin <pi3c@yandex.ru>"]
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
fastapi = {extras = ["all"], version = "^0.108.0"}
|
||||
SQLAlchemy = "^2.0.25"
|
||||
asyncpg = "^0.29.0"
|
||||
pydantic-settings = "^2.1.0"
|
||||
uvicorn = "^0.25.0"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
pytest = "^7.4.4"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
app = "servicemanager.main:run_app"
|
|
@ -0,0 +1,4 @@
|
|||
[pytest]
|
||||
pythonpath = . servicemanager
|
||||
; env_files =
|
||||
; .test.env
|
|
@ -0,0 +1,11 @@
|
|||
from fastapi import APIRouter
|
||||
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@api_router.get("/ping")
|
||||
async def pong():
|
||||
"""Тестовый роут"""
|
||||
return {"ping": "pong!"}
|
||||
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import os
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
|
||||
site_router = APIRouter()
|
||||
print(os.path.abspath(os.curdir))
|
||||
site_router.mount("/static", StaticFiles(directory="servicemanager/frontend/static"), name="static")
|
||||
|
||||
|
||||
templates = Jinja2Templates(directory="servicemanager/frontend/templates")
|
||||
|
||||
|
||||
@site_router.get("/testpage", response_class=HTMLResponse)
|
||||
async def read_item(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request=request, name="test.html"
|
||||
)
|
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>test page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>test_data</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,44 @@
|
|||
import asyncio
|
||||
import sys
|
||||
|
||||
from fastapi import FastAPI
|
||||
import uvicorn
|
||||
|
||||
from api.routes import api_router
|
||||
from frontend.routes import site_router
|
||||
|
||||
async def generate_test_data():
|
||||
"""
|
||||
Создание БД и наполнение ее данными
|
||||
"""
|
||||
print('generating data')
|
||||
|
||||
def create_app():
|
||||
"""
|
||||
Создание экземпляра приложения FastAPI и врзврат его
|
||||
"""
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(api_router)
|
||||
app.include_router(site_router)
|
||||
|
||||
return app
|
||||
|
||||
def run_app():
|
||||
"""
|
||||
Запуск локального вебсервера для тестов и проверки
|
||||
"""
|
||||
uvicorn.run(
|
||||
app="main:create_app",
|
||||
reload=True,
|
||||
factory=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if "--configure" in sys.argv:
|
||||
asyncio.run(generate_test_data())
|
||||
|
||||
if "--webserver" in sys.argv:
|
||||
run_app()
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
DB_HOST: str
|
||||
DB_PORT: int
|
||||
DB_USER: str
|
||||
DB_PASS: str
|
||||
DB_NAME: str
|
||||
|
||||
@property
|
||||
def DATABASE_URL_asyncpg(self):
|
||||
"""
|
||||
Возвращает строку подключения к БД необходимую для SQLAlchemy
|
||||
"""
|
||||
return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env")
|
||||
|
||||
settings = Settings()
|
|
@ -0,0 +1,10 @@
|
|||
from starlette.testclient import TestClient
|
||||
|
||||
from servicemanager.main import create_app
|
||||
|
||||
|
||||
client = TestClient(create_app())
|
||||
|
||||
def test_testpage():
|
||||
response = client.get("/testpage")
|
||||
assert response.status_code == 200
|
|
@ -0,0 +1,11 @@
|
|||
from starlette.testclient import TestClient
|
||||
|
||||
from servicemanager.main import create_app
|
||||
|
||||
|
||||
client = TestClient(create_app())
|
||||
|
||||
def test_ping():
|
||||
response = client.get("/ping")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"ping": "pong!"}
|