1

I'm an extremely new programmer, so could you please explain everything that you do in depth.

here's my code for testing(please note that the indents are all wrong):

import pygame

pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True

while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    for event in pygame.event.get():
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y -= vel
            if event.key == pygame.K_DOWN:
                y += vel
            if event.key == pygame.K_RIGHT:
                x += vel
            if event.key == pygame.K_LEFT:
                x -= vel
    win.fill((0,0,0))
    pygame.draw.rect(win, (255,0,0), (x, y, width, height))   
    pygame.display.update() 

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

pygame.event.get() get all the messages and remove them from the queue. So either the 1st or the 2nd loop gets an event, but never both loops will get all events. That causes that some events seems to be missed. Handle all the events in one event loop:

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                y -= vel
            if event.key == pygame.K_DOWN:
                y += vel
            if event.key == pygame.K_RIGHT:
                x += vel
            if event.key == pygame.K_LEFT:
                x -= vel

    win.fill((0,0,0))
    pygame.draw.rect(win, (255,0,0), (x, y, width, height))   
    pygame.display.update() 

The pygame.KEYUP event occurs only once, when a button is released. If you want to achieve a continuously movement, then you've to evaluate the current state of a key. The states of all keyboard buttons can be retrieved by pygame.key.get_pressed().
Use .tick() of pygame.time.Clock to limit the frame rate, rather than pygame.time.delay. e.g.:

import pygame

pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")
clock = pygame.time.Clock()

x, y, width, height = 50, 50, 40, 60
vel = 5

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_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_LEFT]:
        x -= vel

    win.fill((0,0,0))
    pygame.draw.rect(win, (255,0,0), (x, y, width, height))   
    pygame.display.update() 

pygame.quit() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • this is just a test for how to do it, i'm trying to make a menu in my game that im programming right now – Jacob Elzingacheng Oct 29 '19 at 13:15
  • @JacobElzingacheng I, see. Is this a new question? Is the issue solved? – Rabbid76 Oct 29 '19 at 13:24
  • @JacobElzingacheng The code in the answer is a complete example. Just copy/paste it. The major issue in your code is that `for event in pygame.event.get()` is twice. – Rabbid76 Oct 29 '19 at 21:41