77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
import os
|
|
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'))
|
|
}
|
|
|
|
class Snake:
|
|
def __init__(self, x, y) -> None:
|
|
self.x = x
|
|
self.y = y
|
|
self.direction = 'right'
|
|
self.length = 0
|
|
self.speed = 10 # скорость движения змейки чем меньше, тем быстрее
|
|
self.counter = 0 # Просто счетчик, для регулировки скорости нужен
|
|
self.body = []
|
|
|
|
def draw(self, screen):
|
|
if self.counter >= self.speed:
|
|
match self.direction:
|
|
case 'left':
|
|
self.x -= 30
|
|
if self.x < 0:
|
|
self.x = 750
|
|
case 'right':
|
|
self.x += 30
|
|
if self.x > 750:
|
|
self.x = 0
|
|
case 'up':
|
|
self.y -= 30
|
|
if self.y < 0:
|
|
self.y = 570
|
|
case 'down':
|
|
self.y += 30
|
|
if self.y > 570:
|
|
self.y = 0
|
|
self.counter = 0
|
|
self.counter += 1
|
|
|
|
screen.blit(snake_head[self.direction], (self.x, self.y))
|
|
|
|
snake =Snake(0, 300)
|
|
|
|
def update_game_window():
|
|
screen.blit(bg, (0,0))
|
|
snake.draw(screen)
|
|
pg.display.update()
|
|
|
|
|
|
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]: snake.direction = 'up'
|
|
if pressed[pg.K_DOWN]: snake.direction = 'down'
|
|
if pressed[pg.K_LEFT]: snake.direction = 'left'
|
|
if pressed[pg.K_RIGHT]: snake.direction = 'right'
|
|
|
|
update_game_window()
|
|
clock.tick(FPS)
|