Я решил сделать змейку на pygame. Всё смог написать, кроме поворота. В голове выглядит у меня это так: запоминается координата головы в момент нажатия кнопки, далее голова уходит в другую сторону, а тело поочередно вставая на место головы в момент нажатия, тоже поворачивает. Но что-то никак не получается это реализовать.
import pygame
from pygame import key
import random
import time
def motion(screen):
global snake_body,snake_head,case
snake_head[0] += snake_head[2]
snake_head[1] += snake_head[3]
for n in range(len(snake_body)):
snake_body[n][0] += snake_body[n][2]
snake_body[n][1] += snake_body[n][3]
pygame.draw.circle(screen, 'yellow', (snake_body[n][0],snake_body[n][1]), 10)
pygame.draw.circle(screen, 'yellow', (snake_head[0],snake_head[1]), 10)
food()
eating(adding)
def food():
global snake_body,snake_head,case
nose = random.choice([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15])
if nose == 1:
x = random.randrange(44,458)
y = random.randrange(44,458)
if len(case) != 3:
case.append([x, y])
def eating(thg):
global snake_body,snake_head,case
xl = snake_body[-1][0]
yl = snake_body[-1][1]
xf = snake_head[0]
yf = snake_head[1]
delete = []
for i in range(len(case)):
if xf > case[i][0] - 7 and xf < case[i][0] + 7 and yf > case[i][1] - 7 and yf < case[i][1] + 7:
if thg == 'down':
snake_body.append([xl,yl + 10,0,-1])
delete.append(i)
if thg == 'up':
snake_body.append([xl,yl - 10,0,1])
delete.append(i)
if thg == 'left':
snake_body.append([xl - 10,yl,1,0])
delete.append(i)
if thg == 'right':
snake_body.append([xl + 10,yl,-1,0])
delete.append(i)
for i in delete:
del case[i]
def lose(screen):
global snake_body,snake_head,case
case = []
font = pygame.font.Font(None,50)
textwon1 = font.render('YOU LOSED!',True,(100,255,100))
screen.blit(textwon1,(155,220))
textnewgame1 = font.render('NEW GAME',True,(100,255,100))
screen.blit(textnewgame1,(165,270))
pygame.draw.rect(screen,(100,255,100), (155,260,215,50),1)
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos[0],event.pos[1])
if 380 > event.pos[0] > 155 and 290 > event.pos[1] > 260:
snake_head = [250,250,1,0]
snake_body = [[240,250,1,0],[230,250,1,0]]
adding = 'left'
if __name__ == '__main__':
pygame.init()
size = width,height = 500,500
screen = pygame.display.set_mode(size)
running = True
clock = pygame.time.Clock()
snake_head = [250,250,1,0]
snake_body = [[240,250,1,0],[230,250,1,0]]
case = []
adding = 'left'
#задаем цикл, который выступает в качестве обработчика
while running:
#создаем эвент на закрытие программы
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and adding != 'up':
adding = 'down'
snake_head[2] = 0
snake_head[3] = -1
elif event.key == pygame.K_DOWN and adding != 'down':
adding = 'up'
snake_head[2] = 0
snake_head[3] = 1
elif event.key == pygame.K_RIGHT and adding != 'right':
adding = 'left'
snake_head[2] = 1
snake_head[3] = 0
elif event.key == pygame.K_LEFT and adding != 'left':
adding = 'right'
snake_head[2] = -1
snake_head[3] = 0
pygame.draw.rect(screen, 'white', (10,10,480,480),1)
for n in case:
pygame.draw.circle(screen, 'yellow', (n[0],n[1]), 7)
if snake_head[0] > 478 or snake_head[0] < 22 or snake_head[1] > 478 or snake_head[1] < 22:
lose(screen)
else:
motion(screen)
clock.tick(100)
pygame.display.flip()
screen.fill('black')
pygame.quit()