0

I was trying to make a simple platformer in pygame. However when I was implementing movement and I held the move key(d) down, it would only move the player to the right one at a time. Whenever I held the move key down, it would only move once until I pressed it again at which point it would move one more.

Here is my code:

import pygame
import sys

# General setup
pygame.init()
clock = pygame.time.Clock()

# Setting up the main window
screen_width = 1280
screen_height = 920
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('PyPlat')
# Game rects
accelerationY = .164
speedY = 7
speedX = 3
player = pygame.Rect(1, 1, 20, 60)
ground = pygame.Rect(0, screen_height-50, screen_width, 60)
bg_color = pygame.Color('gray12')
light_blue = (62, 175, 250)
grounded = player.colliderect(ground)
while True:
    # Handling inputs
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                player.x += speedX
    # Handling collisions
    if player.colliderect(ground) == False:
        player.y += speedY
        speedY += accelerationY

    # Visuals
    screen.fill(bg_color)
    pygame.draw.rect(screen, light_blue, player)
    pygame.draw.rect(screen, light_blue, ground)

    # Updating the window
    pygame.display.flip()
    clock.tick(60)
vinzee
  • 17,022
  • 14
  • 42
  • 60

0 Answers0