0

I am making a snake game, and if I don't hold a key or constantly move my mouse over the window, the snake stops and waits for another input to start moving again.

import pygame, sys
from pygame.locals import *
from sys import exit
import keyboard
import random
import time

class body:
    def __init__(self,properties=[0,0,20,20],colour=1):
        self.properties=properties
        self.colour=colour
        self.next=None

class fruit:
    def __init__(self,centre=[0,0],size=10):
        self.centre=centre
        self.size=size

def drawSnake(window,snake):
    temp=snake
    while temp:
        # Alternate snake colour
        if temp.colour==-1:
            colour=(0,150,0)
        else:
            colour=(0,100,0)
        temp.colour=temp.colour*-1
        pygame.draw.rect(window,colour,temp.properties)
        temp=temp.next
    return snake

def drawApple(window,snake,size):
    numApples=500/(size*2)
    bound=numApples-1
    apple=fruit([(random.randint(0,bound)*(500/numApples))+size,(random.randint(0,bound)*(500/numApples))+size],size)
    #apple=fruit([290,250],10)
    pygame.draw.circle(window,"red",apple.centre,apple.size)
    return apple

def newGame():
    # Draw initial snake and apple
    window.fill((255, 255, 255))
    snake=body([240,240,20,20],-1)
    snake=drawSnake(window,snake)
    apple=drawApple(window,snake,10)
    return snake,apple

def die(snake):
    pygame.draw.rect(window,(180,0,0),[snake.properties[0],snake.properties[1],snake.properties[2],snake.properties[3]])
    pygame.display.update()
    time.sleep(1)

def move(snake,apple,direction,length):
    # New body piece location
    x=snake.properties[0]+direction[0]
    y=snake.properties[1]+direction[1]
    # If snake crashed, restart
    if x<0 or y<0 or x>480 or y>480:
        die(snake)
        snake,apple=newGame()
        return snake,apple,0
    # Check if collision with body
    temp=snake
    '''
    while temp:
        if temp.properties[0]==x and temp.properties[1]==y:
            snake,apple=newGame()
            return snake,apple,0
        temp=temp.next
    '''
    # Create new body piece with other colour and add to front of list
    newBody=body([x,y,20,20],snake.colour*(-1))
    newBody.next=snake
    snake=newBody
    # If apple is eaten
    if [x,y]==[apple.centre[0]-10,apple.centre[1]-10]:
        # Add 1 to length, spawn new apple, do not remove end body piece
        length+=1
        apple=drawApple(window,snake,10)
        while temp:
            # Check if apple spawned in body
            if temp.properties[0]==apple.centre[0]-10 and temp.properties[1]==apple.centre[1]-10:
                apple=drawApple(window,snake,10)
                temp=snake.next
            temp=temp.next
    else:
        # Remove end body piece
        temp=snake
        while temp.next:
            # Check if collision with body
            if temp.next.properties[0]==x and temp.next.properties[1]==y:
                die(snake)
                snake,apple=newGame()
                return snake,apple,0
            previous=temp
            temp=temp.next
        pygame.draw.rect(window,"white",temp.properties)
        previous.next=None
    return snake,apple,length

# Make window
pygame.init()
window=pygame.display.set_mode((500, 500))

pressed_keys=pygame.key.get_pressed()
snake,apple=newGame()
length=0
direction=[0,-20]
delay=0.1

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        pressed_keys=pygame.key.get_pressed()
        if pressed_keys[K_w]:
            if direction!=[0,20]:
                direction=[0,-20]
        elif pressed_keys[K_a]:
            if direction!=[20,0]:
                direction=[-20,0]
        elif pressed_keys[K_s]:
            if direction!=[0,-20]:
                direction=[0,20]
        elif pressed_keys[K_d]:
            if direction!=[-20,0]:
                direction=[20,0]
        snake,apple,length=move(snake,apple,direction,length)
        #delay=1/(length+1)
        snake=drawSnake(window,snake)
        time.sleep(delay)
        pygame.display.update()
        pygame.event.pump()

I want to only have to press a key to change the snake's direction and then have it always move, even if I do not give it any input. Is there a way to make it stop pausing?

DumDog6
  • 13
  • 2
  • It is a matter of indentation [Indentation](https://docs.python.org/3/reference/lexical_analysis.html). `pygame.key.get_pressed()` is not an event. You have to do that part of the code in the application loop not in the event loop. See [How can I make a sprite move when key is held down](https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down). – Rabbid76 Oct 29 '22 at 14:56

0 Answers0