StudyRepo_Synergy/part2_OOP/lesson6/snake/snake.py

94 lines
3.8 KiB
Python
Raw Normal View History

2023-12-08 01:14:04 +03:00
import os
import pygame as pg
2023-12-08 01:14:04 +03:00
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')),
}
2023-12-08 01:14:04 +03:00
self.direction = 'right'
2023-12-08 23:58:33 +03:00
self.direction_prev = 'right'
self.length = 2
self.speed = 10 # скорость движения змейки чем меньше, тем быстрее
2023-12-08 01:14:04 +03:00
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
2023-12-08 23:58:33 +03:00
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]
2023-12-08 01:14:04 +03:00
def draw(self, screen):
2023-12-08 23:58:33 +03:00
if len(self.body) > self.length:
self.body.pop()
2023-12-08 01:14:04 +03:00
if self.counter >= self.speed:
2023-12-08 23:58:33 +03:00
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))
2023-12-08 01:14:04 +03:00
match self.direction:
case 'left':
self.x -= 30
if self.x < 0:
self.x = 750
2023-12-08 23:58:33 +03:00
self.direction_prev = 'left'
2023-12-08 01:14:04 +03:00
case 'right':
self.x += 30
if self.x > 750:
self.x = 0
2023-12-08 23:58:33 +03:00
self.direction_prev = 'right'
2023-12-08 01:14:04 +03:00
case 'up':
self.y -= 30
if self.y < 0:
self.y = 570
2023-12-08 23:58:33 +03:00
self.direction_prev = 'up'
2023-12-08 01:14:04 +03:00
case 'down':
self.y += 30
if self.y > 570:
self.y = 0
2023-12-08 23:58:33 +03:00
self.direction_prev = 'down'
if (self.x, self.y) in [(x[1], x[2]) for x in self.body]:
self.impacted = True
2023-12-08 01:14:04 +03:00
self.counter = 0
2023-12-08 23:58:33 +03:00
2023-12-08 01:14:04 +03:00
self.counter += 1
screen.blit(self.snake_head[self.direction], (self.x, self.y))
2023-12-08 23:58:33 +03:00
for el in self.body:
screen.blit(self.snake_body[el[0]], (el[1], el[2]))
2023-12-08 01:14:04 +03:00