1

I was learning python and trying to make snake game using pygame library. I made a rectangle which moves using the pressing of keys but rather than moving one block it moves to the end of the block. Whenever i press in any direction whether it be up , left, right, or down rather than moving 1 box size it moves to the end of the other direction Could someone tell me why that happens.

import pygame


box_size = 50
num_box = 15
color1 = (169, 215, 81)
color2 = (169,250,81)
x = 0
y = 0

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            break

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        if x < width - box_size:
            x = x + box_size

    if keys[pygame.K_LEFT]:
        if x > 0:
            x = x - box_size

    if keys[pygame.K_UP]:
        if y > 0:
            y = y - box_size

    if keys[pygame.K_DOWN]:
        if y < height - box_size:
            y = y + box_size


    print(x,y)
    for i in range(0,num_box):
        if i %2 == 0:
            for j in range(0,num_box):
                if j%2 == 0:
                    pygame.draw.rect(win, color1, (box_size*j, box_size*i, box_size, box_size))
                else:
                    pygame.draw.rect(win, color2, (box_size * j, box_size * i, box_size, box_size))

        else:
            for k in range(0,num_box):
                if k%2 == 0:
                    pygame.draw.rect(win, color2, (box_size*k, box_size*i, box_size, box_size))
                else:
                    pygame.draw.rect(win, color1, (box_size * k, box_size * i, box_size, box_size))
    pygame.draw.rect(win, (0, 0, 250), (x, y, box_size, box_size))
   
    # # rect(surface, color, (left, top, width, height))


    pygame.display.update()
    pass
Azan
  • 35
  • 4
  • 1
    No experience in pygame, but it seems like you do not have any logic for game speed or update ticks. Most likely your `while True` runs thousands of times per second, meaning even a short keypress runs multiple times and moves until it hits the limits. – ivvija Apr 28 '22 at 11:46

1 Answers1

1

See pygame.time.Clock.tick():

This method should be called once per frame.

Use pygame.time.Clock to control the frames per second and thus the game speed. The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. e.g.:

clock = pygame.time.Clock()
while True:
    clock.tick(100)

    for event in pygame.event.get():
        # [...]

If you want to move the object step by step you need to use KEYDOWN event instead of pygame.key.get_pressed():

run = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    # INDENTATION
    #-->|    

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                if x < width - box_size:
                    x = x + box_size

            if event.key == pygame.K_LEFT:
                if x > 0:
                    x = x - box_size

            if event.key == pygame.K_UP:
                if y > 0:
                    y = y - box_size

            if event.key == pygame.K_DOWN:
                if y < height - box_size:
                    y = y + box_size

See How to get keyboard input in pygame? and How can I make a sprite move when key is held down.

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action like jumping or spawning a bullet or a step-by-step movement.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Azan This application is crap.`pygame.key.get_pressed()` is not an event and thus should not be executed in the event loop. (YYou do it in the application loop, which is correct). 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) and [How to get keyboard input in pygame?](https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame/64494842#64494842) – Rabbid76 Apr 28 '22 at 12:49