0

I was making a "world's hardest game" imitation as a personal project to better myself but keyboard inputs wont work, I've tried putting the inputs in my while loop and it still didn't work for some reason.

import pygame
import sys


def rects():
    global running, x_speed, y_speed, vel_x, vel_y
    moving_rect.x += x_speed
    moving_rect.y += y_speed

# Border Collision system
if moving_rect.right >= WIDTH or moving_rect.left <= 0:
    x_speed *= -1
if moving_rect.bottom >= HEIGHT or moving_rect.top <= 0:
    y_speed *= -1

# Player Collision system
if moving_rect.colliderect(player_rect):
    running = False
    pygame.quit()
    sys.exit()

# Key Inputs
keys_pressed = pygame.key.get_pressed()
if keys_pressed == pygame.K_UP:
    player_rect.y -= vel_y
if keys_pressed == pygame.K_DOWN:
    player_rect.y += vel_y
if keys_pressed == pygame.K_LEFT:
    player_rect.x -= vel_x
if keys_pressed == pygame.K_RIGHT:
    player_rect.x += vel_x

pygame.draw.rect(screen, (255, 255, 255), moving_rect)
pygame.draw.rect(screen, (255, 115, 0), player_rect)

I haven't tried using KEYUP and KEYDOWN because it's pretty time consuming and pretty buggy when two keyboard inputs are done at the same time.

Nash
  • 25
  • 2
  • Where is your game loop, where do you handle the events? See [Why is my PyGame application not running at all?](https://stackoverflow.com/questions/65264616/why-is-my-pygame-application-not-running-at-all/65264742#65264742) – Rabbid76 May 06 '21 at 05:02
  • 1
    *"it's pretty time consuming and pretty buggy when two keyboard inputs"* - this is not true. This just the case when do it wrong. See [Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?](https://stackoverflow.com/questions/58086113/faster-version-of-pygame-event-get-why-are-events-being-missed-and-why-are/58087070#58087070) – Rabbid76 May 06 '21 at 05:04
  • I recommend reading [How to get keyboard input in pygame?](https://stackoverflow.com/questions/16044229/how-to-get-keyboard-input-in-pygame/64494842#64494842). – Rabbid76 May 06 '21 at 05:06

0 Answers0