lesson6_snake updated
parent
ec9cb808b3
commit
e11835e6de
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
|
|
||||||
pg.init()
|
pg.init()
|
||||||
|
@ -17,7 +18,11 @@ snake_head = {
|
||||||
}
|
}
|
||||||
snake_body = {
|
snake_body = {
|
||||||
'h': pg.image.load(os.path.join(os.curdir, 'img', 'body_h.png')),
|
'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'))
|
'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:
|
class Snake:
|
||||||
|
@ -25,52 +30,99 @@ class Snake:
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.direction = 'right'
|
self.direction = 'right'
|
||||||
self.length = 0
|
self.direction_prev = 'right'
|
||||||
self.speed = 10 # скорость движения змейки чем меньше, тем быстрее
|
self.length = 2
|
||||||
|
self.speed = 30 # скорость движения змейки чем меньше, тем быстрее
|
||||||
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
|
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
|
||||||
self.body = []
|
self.body = [('h', 60, 300), ('h', 30, 300)]
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, screen):
|
||||||
|
if len(self.body) > self.length:
|
||||||
|
self.body.pop()
|
||||||
|
|
||||||
if self.counter >= self.speed:
|
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:
|
match self.direction:
|
||||||
case 'left':
|
case 'left':
|
||||||
self.x -= 30
|
self.x -= 30
|
||||||
if self.x < 0:
|
if self.x < 0:
|
||||||
self.x = 750
|
self.x = 750
|
||||||
|
self.direction_prev = 'left'
|
||||||
case 'right':
|
case 'right':
|
||||||
self.x += 30
|
self.x += 30
|
||||||
if self.x > 750:
|
if self.x > 750:
|
||||||
self.x = 0
|
self.x = 0
|
||||||
|
self.direction_prev = 'right'
|
||||||
case 'up':
|
case 'up':
|
||||||
self.y -= 30
|
self.y -= 30
|
||||||
if self.y < 0:
|
if self.y < 0:
|
||||||
self.y = 570
|
self.y = 570
|
||||||
|
self.direction_prev = 'up'
|
||||||
case 'down':
|
case 'down':
|
||||||
self.y += 30
|
self.y += 30
|
||||||
if self.y > 570:
|
if self.y > 570:
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
self.direction_prev = 'down'
|
||||||
|
|
||||||
|
if (self.x, self.y) in [(x[1], x[2]) for x in self.body]:
|
||||||
|
gameover()
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
|
|
||||||
self.counter += 1
|
self.counter += 1
|
||||||
|
|
||||||
screen.blit(snake_head[self.direction], (self.x, self.y))
|
screen.blit(snake_head[self.direction], (self.x, self.y))
|
||||||
|
for el in self.body:
|
||||||
|
screen.blit(snake_body[el[0]], (el[1], el[2]))
|
||||||
|
|
||||||
snake =Snake(0, 300)
|
snake =Snake(90, 300)
|
||||||
|
|
||||||
def update_game_window():
|
def update_game_window():
|
||||||
screen.blit(bg, (0,0))
|
screen.blit(bg, (0,0))
|
||||||
snake.draw(screen)
|
snake.draw(screen)
|
||||||
pg.display.update()
|
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:
|
while not done:
|
||||||
for event in pg.event.get():
|
for event in pg.event.get():
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
done = True
|
done = True
|
||||||
pressed = pg.key.get_pressed()
|
pressed = pg.key.get_pressed()
|
||||||
if pressed[pg.K_UP]: snake.direction = 'up'
|
if pressed[pg.K_UP]:
|
||||||
if pressed[pg.K_DOWN]: snake.direction = 'down'
|
if snake.direction != 'down':
|
||||||
if pressed[pg.K_LEFT]: snake.direction = 'left'
|
snake.direction = 'up'
|
||||||
if pressed[pg.K_RIGHT]: snake.direction = 'right'
|
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()
|
update_game_window()
|
||||||
clock.tick(FPS)
|
clock.tick(FPS)
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 5.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 5.8 KiB |
Binary file not shown.
After Width: | Height: | Size: 5.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
Loading…
Reference in New Issue