i am trying to make a snake game in python but can not find out how to make it so that the snake continues moving without the player inputting so I would need to cancel the input after 2 seconds if anyone know an easy way to do this it would be greatly appreciated
the code reads:
import random
import sys
playerY = (1)
playerX= (0)
appleY= random.randint(1,10)
appleX= random.randint(0,9)
length = 1
oldplaceY = [123]
oldplaceX = [123]
while 1 == 1:
board= ["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"],["-","-","-","-","-","-","-","-","-","-","-"]
board[playerX][playerY]="0"
board[appleX][appleY]="🍎"
for x in range (1,length):
x1 =int(oldplaceX[x])
y1 =int(oldplaceY[x])
board [x1][y1]= "o"
for x in range (1,11):
print (board[0][x],board[1][x],board[2][x],board[3][x],board[4][x],board[5][x],board[6][x],board[7][x],board[8][x],board[9][x])
move = input("w,a,s,d or x to finish or e to call for help ")
if move== "w":
playerY= playerY - 1
elif move == "a":
playerX= playerX - 1
elif move == "s":
playerY= playerY + 1
elif move == "d":
playerX= playerX + 1
elif move == "e":
appleY= random.randint(1,10)
appleX= random.randint(0,9)
elif move == "x":
sys.exit()
if playerX == appleX and playerY == appleY:
appleY= random.randint(1,10)
appleX= random.randint(0,9)
length = length+1
oldplaceY.insert(0,playerY)
oldplaceX.insert(0,playerX)
there are no errors
the input I would like to stop after 2 seconds is
move = input(“w,a,s,d or x to finish or e to call for help “)`
i have tried making it without the snake continuing to move and succeeded but it leads to a fairly boring game
Austen Blanch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.