StudyRepo_Synergy/part2_OOP/lesson6/snake/snake.py

104 lines
4.1 KiB
Python

import os
import pygame as pg
class Snake:
def __init__(self, **kwargs) -> None:
self.x = kwargs.get('x', 90)
self.y = kwargs.get('y', 300)
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 = 10
self.speed = 40 # скорость движения змейки чем меньше, тем быстрее
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
self.body = kwargs.get('body', [('h', 60, 300), ('h', 30, 300)])
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):
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:
self.body.pop()
if self.counter >= self.speed:
if self.direction == self.direction_prev:
if self.direction in ('right', 'left'):
self.body.insert(0, ('h', self.x, self.y))
else:
self.body.insert(0, ('v', self.x, self.y))
elif (self.direction_prev, self.direction) in [('right', 'up'), ('down', 'left')]:
self.body.insert(0, ('ru_dl', self.x, self.y))
elif (self.direction_prev, self.direction) in [('right', 'down'), ('up', 'left')]:
self.body.insert(0, ('rd_ul', self.x, self.y))
elif (self.direction_prev, self.direction) in [('left', 'up'), ('down', 'right')]:
self.body.insert(0, ('lu_dr', self.x, self.y))
elif (self.direction_prev, self.direction) in [('left', 'down'), ('up', 'right')]:
self.body.insert(0, ('ld_ur', self.x, self.y))
match self.direction:
case 'left':
self.x -= 30
if self.x < 0:
self.x = 750
self.direction_prev = 'left'
case 'right':
self.x += 30
if self.x > 750:
self.x = 0
self.direction_prev = 'right'
case 'up':
self.y -= 30
if self.y < 0:
self.y = 570
self.direction_prev = 'up'
case 'down':
self.y += 30
if self.y > 570:
self.y = 0
self.direction_prev = 'down'
if (self.x, self.y) in [(x[1], x[2]) for x in self.body]:
self.impacted = True
self.counter = 0
self.counter += 1
screen.blit(self.snake_head[self.direction], (self.x, self.y))
for el in self.body:
screen.blit(self.snake_body[el[0]], (el[1], el[2]))