diff --git a/part2_OOP/lesson6/app.py b/part2_OOP/lesson6/app.py new file mode 100644 index 0000000..ed8e376 --- /dev/null +++ b/part2_OOP/lesson6/app.py @@ -0,0 +1,4 @@ +from snake import game + +if __name__ == "__main__": + game.mainloop() diff --git a/part2_OOP/lesson6/snake/__init__.py b/part2_OOP/lesson6/snake/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/part2_OOP/lesson6/snake/config.py b/part2_OOP/lesson6/snake/config.py new file mode 100644 index 0000000..16a5d8e --- /dev/null +++ b/part2_OOP/lesson6/snake/config.py @@ -0,0 +1,7 @@ +class GameConfig: + pass + + +class SnakeConfig: + pass + diff --git a/part2_OOP/lesson6/snake/food.py b/part2_OOP/lesson6/snake/food.py new file mode 100644 index 0000000..1ae21f3 --- /dev/null +++ b/part2_OOP/lesson6/snake/food.py @@ -0,0 +1,25 @@ +import os +import random + +import pygame as pg + + +class Food: + FOOD = ( + pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'mouse1.png')), + pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'mouse2.png')) + ) + + def __init__(self) -> None: + self.x = random.randint(0, 26) * 30 + self.y = random.randint(0, 20) * 30 + self.type = random.choice(self.FOOD) + + + def draw(self, screen): + screen.blit(self.type, (self.x, self.y)) + + def get_coords(self): + return self.x, self.y + + diff --git a/part2_OOP/lesson6/snake/game.py b/part2_OOP/lesson6/snake/game.py new file mode 100644 index 0000000..9bdcc45 --- /dev/null +++ b/part2_OOP/lesson6/snake/game.py @@ -0,0 +1,89 @@ +import os +import time +from random import choice + +import pygame as pg + +from .snake import Snake +from .food import Food + +class Game: + def __init__(self) -> None: + self.screen_widht = 780 + self.screen_hight = 600 + self.clock = pg.time.Clock() + pg.font.init() + self.font = pg.font.SysFont(choice(pg.font.get_fonts()), 72) + self.score = 0 + + self.title = 'Snakessss' + self.done = False + 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.fps = 60 + + def game_init(self): + pg.init() + + def set_title(self): + pg.display.set_caption(self.title) + + def set_bg(self): + self.screen.blit(self.bg, (0, 0)) + + def gameover(self): + self.done = True + surf = self.font.render('Game over', True, (20, 20, 20)) + rect = surf.get_rect() + rect.midtop = (390, 250) + self.screen.blit(surf, rect) + pg.display.flip() + time.sleep(5) + pg.quit() + + +def mainloop(): + game = Game() + snake =Snake(90, 300) + foods_array = [] + 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: + for event in pg.event.get(): + if event.type == pg.QUIT: + game.done = True + pressed = pg.key.get_pressed() + 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) diff --git a/part2_OOP/lesson6/img/bg.png b/part2_OOP/lesson6/snake/img/bg.png similarity index 100% rename from part2_OOP/lesson6/img/bg.png rename to part2_OOP/lesson6/snake/img/bg.png diff --git a/part2_OOP/lesson6/img/body_h.png b/part2_OOP/lesson6/snake/img/body_h.png similarity index 100% rename from part2_OOP/lesson6/img/body_h.png rename to part2_OOP/lesson6/snake/img/body_h.png diff --git a/part2_OOP/lesson6/img/body_ld_ur.png b/part2_OOP/lesson6/snake/img/body_ld_ur.png similarity index 100% rename from part2_OOP/lesson6/img/body_ld_ur.png rename to part2_OOP/lesson6/snake/img/body_ld_ur.png diff --git a/part2_OOP/lesson6/img/body_lu_dr.png b/part2_OOP/lesson6/snake/img/body_lu_dr.png similarity index 100% rename from part2_OOP/lesson6/img/body_lu_dr.png rename to part2_OOP/lesson6/snake/img/body_lu_dr.png diff --git a/part2_OOP/lesson6/img/body_rd_ul.png b/part2_OOP/lesson6/snake/img/body_rd_ul.png similarity index 100% rename from part2_OOP/lesson6/img/body_rd_ul.png rename to part2_OOP/lesson6/snake/img/body_rd_ul.png diff --git a/part2_OOP/lesson6/img/body_ru_dl.png b/part2_OOP/lesson6/snake/img/body_ru_dl.png similarity index 100% rename from part2_OOP/lesson6/img/body_ru_dl.png rename to part2_OOP/lesson6/snake/img/body_ru_dl.png diff --git a/part2_OOP/lesson6/img/body_v.png b/part2_OOP/lesson6/snake/img/body_v.png similarity index 100% rename from part2_OOP/lesson6/img/body_v.png rename to part2_OOP/lesson6/snake/img/body_v.png diff --git a/part2_OOP/lesson6/snake/img/mouse1.png b/part2_OOP/lesson6/snake/img/mouse1.png new file mode 100644 index 0000000..73e776a Binary files /dev/null and b/part2_OOP/lesson6/snake/img/mouse1.png differ diff --git a/part2_OOP/lesson6/snake/img/mouse2.png b/part2_OOP/lesson6/snake/img/mouse2.png new file mode 100644 index 0000000..7b57c26 Binary files /dev/null and b/part2_OOP/lesson6/snake/img/mouse2.png differ diff --git a/part2_OOP/lesson6/img/snake_down.png b/part2_OOP/lesson6/snake/img/snake_down.png similarity index 100% rename from part2_OOP/lesson6/img/snake_down.png rename to part2_OOP/lesson6/snake/img/snake_down.png diff --git a/part2_OOP/lesson6/img/snake_left.png b/part2_OOP/lesson6/snake/img/snake_left.png similarity index 100% rename from part2_OOP/lesson6/img/snake_left.png rename to part2_OOP/lesson6/snake/img/snake_left.png diff --git a/part2_OOP/lesson6/img/snake_right.png b/part2_OOP/lesson6/snake/img/snake_right.png similarity index 100% rename from part2_OOP/lesson6/img/snake_right.png rename to part2_OOP/lesson6/snake/img/snake_right.png diff --git a/part2_OOP/lesson6/img/snake_up.png b/part2_OOP/lesson6/snake/img/snake_up.png similarity index 100% rename from part2_OOP/lesson6/img/snake_up.png rename to part2_OOP/lesson6/snake/img/snake_up.png diff --git a/part2_OOP/lesson6/game.py b/part2_OOP/lesson6/snake/snake.py similarity index 51% rename from part2_OOP/lesson6/game.py rename to part2_OOP/lesson6/snake/snake.py index 81e4777..8e20498 100644 --- a/part2_OOP/lesson6/game.py +++ b/part2_OOP/lesson6/snake/snake.py @@ -1,40 +1,40 @@ import os -import time + import pygame as pg -pg.init() -pg.display.set_caption("First Game") -screen = pg.display.set_mode((780, 600)) -done = False - -FPS = 60 # Создаем переменную FPS -clock = pg.time.Clock() # Создаем счетчик для FPS -bg = pg.image.load(os.path.join(os.curdir, 'img', 'bg.png')) -snake_head = { - 'left': pg.image.load(os.path.join(os.curdir, 'img', 'snake_left.png')), - 'right': pg.image.load(os.path.join(os.curdir, 'img', 'snake_right.png')), - 'up': pg.image.load(os.path.join(os.curdir, 'img', 'snake_up.png')), - 'down': pg.image.load(os.path.join(os.curdir, 'img', 'snake_down.png')), -} -snake_body = { - 'h': pg.image.load(os.path.join(os.curdir, 'img', 'body_h.png')), - 'v': pg.image.load(os.path.join(os.curdir, 'img', 'body_v.png')), - 'ru_dl': pg.image.load(os.path.join(os.curdir, 'img', 'body_ru_dl.png')), - 'rd_ul': pg.image.load(os.path.join(os.curdir, 'img', 'body_rd_ul.png')), - 'lu_dr': pg.image.load(os.path.join(os.curdir, 'img', 'body_lu_dr.png')), - 'ld_ur': pg.image.load(os.path.join(os.curdir, 'img', 'body_ld_ur.png')), -} class Snake: def __init__(self, x, y) -> None: self.x = x self.y = y + self.snake_head = { + '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')), + 'up': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_up.png')), + 'down': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'snake_down.png')), + } + self.snake_body = { + 'h': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_h.png')), + 'v': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_v.png')), + 'ru_dl': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_ru_dl.png')), + 'rd_ul': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_rd_ul.png')), + 'lu_dr': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_lu_dr.png')), + 'ld_ur': pg.image.load(os.path.join(os.curdir, 'snake', 'img', 'body_ld_ur.png')), + } + self.direction = 'right' self.direction_prev = 'right' - self.length = 2 - self.speed = 30 # скорость движения змейки чем меньше, тем быстрее + self.length = 2 + self.speed = 10 # скорость движения змейки чем меньше, тем быстрее self.counter = 0 # Просто счетчик, для регулировки скорости нужен self.body = [('h', 60, 300), ('h', 30, 300)] + self.impacted = False + + def get_head_coords(self): + return self.x, self.y + + def get_coords(self): + return [(self.x, self.y)] + [(el[1], el[2]) for el in self.body] def draw(self, screen): if len(self.body) > self.length: @@ -80,49 +80,14 @@ class Snake: self.direction_prev = 'down' if (self.x, self.y) in [(x[1], x[2]) for x in self.body]: - gameover() + self.impacted = True + self.counter = 0 self.counter += 1 - screen.blit(snake_head[self.direction], (self.x, self.y)) + screen.blit(self.snake_head[self.direction], (self.x, self.y)) for el in self.body: - screen.blit(snake_body[el[0]], (el[1], el[2])) + screen.blit(self.snake_body[el[0]], (el[1], el[2])) -snake =Snake(90, 300) -def update_game_window(): - screen.blit(bg, (0,0)) - snake.draw(screen) - pg.display.update() - -def gameover(): - go_font = pg.font.SysFont('monaco', 72) - go_surf = go_font.render('Game over', True, (20, 20, 20)) - go_rect = go_surf.get_rect() - go_rect.midtop = (360, 15) - screen.blit(go_surf, go_rect) - pg.display.flip() - time.sleep(5) - pg.quit() - -while not done: - for event in pg.event.get(): - if event.type == pg.QUIT: - done = True - pressed = pg.key.get_pressed() - 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() - clock.tick(FPS)