0

I have a window that pops up in Pygame with a green box but, despite using the K_RIGHT command it won't move right. When I press the right key it gives me no feedback suggesting I hit the right key. The box does not move right, my terminal is giving me no messages, and there is no debug message. It simply does nothing. any information on what I've done incorrectly would be highly appreciated. [code for context]

import pygame
pygame.init()
width=1800
height=900
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
white=(0,0,0)
black=(255,255,255)
x=200
y=150

running = True

def main():
    global running, screen, x, y
    screen = pygame.display.set_mode((width,height))
    while running:
        pygame.draw.rect(screen, green, ( 900, 300, x, y))
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running = False
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.type==pygame.K_RIGHT:
                    x+=20
        pygame.display.update()
            
        
main()
Popyo
  • 1
  • 2
  • You have to use [`pygame.key.get_pressed()`](https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed). 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). – Rabbid76 Jul 23 '22 at 17:47
  • @Rabbid76 I'll try that out! Why does his character move by one pixel though when mine doesn't move at all? our code should be practically the same right? – Popyo Jul 23 '22 at 17:52
  • event type CANNOT be equal to both KEYDOWN and K_RIGHT simultaneously ... so your if statement that causes movement can never be true – Joran Beasley Jul 23 '22 at 18:37

0 Answers0