1

i am new to python programming and now starting to write simple "Snake" game. i am trying to make the head of the snake move around the screen in a way that when an arrow key is pressed, the head should move in that direction non-stop, until a perpendicular arrow is pressed. i cant make the head of the snake ( a rectangle) to move non stop in single direction slowly. i tried to use time.sleep() --> it made the whole code stuck. i tried to use for loop --> it just made the rectangle to transport to the other direction fast.

this is my main function:

while not GameOver:

    #create time delay of 10 milliseconds
    pygame.time.delay(10)
    # Get all of the events on the game's screen [all of the user's input]
    # Loop on the user's activity and look for exit button pressing.
    for event in pygame.event.get():
        # If the user presses the X button:
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    # Define a shortcut name to "get_pressed" method in pygame:
    keys = pygame.key.get_pressed()

    # defining x location
    x = head_position[0]
    y = head_position[1]
    # if he pressed left key
    if keys[pygame.K_LEFT]:
        # reducing location by constant
                head_position[0] -= head_speed

    elif keys[pygame.K_RIGHT]:
        # increacing location by constant
            head_position[0] += head_speed


    elif keys[pygame.K_UP]:
        # increacing location by constant
            head_position[1] -= head_speed


    elif keys[pygame.K_DOWN]:
        # increacing location by constant
            head_position[1] += head_speed

    #If the head passes the screen Boundaries, it would pop in the other side.
    if head_position[0] >= WIDTH:
        head_position[0] = 0

    elif head_position[0] < 0:
        head_position[0] = WIDTH-head_size

    elif head_position[1] >= HEIGHT:
        head_position[1] = 0

    elif head_position[1] < 0:
        head_position[1] = HEIGHT-head_size
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
levi
  • 43
  • 1
  • 6

2 Answers2

0

Use the pygame.time module to control the frames per second. Generate a pygame.time.Clock object. If you pass the optional framerate argument to pygame.time.Clock.tick, the function will delay to keep the game running slower than the given ticks per second:

clock = pygame.time.Clock()

while not GameOver:
    clock.tick(10) # 10: the game runs with 10 frames per second

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • thank you for your comment! actually i have tried it, and it is not making things better. more so, i couldn't figure out a way to make the object move slowly in one direction continuously after one arrow key press. [ i have managed to only make it move constant amount of frames in every arrow key press.] – levi Oct 04 '20 at 07:43
  • @levi This is not a comment it is an answer. If it doesn't make things better, then I've misunderstood your question. Your question is not clear and ambiguous . – Rabbid76 Oct 04 '20 at 07:50
0

if you want to make it move at a constant speed in one direction after one key press then make a right = False varible then make a key[pygame.K_YOURCHOICE]: right = True

if right: head_pos[1 or 0] -= head_speed