Search code examples
pythonpygamecoordinatesmovedirection

How to change my ball direction by clicking keys in pygame?


I have question. My ball is allways moving in 1 of 8 directions but when i click LEFT or RIGHT arrow I want to change direction and turn in smooth arc. I need advice what should I write to conditions for keys. I'm beginner but this is my code:

import pygame
pygame.init()
import random

win = pygame.display.set_mode((1280,720))

x = random.randint(150,1130)
y = random.randint(150,570)
vel = 1
x_direction = random.randint(-vel, vel)
y_direction = random.randint(-vel, vel)

while True:
    x += x_direction
    y += y_direction
    
    pygame.time.delay(10)
    
    keys = pygame.key.get_pressed()
            
    if keys[pygame.K_LEFT]:
        pass
    
    if keys[pygame.K_RIGHT]:
        pass
    
    win.fill((0,0,0))
    pygame.draw.circle(win, (255,0,0), (x, y), 6)
    pygame.display.update()

pygame.quit()

Solution

  • I recommend to store the direction in a pygame.math.Vector2 object:

    direction = pygame.math.Vector2(x_direction, y_direction)
    

    Change the the direction by rotating the vector with rotate_ip():

    if keys[pygame.K_LEFT]:
        direction.rotate_ip(-1)
    
    if keys[pygame.K_RIGHT]:
        direction.rotate_ip(1)
    
    x += direction.x
    y += direction.y
    

    Not you can use rotate to create a vector with a random direction:

    direction = pygame.math.Vector2(1, 0).rotate(random.randint(0, 360))
    

    See also Motion and movement.


    Complete example:

    import pygame
    import random
    pygame.init()
    win = pygame.display.set_mode((1280,720))
    background = pygame.Surface(win.get_size(), pygame.SRCALPHA)
    background.fill((0, 0, 0, 1))
    clock = pygame.time.Clock()
    
    x = random.randint(150,1130)
    y = random.randint(150,570)
    direction = pygame.math.Vector2(1, 0).rotate(random.randint(0, 360))
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            direction.rotate_ip(-1)
        if keys[pygame.K_RIGHT]:
            direction.rotate_ip(1)
    
        x = max(0, min(direction.x + x, win.get_width()))
        y = max(0, min(direction.y + y, win.get_height()))
        
        win.blit(background, (0, 0))
        pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 6)
        pygame.display.update()
    
    pygame.quit()