Начало разработки курсового проекта

develop
Сергей Ванюшкин 2024-01-05 01:14:10 +03:00
parent 33fdd19ff5
commit fd9792aa95
58 changed files with 39 additions and 20 deletions

View File

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -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'

View File

Before

Width:  |  Height:  |  Size: 1013 KiB

After

Width:  |  Height:  |  Size: 1013 KiB

View File

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -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