Я сделал эту игру со змеями (с помощью обучающих программ) и изменил форму арены. Я хочу напечатать направление нажатой клавиши, пока змея не изменит направление, если это имеет смысл. Исходя из этого, моей целью было переделать путь змеи, чтобы он отслеживал общее движение змеи в конце вашей игры (если у вас есть более интересные идеи для этого, пожалуйста, помогите). Я думал, что функция "while" подойдет для этого в качестве первого шага, однако, похоже, она не работает. Если бы кто-то мог помочь, это было бы замечательно. Всего наилучшего.
import pygame
import time
import random
import threading
pygame.init()
from random import choice
blue = (0,0,255)
purple = (155, 0 , 155)
green = (0,225,200)
red = (255,0,0)
black = (0,0,0)
white = (255, 255, 255)
brown = (200, 180, 0)
dbrown = (180, 180, 0)
cream = (255, 255, 180)
screen = pygame.display.init()
dis_width = 840
dis_height = 640
dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('patricks best attempt at making a snake game')
clock = pygame.time.Clock()
snake_block = 10
dis_bed = 200 - 240
clock = pygame.time.Clock()
snake_speed = 20
font_style = pygame.font.SysFont(None, 50)
xchc= choice([i for i in range(1,840) if i not in [200,210,220,230,240]])
score_font = pygame.font.SysFont("bahnschrift", 35)
def Your_score(score):
value = score_font.render("your score" + str(score), True, black)
dis.blit(value, [0,0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis,blue, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width/3, dis_height/2])
def gameLoop():
game_over = False
game_close = False
x1 = 10
y1 = dis_height -10
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
while not game_over:
while game_close == True:
dis.fill(white)
message("You lost q to quit, c to play", red)
Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
while event.key == pygame.K_LEFT:
print("Left")
if event.key != pygame.K_LEFT:
break
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
print ("right")
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
print ("up")
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
print ("down")
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
elif 0 <= y1 and y1 <= 310 and 200 <= x1 and x1 <= 420:
game_close = True
elif 0 <= y1 and y1 <= 40 and 100 <= x1 and x1 <= 180:
game_close = True
elif 0 <= y1 and y1 <= 100 and 520 <= x1 and x1 <= 700:
game_close = True
elif 560 <= y1 and y1 <= 640 and 640 <= x1 and x1 <= 840:
game_close = True
elif 520 <= y1 and y1 <= 640 and 260 <= x1 and x1 <= 510:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(cream)
pygame.draw.rect(dis,black,[foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
pygame.draw.rect(dis, blue, [x1, y1, snake_block, snake_block])
pygame.draw.rect(dis, purple, [200,0,240,320])
pygame.draw.rect(dis, brown, [100, 0, 80, 40])
pygame.draw.rect(dis, dbrown, [520, 0, 180, 100])
pygame.draw.rect(dis, brown, [640, 560, 200, 80])
pygame.draw.rect(dis, dbrown, [260, 520, 260, 120])
pygame.display.update()
if x1 == foodx and y1 ==foody:
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 1
elif foodx in range(200,440) and foody in range(0,320):
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 0
elif foodx in range(100, 180) and foody in range(0,40):
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 0
elif foodx in range(520, 700) and foody in range(0,100):
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 0
elif foodx in range(640, 840) and foody in range(560, 640):
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 0
elif foodx in range(260, 520) and foody in range(520, 640):
foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
Length_of_snake += 0
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()